# How to add collision to this script in Unity

I Want to be able to have collision were my player can't go through objects with colliders un Unity but I don't know how to make that happen, can you please help?

public float moveSpeed;
private Rigidbody2D RB;
Vector2 movementDirection;
Vector2 moveVelocity;

private float inputX;
private float inputY;

// Use this for initialization
void Start ()
{
RB = GetComponent<Rigidbody2D>();
}

// Update is called once per frame (Good place for Input so we can check on every frame)
void Update()
{
// Gather the Input of the X and Y axis and put it into the assigned variables
inputX = Input.GetAxisRaw("Horizontal");
inputY = Input.GetAxisRaw("Vertical");
// Take the numbers from both the X and Y axis and combine it into a Vector2 called "movementDirection"
movementDirection = new Vector2(inputX, inputY);
// Normalize moveDirection and add moveSpeed to get the final result of where the player needs to go
moveVelocity = movementDirection.normalized * moveSpeed;
}

// FixedUpdate is called between every frame and every 2 frames to try to account for different computer speeds (Good for applying force so that gravity and so on isin't as affected by PC prossessing power)
void FixedUpdate ()
{
// Take the current position and apply the final result/moveVelocity variable then multiply it my Time.fixedDeltaTime (We use "fixedDeltaTime" because it is good practice to do this in the "FixedUpdate" Function)
RB.MovePosition (RB.position + moveVelocity * Time.fixedDeltaTime);
}

• Do you have a Collider component attached to your Player gameobject? They are usually added using the Add Component button in the inspector window when an object is selected. – Gabriele Vierti Dec 8 '18 at 9:01
• Instead of downvoting every answer under your question try to edit it or add comments on why the answers we have provided don't satisfy your requirements. – Gabriele Vierti Dec 14 '18 at 6:33
• Comments are not for extended discussion; this conversation has been moved to chat. – DMGregory Mar 29 at 17:43

Collision detection in Unity has a few non-obvious pitfalls.

First of all, check the collision action matrix at the end of the Unity Manual article on colliders. If you want your player-character to not fall through other objects, then:

• Your player and the object must have colliders in the right shape
• Neither collider must have the "is Trigger?" checkbox marked
• Your player-character must have a Rigidbody
• The object must either also have a Rigidbody or its colliders must have the "Static" checkbox marked (which means that this collider will not move during the game)

Another potential problem might be that you are mixing 2d colliders and 3d colliders. The 2d collision system and the 3d collision system are completely separated from each other. 2d colliders and 3d colliders do not interact with each other.

When you also verified this, then another possible pitfall is that they are on different physics layers which are not configured to interact with each other.

When collisions still don't work the way you expect them to do: How are you moving your player-character? Are you modifying its transform.position or are you using Rigidbody.MovePosition? If you do that, you aren't moving the character, you are teleporting it. If you try to teleport an object into or through a collider, the Unity physics engine will let you do that. For that reason you should always move an object by adding forces to its Rigidbody.

• As you can see at the bottom of the script I am using Rigidbody.MovePosition, what would you recommend using and how do I implement it? – Ultra Gamer Dec 8 '18 at 17:00
• On the line in the "FixedUpdate" function the player stops responding to input and glides 45 degrees down and to the right. Can you please give an example of what you would recommend? – Ultra Gamer Dec 8 '18 at 19:25
• @UltraGamer When you are using any Newtonian physics in your game (objects you can push around), you should use rigidbody.AddForce. When you haven't, you can change the rigidbody.velocity. – Philipp Dec 8 '18 at 22:01
• Well I don't want the player to be pushed but when I try the rigid rigidbody.velocity, that causes the strange 45-degree angle thing. And changing it to rigidbody.addForce doesn't work at all (It stops responding to input entirely) Any suggestions? – Ultra Gamer Dec 9 '18 at 6:32
• @UltraGamer You shouldn''t add the position to the velocity. That makes no sense. The velocity is the speed the object is supposed to move by. So when the object is supposed to stay stationary, the velocity should be 0. – Philipp Dec 9 '18 at 20:18

It is usually done by using the Add Component button as someone in the comments said, but what I think that you asked (although you are not very clear) is that you want to add it via script

In you start method, you need to add this code:

gameObject.AddComponent(typeof(BoxCollider2D));


This way, you can add any other collider by just changing the type over there in that line of code.

Hope this helps!

• No, just adding colliders don't fix it, anything else? – Ultra Gamer Dec 8 '18 at 17:00

Colliders are a type of Component that you would tipically attach to your GameObjects in Unity's Inspector window, by selecting one in your scene and choosing one from the list;

However, if for some reason you need to add a component to an object at runtime, you can use the GameObject.AddComponent<T>().

There are multiple collider types you can choose from (some also have a 2D counterpart)

• BoxCollider
• MeshCollider
• SphereCollider
• Etc...

If you want to add a BoxCollider component to your GameObject you would do:

YourObject.AddComponent<BoxCollider>();


YourObject is the name of the GameObject you are attaching the component to. If it's on the current instance, this can also be used

• No, just adding colliders don't fix it, anything else? – Ultra Gamer Dec 8 '18 at 17:01