- Home /
The grounded bool of my jump function doesn't become true, using ray cast
Hi all,
Would be grateful for some help.
I've created a capsule with a box collider. I've attached an empty game object to the bottom of the capsule. The game is on a 2D plane.
I've created a jump function using rigid body.addforce. I've also attached a ray cast script onto the empty gameObject at the base of my capsule (the player).
I believe I've set it so that, every time the ray cast detects anything below it (in my case, a plane that I'm using as the floor), it should change a bool value (I've called it Grounded) to true. As long as Grounded is true, it should allow me to access a Jump function on another script to allow my player to jump.
Once my player jumps, I should have a script that allows me to change the Grounded bool value back to false, so that my player can't double jump by accident.
However, after I have jumped once, when my character lands back onto the floor again, the bool value remains on false. Is there something wrong with my script?
The jump function on my playerMove script
void Update ()
{
transform.rotation = Quaternion.Euler (lockPos, lockPos, lockPos);
Vector2 x = Input.GetAxis ("Horizontal") * transform.right * Time.deltaTime * speed;
transform.Translate (x);
// If "Jump" is pressed and player grounded == true
if (Input.GetButtonDown ("Jump") && (grounded))
{
Jump ();
}
}
//Player jumps and the grounded bool value becomes false to prevent double jump
public void Jump()
{
rigidbody.AddForce(Vector2.up * jumpForce);
Debug.Log ("Jump has been pressed");
grounded = false;
Debug.Log (grounded);
}
The ground check script on my empty game object that holds the ray cast on the character
private float distanceToGround = 0.15f;
public Transform player; //allows me to access the player move script
public LayerMask GroundLayer;
void Update ()
{
PlayerMove playerScript = player.GetComponent<PlayerMove>();
RaycastHit hit;
Ray groundedRay = new Ray (transform.position, -Vector2.up);
Debug.DrawRay (transform.position, -Vector2.up * distanceToGround);
//If the ray cast hits something below, the grounded bool value becomes true?
if (Physics.Raycast (groundedRay, out hit, distanceToGround)) {
playerScript.grounded = true;
Debug.Log ("Player is touching ground");
}
}
Is the Debug.Log("Player is touching ground") getting called at all?
Yes. I have my player suspended in mid at the start. Once I hit play, the player will fall to the ground. Once it hits the ground, the console does indeed print out that the player is touching the ground. I am then able to hit the spacebar to make the player 'jump'. But when the player lands back onto the floor again, it doesn't register that the player is touching the floor :(
Alright, how often are you calling the Raycast function? From your code it looks like it isn't in Update?
Apologies gcoope. I forgot to include the Update() in the above script, but in my code, it is definitely in Update, but in the GroundCheck update script that I have attached to an empty game object, which is a child to the player object.
Have you tried increasing the distanceToGround variable gradually? $$anonymous$$aybe you just need a longer distanceToGround, say 0.175f
Answer by Darth Futuza · Jul 31, 2014 at 01:40 AM
PlayerMove playerScript = player.GetComponent<PlayerMove>();
RaycastHit hit;
Ray groundedRay = new Ray (transform.position, -Vector2.up);
Debug.DrawRay (transform.position, -Vector2.up * distanceToGround);
//If the ray cast hits something below, the grounded bool value becomes true?
**if (Physics.Raycast (groundedRay, out hit, distanceToGround)) {
playerScript.grounded = true;**
Debug.Log ("Player is touching ground");
}
}
The bolded part is probably causing the problem. Are you sure playerScript.grounded actually writes to grounded? Is grounded a public/private variable?
grounded is a public bool variable on the player$$anonymous$$ove script (which I access from here). But increasing the distanceToGround variable for the ray cast did help a lot! Although it seemed to be only temporary...
Your answer