Problem with collisions2D

Hi there, I am making a college project and i can’t find the solution how to fix a problem when my player object collides with the walls. he doesn’t stops, just passing through them, can any one please help me with this ? and yes, the Trigger and is Kinematic options are unchecked. Here’s my player controller code.

#pragma strict

var myRigidbody2D : Rigidbody2D;
var angle:float = 0;

private var anim : Animator;

public var speed2 : float;
public var rotationSpeed: float;
         //transform
var myTrans: Transform;
         //object position
var myPos: Vector2;
         //object rotation
var myRot: Vector3;


function OnTriggerEnter2D(col: Collider2D)
{
    if(col.gameObject.tag == "Coins")
    {
        GameMaster.points += 1;
        Destroy(col.gameObject);
    }
}
    function Start ()
    {
        myRigidbody2D = GetComponent(Rigidbody2D);

        anim = GetComponentInChildren(Animator);
    
        myTrans = transform;
        myPos = myTrans.position;
        myRot = myTrans.rotation.eulerAngles;
    }
    
    function FixedUpdate()
    {
        angle = myTrans.eulerAngles.magnitude * Mathf.Deg2Rad;

                //rotate object Right & Left
                 if (Input.GetKey (KeyCode.D))
                 {
                         myRot.z -= rotationSpeed;
                 }
                 if (Input.GetKey (KeyCode.A))
                 {
                         myRot.z += rotationSpeed;
                 }

                 //move object Forward & Backward
                 if (Input.GetKey (KeyCode.W))
                 {
                         myPos.x += (Mathf.Cos (angle) * speed2) * Time.deltaTime;
                         myPos.y += (Mathf.Sin (angle) * speed2) * Time.deltaTime;
                      
                         anim.SetBool("Swim", true);
                
                 }
                 else
                 {
                    anim.SetBool("Swim", false);
                 }
              
                 if (Input.GetKey (KeyCode.S))
                 {
                         myPos.x += (Mathf.Cos (angle) * -speed2/3) * Time.deltaTime;
                         myPos.y += (Mathf.Sin (angle) * -speed2/3) * Time.deltaTime;
                 }
    
    
    
        myTrans.position = myPos;
        myTrans.rotation = Quaternion.Euler (myRot);

    }

Use code tags when posting code, please.

–Eric

sorry, didn’t knw i had to use tags, its done now, can u give me a hand with this ?

You’re setting the position explicitly, which bypasses the physics engine. Either use Transform.Translate, or Rigidbody2D.MovePosition. Also you can simplify the code by using Transform.Rotate instead of doing the math manually.

–Eric

Thanks, everything works now.

Transform.Translate will obey the collision matrix? I thought that was a “move by” explicit position set?

Yep.

Nope.

–Eric

Are you certain? What would prevent Translate from moving right through a collider if you move by 1000 units?

I wouldn’t have said it if I wasn’t certain. :wink:

What prevents standard physics movement from going right through a collider if the velocity causes it to move 1000 units in a physics frame?

–Eric

Just making sure :smile:

If you use Continuous Collision on the rigidbody, MovePosition will never pass through a collider no matter how far you tell it to move. If you use Translate, it will teleport to the new position.

My (albeit vague) understanding is that Rigidbody functions are intended to respect collisions while altering the object’s Transform, while Transform class functions do not consider collisions at all.

Exactly. Transform.Translate does obey the collision matrix as long as you’re talking about discrete physics, whereas Transform.position does not obey the collision matrix at all.

–Eric

1 Like

Ah yes, I see your point.