- Home /
The question is answered, right answer was accepted
Jump on the ground
Hello! I know that a lot of these questions have already been asked, but I have looked through many, and none of them seem to work for me. My problem is: I want my player to jump only when he is on the ground.
I'm new with scripting, i'm really a beginner. The original code is from the unity tutorial "roll-a-ball".
Sorry for my bad english, and here is my code, any help is appreciated!
using UnityEngine;
using System.Collections;
public class Movimento : MonoBehaviour {
public float speed;
public float jump;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
if (Input.GetKeyDown(KeyCode.Space)) {
GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0)); }
rb.AddForce (movement * speed);
}
}
Answer by sparkzbarca · Aug 30, 2016 at 03:45 AM
there are multiple ways to do this.
you can cast a ray from the center of the player object or the player objects attached collider "down" that is the local down not global down and do a distance check. Basically if the player is say 6 feet tall so the center is 3 feet then if your 3.1 feet or less from the ground allow a jump, if not, don't.
if that seems too complicated this is really the same thing but it hides writing the code for you.
Attach a sphere collider to your player and check the is_trigger box. then make it just big enough so the bottom edge of the collider is touching your players feet. then for the collider do the code
collider ontriggerenter (collider other)
{
if other.gameobject == terrain
(then your touching the terrian because its inside the sphere)
//allow jumping
}
that's not foolproof by the way. If your falling very close to the edge of the cliff you could be allowed to jump because you have terrain in your sphere. This is just to help you get started.
A better way to do that method would probably be to use a capsule collider or a cube collider as a trigger, It would [hopefully] fix the issue probably.
Answer by hellopeople0004 · Aug 30, 2016 at 03:57 AM
Try adding this to your script:
void OnTriggerEnter (Collider other){
isGrounded = true;
}
void OnTriggerExit (Collider other){
isGrounded = false;
}
(Make sure you add isGrounded as a variable to your script) Then where your jump code is, check if isGrounded is true.
if(Input.GetKeyDown(KeyCode.Space)) {
if(isGrounded) {
GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0));
}
}
What this [should] do/es is detect if the player is colliding with any object, and if it is then isGrounded is true, allowing you to jump. When you aren't touching anything (e.g in air) then you cannot jump. Hope this helped! Let me know if there are any errors, I did this in JavaScript and converted it to CS so I cannot guarantee there will be no errors (but there shouldn't be any.)
1 Issue however, if you are touching a wall, you will be able to repeat jumping. To fix, just change the first code to:
void OnTriggerEnter (Collider col){
if (col.tag == Ground) {
isGrounded = true;
}
}
void OnTriggerExit (Collider col){
isGrounded = false;
}
And then set your terrain / ground's tag to "Ground" Again, let me know if there are any issues :)
Follow this Question
Related Questions
How do i destroy only 1 object when 2 instances of the same object collide ? 2 Answers
OnCollisionEnter2D not being called on entering new tile once already called. 1 Answer
GJK box problem 0 Answers
How to stop object from going through walls. 4 Answers
Unity 5: Clean way to manage dynamically created GameObjects 1 Answer