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.
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);