简体   繁体   中英

C# 2D platformer movement code

This code will keep on jumping even though it is not on the ground how do you stop this (using Unity).

The Code:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{

    //Movement
    public float speed;
    public float jump;
    float moveVelocity;

    //Grounded Vars
    bool grounded = true;

    void Update () 
    {
        //Jumping
        if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W)) 
        {
            if(grounded)
            {
                GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
            }
        }

        moveVelocity = 0;

        //Left Right Movement
        if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) 
        {
            moveVelocity = -speed;
        }
        if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) 
        {
            moveVelocity = speed;
        }

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);

    }
    //Check if Grounded
    void OnTriggerEnter2D()
    {
        grounded = true;
    }
    void OnTriggerExit2D()
    {
        grounded = false;
    }
}

There are a couple of ways to achieve this but following your current implementation: why not set the isGrounded flag to false when you jump and just let the trigger handle resetting the flag when you land?

Some things you could check: Are all your colliders 2D and set to be triggers?
Have you checked your layer collisions are actually happening?

public class PlayerController : MonoBehaviour 
{

    //Movement
    public float speed;
    public float jump;
    float moveVelocity;

    //Grounded Vars
    bool isGrounded = true;

    void Update () 
    {
        //Jumping
        if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W)) 
        {
            if(isGrounded)
            {
                GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
                isGrounded = false;
            }
        }

        moveVelocity = 0;

        //Left Right Movement
        if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) 
        {
            moveVelocity = -speed;
        }
        if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) 
        {
            moveVelocity = speed;
        }

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);

    }
    //Check if Grounded
    void OnTriggerEnter2D()
    {
        isGrounded = true;
    }
}

我很确定你需要为 () 中的 GetKeyDown 设置 "" 但我不确定我对此很陌生,我的用户名说它。

I am new to programming so I took this as a challenge. I just noticed this was five years ago, but ill send my solution anyways. I still don't really know how your script works. lol

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement: MonoBehaviour
{

    //Movement
    public float speed;
    public float jump;
    float moveVelocity;
    public Rigidbody2D rb;

    void Update()
    {
        //Grounded?
        if (rb.position.y < 5.52)
        {
            //jumping
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
            {
        
            GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
            }
      }
  
       moveVelocity = 0;

       //Left Right Movement
       if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
       {
           moveVelocity = -speed;
       }
       if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
       {
           moveVelocity = speed;
       }

       GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);

    }
}

I know it's been 6 years, but I found a solution that worked for me.

 //Movement

public float speed;
public float jump;
float moveVelocity;
public Rigidbody2D rb;
bool isGrounded;

void Update()
{
    //Grounded?
    if(isGrounded == true){
        //jumping
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
        {
    
        GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
        }
        
  }

   moveVelocity = 0;

   //Left Right Movement
   if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
   {
       moveVelocity = -speed;
   }
   if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
   {
       moveVelocity = speed;
   }

   GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);

}
 void OnCollisionEnter2D(Collision2D col)
{
    Debug.Log("OnCollisionEnter2D");
    isGrounded = true;
}
  void OnCollisionExit2D(Collision2D col)
{
    Debug.Log("OnCollisionExit2D");
    isGrounded = false;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM