# Weird Behavior of rigid Body..

How Can I stop Player from Passing thought the Objects I have added RigidBody Component to the Player Please can Anyone point out the mistake in the code.

``````void Update(){
//Increase inSpeed
if (Input.GetKey ("up") && (CarCurrentSpeed < CarTopSpeed)) {
NewSpeed = CarCurrentSpeed + CarAcceleration;
CarCurrentSpeed = NewSpeed;
}
//Decrese the speed
if (Input.GetKey ("down") && (CarCurrentSpeed > RevCarTopSpeed)) {
NewSpeed = CarCurrentSpeed - CarDecceleration;
CarCurrentSpeed = NewSpeed;
}
if (CarCurrentSpeed < 0 || CarCurrentSpeed == 0) {
CarCurrentSpeed = 0;
NewSpeed = 0;
move = Vector3.forward * CarCurrentSpeed;
}
if (CarCurrentSpeed > 0) {
move = Vector3.forward * Time.deltaTime * CarCurrentSpeed;
}
if(CarCurrentSpeed > CarTopSpeed)
{
CarCurrentSpeed = CarTopSpeed;
}
}
``````

Move a Rigidbody in FixedUpdate() using AddForce.

You can use rigidbody.velocity but this can lead to unrealistic movement.

Using standard movement like Translate and .position will not take Physics in to account and you will find objects moving through one another.

Use the velocity to find the vector for movement direction and apply a force in the opposite direction to decelerate faster.

Use rigidbody.velocity intead of tranform.Translate() and it will work.

``````public Vector3 startVelocity = new Vector3(0f,0f,1f);

void Start () {
rigidbody.velocity = startVelocity ;
}
``````

Maybe you can have something better by using MovePosition()

Changing the position of a Rigidbody object by setting its transform.position value will cause it to “teleport” directly to the new position (ie, it doesn’t move through any intermediate positions along the way). Any other Rigidbodies lying between the old position and the new one will be unaffected by the movement. Also, any colliders at the target position will react immediately with the moved rigidbody on the next update.

Your problem is definitely linked to using transform.position. Using that won’t take physic into account.

From what I see of what you’re trying to achieve you should take a look at raycasts and Collider.Contains(). Cube vs Cube collision is easy to re-create properly.

i think u need rigidbody on the cubes also plus try unchecking ( for testing purpose) the freeze rotation on player rigidbody