C# 我的getkeydown未被识别

C# 我的getkeydown未被识别,c#,unity3d,C#,Unity3d,嘿,这是一个带有动画参数的简单脚本,但我不知道为什么我的if with getkeydown没有执行!它假设播放一个空闲的动画,如果按键没有被按下,但这并没有发生,相反,它只是没有通过这部分代码 public class PlayerMovement : MonoBehaviour { private Rigidbody2D rigidbodyMurrilo; public float speed; public Animator anim; public bool FacingRigth;

嘿,这是一个带有动画参数的简单脚本,但我不知道为什么我的if with getkeydown没有执行!它假设播放一个空闲的动画,如果按键没有被按下,但这并没有发生,相反,它只是没有通过这部分代码

public class PlayerMovement : MonoBehaviour {

private Rigidbody2D rigidbodyMurrilo;
public float speed;
public Animator anim;
public bool FacingRigth;
public Transform transform;

private void Start()
{
    rigidbodyMurrilo = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();

    FacingRigth = true;

}

void FixedUpdate()
{
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");

    HandleMovement(horizontal);

    Flip(horizontal);




}

void HandleMovement(float horizontal)

{
    rigidbodyMurrilo.velocity = new Vector2(horizontal * speed, rigidbodyMurrilo.velocity.y);

}


private void Update()
{
    if (Input.GetKeyDown("d") || Input.GetKeyDown("left") || Input.GetKeyDown("a") || Input.GetKeyDown("rigth"))
    {
        anim.Play("MoveRigth");
    }

    Debug.Log(Input.GetKey("d"));

    if (Input.GetKeyUp("d") || Input.GetKeyUp("left") || Input.GetKeyUp("a") || Input.GetKeyUp("rigth"))
    {
        anim.Play("Idle");
    }
}

private void Flip (float horizontal){

    if(horizontal > 0 && !FacingRigth || horizontal<0 && FacingRigth)
    {

        Vector3 theScale = transform.localScale;

        theScale.x *= -1;

        transform.localScale = theScale;

        FacingRigth = !FacingRigth;

    }

}
公共类玩家移动:单一行为{
私人Rigidbody2D rigidbodyMurrilo;
公众浮标速度;
公共动画;
公共厕所设施;
公共转型;
私有void Start()
{
rigidbodyMurrilo=GetComponent();
anim=GetComponent();
FacingRigth=正确;
}
void FixedUpdate()
{
float horizontal=Input.GetAxis(“水平”);
float vertical=Input.GetAxis(“垂直”);
手部运动(水平);
翻转(水平);
}
无效手柄移动(水平浮动)
{
rigidbodyMurrilo.velocity=新矢量2(水平*速度,rigidbodyMurrilo.velocity.y);
}
私有void更新()
{
if(Input.GetKeyDown(“d”)| Input.GetKeyDown(“左”)| Input.GetKeyDown(“a”)| Input.GetKeyDown(“右”))
{
动漫游戏(“MoveRigth”);
}
Log(Input.GetKey(“d”);
if(Input.GetKeyUp(“d”)| Input.GetKeyUp(“左”)| Input.GetKeyUp(“a”)| Input.GetKeyUp(“右”))
{
动漫游戏(“空闲”);
}
}
专用空心翻转(水平浮动){

如果(horizontal>0&!FacingRigth | | | horizontal,原因是
Input.GetKeyUp()
方法的工作方式与您认为的不同:)。
Input.GetKeyUp()
的工作方式如下:

在用户释放键的帧期间,该方法返回true 通过名字识别

我建议您重新编写if语句,如下所示:

    if (Input.GetKeyDown("d") || Input.GetKeyDown("left") || Input.GetKeyDown("a") || Input.GetKeyDown("rigth"))
    {
        anim.Play("MoveRigth");
    } 
    else 
    {
        anim.Play("Idle");
    }
或者第二次使用相同的语句,前面有
。我希望这有帮助:)

@PedroGouveia Nice:)继续学习和编码,我会很高兴看到最终结果:)