Make character fall when colliding with an object

Hello. I have an object with a rigid body that can move left and right and jump on platforms. The problem I have is that when the character jumps off a platform and then moves into the platform it gets “stuck” and is unable to fall if the button is held down. What would be the best way to fix this? I figured I need to disable the direction it was moving, but I’m not sure how to do that without breaking the other direction.

The code I currently have is:

// Update is called once per frame
void FixedUpdate () {

	Vector3 p = mainCharacter.transform.position;

	if (Input.GetKey (KeyCode.A)) 
	{
		p.x -= 0.2f;
	} else 	if (Input.GetKey (KeyCode.D)) 
	{
		p.x += 0.2f;
	}

	if (Input.GetKey (KeyCode.Space)) 
	{
		if (isJumping == false)
		{
			mainCharacter.rigidbody.AddForce(Vector3.up * JumpForce);
			isJumping = true;
		}
	}

	Debug.Log (mainCharacter.rigidbody.velocity.y.ToString ());

	mainCharacter.transform.position = new Vector3(p.x, p.y, 0);
	mainCamera.transform.position = new Vector3(p.x, p.y + 2, -10.0f);
}

void OnCollisionEnter(Collision col)
{
	if (col.gameObject.tag == "Land") 
	{
		isJumping = false;
	}
}

NEVER use the position of a rigidbody directly to move it.

Instead use THIS or THIS. Add a force in the direction you want to move. Activate gravity in the rigidbody and it will fall down automatically.

For your interest: See the Time.deltaTime in the code samples? Your code would have different results, depending on the frames per second. You have to use Time.deltaTime to make it FPS independent. :slight_smile:

Read THIS!