Jump Script
I am currently having issues with a jump script as you can see with the code below, I have tried many variations of the code and still no success, the issue i am having is that the character jumps up and down way too fast and when you press the space bar after an initial jump it continues to jump.
using UnityEngine;
using System.Collections;
public class Jumping: MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Jump ();
}
void Jump ()
{
if (Input.GetKeyDown ("space")) {
transform.Translate (Vector3.up * 130 * Time.deltaTime);
}
}
}
make a bool variable and test if true then jump and when player jump set it false and for the jump speed just increase or decrease 130
Answer by jgodfrey · Feb 24, 2016 at 05:06 PM
You'll need to have some notion of your character being "grounded" (on the ground). This can be done via collision events, trigger events, etc. Once you know that your character is on the ground, you can allow a jump. However, once the character has jumped, you'll want to not allow another jump until the character becomes grounded again (typically done with a boolean variable). Unless, of course, you want to allow a double-jump. In that case, you could allow one additional jump before requiring the character to be grounded again.
None of the above is overly difficult to do, but is too broad to simply provide code for. You should work on the various pieces mentioned above and ask more specific questions as you run into trouble.
Answer by XaenSai · Mar 03, 2016 at 02:06 PM
I have attempted to modify the script further but i wish to extend this so that it checks if the player is grounded, how would i go about this ? improved code below.
using UnityEngine;
using System.Collections;
public class Jumping: MonoBehaviour {
public Vector2 velocity;
public Vector2 speed;
bool jump;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Jump ();
}
void Jump ()
{
jump = false;
if (!jump)
{
if (Input.GetKeyDown ("space"))
{
jump = true;
transform.Translate(Vector3.up * 75 * Time.deltaTime);
}
}
/*else
{
//jumping is true.
//Check to see if player is on the floor.
if (PlayerOnFloor())
{
jump = false;
}
}*/
}
}