# BoxCollider not blocking other object(s)

I'm really newbie , watched about 20 videos about and tried to do something. But I still have problems with collisions.

I'm working on 2D. I have a Sprite and a Cube (GameObject>3D Object>Cube). sprite is moving to a random position on screen and when it arrived to that position it's moving to another random position. It's going on like this forever. But when sprite hit to cube, it should change direction (when collision occured) , but instead it's passing through it.

Sprite has these components:

Cube has these components:

BallControl Script

public class Move : MonoBehaviour {

public float moveSpeed = 3f;
private Transform tf;
Vector3 mousePosition;
bool isMoving=false;
void Start()
{
tf = GetComponent<Transform> ();
}

void Update ()
{
if(!isMoving) // Find a new position to move
walkToRandomPosition();
if(isMoving) // If we already found a position to move
transform.position = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
if (transform.position == mousePosition)
{ // Stop
isMoving = false;
}
}

void walkToRandomPosition()
{
mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), Camera.main.farClipPlane/2));
mousePosition.z = 0f; // Because we are working on 2D
isMoving=true;
}
}


Can you tell me why cube's Box Collider not blocking sprite? Should i add something like this to my BallControl script?

 void OnCollisionEnter2D(...)
{
// Change direction (find new position)
}


UPDATE 1

Just add a new sprite and gave BoxCollider2D , now collision working as expected. But it's still not working with 3D cubes.

• Maybe the sprite is missing the box collider, try adding a regular collider to sprite. – Vadim Tatarnikov Aug 30 '15 at 17:33
• What do you mean by regular collider ? – Eray Aug 30 '15 at 17:38
• Sorry my bad I meant box collider. – Vadim Tatarnikov Aug 30 '15 at 17:39
• But it already has boxCollider2d ? – Eray Aug 30 '15 at 17:41

If your sprite has a 2d collider you want to replace the cubes collider with a 2d as well.

• Wow, now it's working. Can you tell me, z-scale matter for 2d objects? Thank you. – Eray Aug 31 '15 at 15:28
• typically you will want to freeze the z rotation in the rigidbody so he doesn't fall over as he collides with things – Justin Markwell Aug 31 '15 at 15:37
• z- scale can be left alone – Justin Markwell Aug 31 '15 at 15:44

Hey your ball's rigid body has isKinematic true , you need to uncheck it so that the rigidbody starts colliding with other colliders.

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position. Kinematic bodies also affect the motion of other rigidbodies through collisions or joints. Eg. can connect a kinematic rigidbody to a normal rigidbody with a joint and the rigidbody will be constrained with the motion of the kinematic body. Kinematic rigidbodies are also particularly useful for making characters which are normally driven by an animation, but on certain events can be quickly turned into a ragdoll by setting isKinematic to false.

• thank you for answer.Now isKinematic is false and Gravity Scale = 0 (otherwise ball falling down) but nothing changed. – Eray Aug 30 '15 at 17:31
• Just for testing , put the gravity on and place the ball over a collider and let it fall down. Check if the ball collides with it or passes through it. – Hash Buoy Aug 31 '15 at 4:14
• nothing changed. Still passing through it. BTW colliders working with sprites. I add 2-3 new sprites with same components, they are changing direction when collision occured. – Eray Aug 31 '15 at 14:08