Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Objects not colliding (going through)

Discussion in '2D' started by Dmumzo, Dec 16, 2019.

  1. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    Hello,

    I have one object A with a boxcollider2d and a rigidbody2d with collision detection set to "continuous".
    I have another object B, which is a wall, with the same characteristics.

    Object A moves on the X axis with the code
    gameObject.transform.Translate(speed * Time.fixedDeltaTime, 0, 0);

    If speed is sufficiently high, object A goes through B. How can I solve this?

    I have read that apparently it is possible to set the collision detection to "continuous dynamic". But I can only choose between continuous and discrete.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Translate is a function that sets the transform position directly. It's essentially a teleport, and then the physics system figures out the result later and pushes them apart. If your object moves fast enough, it will just teleport through the other object, or enough to be pushed out the other side once the physics system corrects it.

    For proper physics behavior, you should let the rigidbody control the transform and only use rigidbody functions for motion, then it will respect collisions, and prevent tunneling with continuous collision detection.

    Get a reference to your rigidbody component, and use
    myRigidbody.MovePosition(myRigidbody.position + speed * Time.deltaTime);


    https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html
     
    Last edited: Dec 19, 2019
  3. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    Thanks a lot!
     
    LiterallyJeff likes this.