C#2D平台成型器移动代码

C#2D平台成型器移动代码,c#,unity3d,C#,Unity3d,此代码将继续跳转,即使它不在地面上,您如何阻止它(使用Unity) 守则: using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { //Movement public float speed; public float jump; float moveVelocity; //Grounded Vars bool

此代码将继续跳转,即使它不在地面上,您如何阻止它(使用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;
    }
}
使用UnityEngine;
使用系统集合;
公共类玩家控制器:单行为
{
//运动
公众浮标速度;
公众浮标跳跃;
浮动速度;
//接地变压器
bool-grounded=true;
无效更新()
{
//跳跃
if(Input.GetKeyDown(KeyCode.Space)| Input.GetKeyDown(KeyCode.UpArrow)| Input.GetKeyDown(KeyCode.Z)| Input.GetKeyDown(KeyCode.W))
{
如果(接地)
{
GetComponent().velocity=newvector2(GetComponent().velocity.x,jump);
}
}
移动速度=0;
//左右运动
if(Input.GetKey(KeyCode.LeftArrow)| | Input.GetKey(KeyCode.A))
{
移动速度=-速度;
}
if(Input.GetKey(KeyCode.RightArrow)| | Input.GetKey(KeyCode.D))
{
移动速度=速度;
}
GetComponent().velocity=newvector2(moveVelocity,GetComponent().velocity.y);
}
//检查是否接地
void OnTiggerEnter2D()
{
接地=正确;
}
void OnTriggerExit2D()
{
接地=假;
}
}

有两种方法可以实现这一点,但遵循您当前的实现方式:为什么不在跳转时将isground标志设置为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;
    }
}
公共类PlayerController:MonoBehavior
{
//运动
公众浮标速度;
公众浮标跳跃;
浮动速度;
//接地变压器
bool isGrounded=true;
无效更新()
{
//跳跃
if(Input.GetKeyDown(KeyCode.Space)| Input.GetKeyDown(KeyCode.UpArrow)| Input.GetKeyDown(KeyCode.Z)| Input.GetKeyDown(KeyCode.W))
{
如果(接地)
{
GetComponent().velocity=newvector2(GetComponent().velocity.x,jump);
isfounded=false;
}
}
移动速度=0;
//左右运动
if(Input.GetKey(KeyCode.LeftArrow)| | Input.GetKey(KeyCode.A))
{
移动速度=-速度;
}
if(Input.GetKey(KeyCode.RightArrow)| | Input.GetKey(KeyCode.D))
{
移动速度=速度;
}
GetComponent().velocity=newvector2(moveVelocity,GetComponent().velocity.y);
}
//检查是否接地
void OnTiggerEnter2D()
{
isGrounded=true;
}
}

有两种方法可以实现这一点,但遵循您当前的实现方式:为什么不在跳转时将isground标志设置为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;
    }
}
公共类PlayerController:MonoBehavior
{
//运动
公众浮标速度;
公众浮标跳跃;
浮动速度;
//接地变压器
bool isGrounded=true;
无效更新()
{
//跳跃
if(Input.GetKeyDown(KeyCode.Space)| Input.GetKeyDown(KeyCode.UpArrow)| Input.GetKeyDown(KeyCode.Z)| Input.GetKeyDown(KeyCode.W))
{
如果(接地)
{
GetComponent().velocity=newvector2(GetComponent().velocity.x,jump);
isfounded=false;
}
}
移动速度=0;
//左右运动
if(Input.GetKey(KeyCode.LeftArrow)| | Input.GetKey(KeyCode.A))
{
移动速度=-速度;
}
if(Input.GetKey(KeyCode.RightArrow)| | Input.GetKey(KeyCode.D))
{
移动速度=速度;
}
GetComponent().velocity=newvector2(moveVelocity,GetComponent().velocity.y);
}
//检查是否接地
void OnTiggerEnter2D()
{
isGrounded=true;
}
}

我很确定你需要在GetKeyDown()中使用“”,但我不确定我在这方面很新,我的用户名是这么说的。

我很确定你需要在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);

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级运动:单一行为
{
//运动
公众浮标速度;
公众浮标跳跃;
浮动速度;
公共刚体2d rb;
无效更新()
{
//停飞?
如果(径向位置y<5.52)
{
//跳跃
if(Input.GetKeyDown(KeyCode.Space)| Input.GetKeyDown(KeyCode.UpArrow)| Input.GetKeyDown(KeyCode.Z)| Input.GetKeyDown(KeyCode.W))
{
GetComponent().velocity=新矢量2(GetComponent().velocity.x,跳跃);
}
}
移动速度=0;
//左右运动
if(Input.GetKey(KeyCode.LeftArrow)| | Input.GetKey(KeyCode.A))
{
移动速度=-速度;
}
if(Input.GetKey(KeyCode.RightArrow)| | Input.GetKey(KeyCode.D))
{
移动速度=速度;
}
GetComponent().velocity=新矢量2(moveVelocity,GetComponent().velocity.y);
}
}

我是编程新手,所以我把这当作一个挑战。我刚刚注意到这是五年前的事了,但我还是会把我的解决方案寄出去。我还是不知道你的剧本是怎么写的。哈哈

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

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
普布利