繁体   English   中英

C# 2D平台动作代码

[英]C# 2D platformer movement code

即使它不在地面上,这段代码也会继续跳跃你如何阻止它(使用 Unity)。

编码:

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;
    }
}

有几种方法可以实现这一点,但要遵循您当前的实现:为什么不在跳跃时将 isGrounded 标志设置为 false 并在着陆时让触发器手柄重置标志?

您可以检查一些事情:您的所有碰撞器都是 2D 的并设置为触发器吗?
你有没有检查过你的图层碰撞是否真的发生了?

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 设置 "" 但我不确定我对此很陌生,我的用户名说它。

我是编程新手,所以我把这当作一个挑战。 我才注意到这是五年前的事,但无论如何我都不会发送我的解决方案。 我仍然不知道你的脚本是如何工作的。 哈哈

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);

    }
}

我知道已经 6 年了,但我找到了一个对我有用的解决方案。

 //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;
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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