1
\$\begingroup\$

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:

enter image description here

Cube has these components:

enter image description here

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.

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

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

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

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.

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ thank you for answer.Now isKinematic is false and Gravity Scale = 0 (otherwise ball falling down) but nothing changed. \$\endgroup\$ – Eray Aug 30 '15 at 17:31
  • \$\begingroup\$ 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. \$\endgroup\$ – Hash Buoy Aug 31 '15 at 4:14
  • \$\begingroup\$ 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. \$\endgroup\$ – Eray Aug 31 '15 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.