I have a game that player is the cube.That cube moves around.The cube has a rigidbody with box collier.When the player moves toward the wall, and touch the wall, a bit later, the player suddenly goes through. How to prevent a rigidbody to move through walls?
#pragma strict
// Move object using accelerometer
var speed = 10;
function Start () {
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
function Update () {
var dir : Vector3 = Vector3.zero;
dir.x =/*Input.acceleration.x*5;*/ Input.GetAxis("Horizontal");//Input.acceleration.x*5;//Input.GetAxis("Horizontal");//-Input.acceleration.y;
dir.z =/*Input.acceleration.y*2;*/Input.GetAxis("Vertical");//Input.acceleration.y*5;//Input.GetAxis("Vertical"); //Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// stop to cross movement
if(Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) {
dir.z = 0;
}
else {
dir.x = 0;
}
// Move object
transform.Translate (dir * speed);
}