Hello! I have a wall (a cube) and a rat that I made using 3dmax (a .fbx), both with a box colider, but whem my rat moves (gameObject.transform.Translate) it crosses the wall.
I would like the rat stops walk when touch the wall.
Hello! I have a wall (a cube) and a rat that I made using 3dmax (a .fbx), both with a box colider, but whem my rat moves (gameObject.transform.Translate) it crosses the wall.
I would like the rat stops walk when touch the wall.
Transform.Translate doesn’t care about physics! You need to have a rigidbody component on your rat, because two colliders on their own are assumed to be static, and do not collide with eachother! For the physics engine to detect collisions and push objects out of other colliders, you need to be using a non-kinematic rigidbody.
You should take all of your movement code out of the Update loop, put it into the FixedUpdate loop, and use rigidbody.MovePosition instead of transform.Translate for your motion. This should make your colliders interact with each other!
Use a raycast collider component on the rat for this. This is not a script, it is part of one to get you started. For more help, look up Unity script reference RaycastHit.
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, direction, hit, 1))