Unity 2d jumping script

后端 未结 3 1846
北恋
北恋 2021-01-02 01:37

Does anyone have a good jumping script for 2d games in unity? The code I have works but still is far from jumping, it looks like it is flying.

using UnityEng         


        
3条回答
  •  渐次进展
    2021-01-02 02:20

    The answer above is now obsolete with Unity 5 or newer. Use this instead!

    GetComponent().AddForce(new Vector2(0,10), ForceMode2D.Impulse);
    

    I also want to add that this leaves the jump height super private and only editable in the script, so this is what I did...

        public float playerSpeed;  //allows us to be able to change speed in Unity
    public Vector2 jumpHeight;
    
    // Use this for initialization
    void Start () {
    
    }
    // Update is called once per frame
    void Update ()
    {
        transform.Translate(playerSpeed * Time.deltaTime, 0f, 0f);  //makes player run
    
        if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))  //makes player jump
        {
            GetComponent().AddForce(jumpHeight, ForceMode2D.Impulse);
    

    This makes it to where you can edit the jump height in Unity itself without having to go back to the script.

    Side note - I wanted to comment on the answer above, but I can't because I'm new here. :)

提交回复
热议问题