TicoAnd Alpha
TicoAnd Alpha

Reputation: 21

How not to bypass the colliders unity

I'm so noob in unity, my problem is that i move with transform.position and when my sprite(2d game) go against a plane he ignore his collider that is convex(player have a 2d box collider. Pls help me.

Upvotes: 1

Views: 833

Answers (2)

Fredrik
Fredrik

Reputation: 5108

To add to the other answer, when you're moving with transform.position you're essentially "teleporting" around. And you don't care about colliders when teleporting.

Adding to the other answers, you can move by setting the rigidbody's velocity, a quick example:

Rigidbody2D rb;
float speed = 50;

void Start() 
{
    rb = GetComponent<Rigidbody2D>();
}

void Update() 
{
    var movement = Vector2.Zero;

    if (Input.GetKeyDown(KeyCode.A)) {
        movement.x -= 1;
    }

    // more inputs here

    rb.velocity = movement * speed;
}

Upvotes: 1

varunkaustubh
varunkaustubh

Reputation: 351

There are 2 things needed for collisions to work in your case:

  1. Make sure that one of the colliding objects has a RigidBody2D attached. Collisions will not work in unity if one of the colliding GameObjects does not have an attached RigidBody.

  2. Do not move physics bodies with transform.translate, or by setting the transfrom.position value directly. it is a physics object, and should be treated as such. move them with AddForce or MovePosition

Upvotes: 1

Related Questions