- Home /
Problem with rigidbody jumping off of platform.
I have a rather simple issue that I just can't figure out. I'm making a game where characters (rigidbody prefabs) walk by themselves and jump when they reach the edge of a platform. I have this working perfectly when they jump to the right, but when they reach the left side of a platform, the rigidbody suddenly shoots straight up into the air and doesn't come down. The most confusing thing is the code is identical for each direction except for the direction and the variable that determines the direction. Am I missing something? Thanks for any help. Here is the code I have:
function Start() {
//get the distance to the ground
distToGround = collider.bounds.extents.y;
}
//Checks distance to ground
function IsGrounded(): boolean {
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function Update() {
if(IsGrounded()) {
isJumping = false;
}
//Starts squirrel moving to the right when it touches the ground
if(IsGrounded() && squirrelDirection == false)
{
//moves squirrel to the right
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
}
if(IsGrounded() && squirrelDirection == true)
{
//moves squirrel to the left
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
//Makes squirrel jump when they reach the edge of a platform
if(IsGrounded() == false && isJumping == false && squirrelDirection == false)
{
rigidbody.AddForce(transform.up * jumpForce);
rigidbody.AddForce(transform.right * jumpForce);
isJumping = true;
}
//This is where it tells me I have an error. On the (transform.left...) line.
if(IsGrounded() == false && isJumping == false && squirrelDirection == true)
{
rigidbody.AddForce(transform.up * jumpForce);
rigidbody.AddForce(transform.left * jumpForce);
isJumping = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613103523im_/https://answers.unity.com/themes/thub/images/avi.jpg)