Jumping C#

Hi all im having a bit of a problem making my character jump. Its just a cude that im using for a prototype 2.5D platformer.

Ive spent the past hour looking on google but i can find anything that will help.

Ive gotten the object to jump up but it keeps getting faster every time you press the ‘space’ bar and it wont come back down :neutral:

Heres all of my code for the player:

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour 
{
    public float speed;
    public float jump = 10.0f;
    public float gravity = 10.0f;


        // Start
        //-----------------------------------------------------------------------------------
	// Use this for initialization
	void Start () 
    {
	
	}
	

    // Update
    //-----------------------------------------------------------------------------------
	// Update is called once per frame
	void Update () 
    {
        Movement();
	}


    // Movement
    //-----------------------------------------------------------------------------------
    void Movement()
    {
        if (Input.GetKey(KeyCode.W)) // Up 
        {
            float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime * 5;
            transform.Translate(Vector3.up * moveVertical);
        }

        if (Input.GetKey(KeyCode.S)) // Down
        {
            float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime * 5;
            transform.Translate(Vector3.up * moveVertical);
        }

        if (Input.GetKey(KeyCode.D)) // Left
        {
            float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime * 5;
            transform.Translate(Vector3.left * moveHorizontal);
        }

        if (Input.GetKey(KeyCode.A)) // Right
        {
            float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime * 5;
            transform.Translate(Vector3.left * moveHorizontal);
        }

        if (Input.GetKeyDown(KeyCode.Space)) // Jump
        {
            rigidbody.AddForce(Vector3.up * jump);
        }
    }
}

I hope someone can help me as soon as possible.

Thanks

public float gravity = 10.0f;
is never used.
Unity should have given a warning as such in the console.

Try this:

using UnityEngine;

using System.Collections;

 

public class player : MonoBehaviour 

{

    public float speed;

    public float jump = 10.0f;

    public float gravity = 10.0f;

 

 

        // Start

        //-----------------------------------------------------------------------------------

    // Use this for initialization

    void Start () 

    {

    

    }

    

 

    // Update

    //-----------------------------------------------------------------------------------

    // Update is called once per frame

    void Update () 

    {

        Movement();

    }

 

 

    // Movement

    //-----------------------------------------------------------------------------------

    void Movement()

    {

        if (Input.GetKey(KeyCode.W)) // Up 

        {

            float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime * 5;

            transform.Translate(Vector3.up * moveVertical);

        }

 

        if (Input.GetKey(KeyCode.S)) // Down

        {

            float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime * 5;

            transform.Translate(Vector3.up * moveVertical);

        }

 

        if (Input.GetKey(KeyCode.D)) // Left

        {

            float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime * 5;

            transform.Translate(Vector3.left * moveHorizontal);

        }

 

        if (Input.GetKey(KeyCode.A)) // Right

        {

            float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime * 5;

            transform.Translate(Vector3.left * moveHorizontal);

        }

 

        if (Input.GetKeyDown(KeyCode.Space)) // Jump

        {

            rigidbody.AddForce(Vector3.up * jump);

        }
            rigidbody.AddForce(gravity);
    }

}

@aaro4130, you’re passing a float to rigidbody.AddForce(). That function takes a vector3.

You’d need:

rigidbody.AddForce(new Vector3(0, -gravity, 0));

^negative gravity, assuming you want gravity to return you down.

edit: preferably, replace the current gravity variable with the vector3 so you don’t have to ‘new’ a vector3 every update.

Ive tried the rigidbody -gravity one but it still doesnt work, and for some reason my cude(the player) wont stay on the x-axis, any suggestions?

Thanks fr the replies :slight_smile: