Bounce Upwards After Collision

Hi,

I’ve been trying to create a 3D platformer using a simple player controlled cube and cubes for the walls (not doing anything too complex atm).

However, when the player moves left to a wall (moving left along the x-axis, the players cubes bounces upwards this is also works when moving right.
The code to move is done via translation:

transform.Translate(Input.GetAxis("Horizontal"),0,0);

The cube just seems to bounce up and away from the other collider and if you hold the directional key down it climbs up it.

I’ve spent a lot of time searching this site and none of the answers have helped me yet!
I’ve altered the settings of the colliding boxes, adjusted the project time and physics settings and to no avail. :frowning:

Thanks in advance :slight_smile:

The problem may be that transform.Translate() is ignoring collisions, as it alters the position directly. I recommend that you use:

(or http://docs.unity3d.com/412/Documentation/ScriptReference/CharacterController.SimpleMove.html)
Unity - Scripting API: Rigidbody.AddForce
(or Unity - Scripting API: Rigidbody.AddRelativeForce)

Yeah, if you’re using rigidbodys, then try to avoid changing the position directly of the rigidbody, as that will ignore any collisions while moving, and will cause things like you described. In your case, I would do the following (use this in FixedUpdate()):

rigidbody.velocity = new Vector3(Input.GetAxis("Horizontal",0,0);

Although changing the velocity isn’t recommended, I think it will be enough for your game. If you really want to play it safe, use rigidbody.AddForce in FixedUpdate().