The character has been added box collier 2D and Rigid Body 2D. It will move on a plane like this
The plane has been added box collider 2D
When I press left it works totally fine, but when I press right for a period of time, like 1 second, the character will float to upper and go back down to the plane when ‘right’ button not pressed any more.
//Here is the code
void Update () {
if (Input.GetAxis(HORISONTAL) < -0.1f)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetAxis(HORISONTAL) > 0.1f)
{
transform.localScale = new Vector3(1, 1, 1);
}
}
void FixedUpdate()
{
h = Input.GetAxis(HORISONTAL);
//move the character
mushRoomRigid2D.AddForce((Vector2.right * speed) * h);
//limit the speed of character
if (mushRoomRigid2D.velocity.x > maxSpeed)
{
mushRoomRigid2D.velocity = new Vector2(maxSpeed, mushRoomRigid2D.velocity.x);
}
if (mushRoomRigid2D.velocity.x < -maxSpeed)
{
mushRoomRigid2D.velocity = new Vector2(-maxSpeed, mushRoomRigid2D.velocity.x);
}
}
And the settings of the character object: