C# Unity GetKeyDown() 没有被正确检测到 [英] C# Unity GetKeyDown() not being detected properly

查看:48
本文介绍了C# Unity GetKeyDown() 没有被正确检测到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个小雪橇原型.我目前正在编写一些控件来补充我应用的刚体物理.我的目标是在左转或右转的相反方向施加一个力,以获得一种打滑、抓边效果,然后向前施加一个力以补偿动量的损失.

So I've got a little sledding prototype going. I'm currently coding some controls to compliment the RigidBody physics I've applied. My goal is to apply a force in the opposite direction of the turn left or right in order to get a sort of skidding, edge catching effect, and then apply a force forward to compensate for the loss of momentum.

但是我做不到那么远,因为无论出于何种原因,我都无法使 GetKeyDown() 或 GetKeyUp() 函数正常工作.我已经包含了下面的代码.基本上发生的是按钮释放".文本不断输出,然后偶尔我会得到一个按钮按下"输出,在恢复到按钮释放"之前只发生一次.输出.即使这是罕见的.通常我没有得到任何检测的迹象.

However I can't get that far, because for whatever ever reason I can't get the GetKeyDown() or GetKeyUp() functions to work. I've included the code below. Basically what happens is the "Button released." text gets output constantly, and then once in a while I will get a "Button pressed" output that happens only once before reverting back to the "Button released." outputs. Even this is rare. Usually I get no indication of any detection.

我确定我遗漏了一些很小的问题,我也曾寻找过类似的问题,但似乎没有一个能解决我的问题.如果有人能给我任何想法,我将不胜感激.此外,任何优化提示将不胜感激.我对 C# 和 Unity 还是有点陌生​​.

I'm sure I'm missing something miniscule, and I have looked for similar questions, but none of them seem to solve my problem. If anyone can give me any ideas I'd be very grateful. Also, any optimization tips would be appreciated. I'm still a little new to C# and Unity.

我正在进行调整和试验,所以如果我错过了一些不必要的评论,我深表歉意.

I'm in the middle of tweaking and experimenting so I apologize if I missed some un-necessary comments.

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

    public class PlayerController : MonoBehaviour
    {

    private Rigidbody rb;
    public bool sidePhysics;
    public bool keyPress;

    // Use this for initialization
    void Awake()
    {
        rb = GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        var x = Input.GetAxis("Horizontal") * Time.deltaTime * 50.0f;
        // No vertical controlls for now.
        //var z = Input.GetAxis("Vertical") * Time.deltaTime * 40.0f;


        // Get axis against which to apply force
        var turnAngle = rb.transform.position.x;




        transform.Rotate(0, x, 0);
        //transform.Translate(0, 0, z);


        // Debug to test input detection
        keyPress = Input.GetKeyDown(KeyCode.A);
        Debug.Log("keyPress: " + keyPress);

        // On either right or left key input down, apply force
        // Later once keys are being detected - addForce forward too 
        if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.LeftArrow)
           || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("Button pressed. ");

            // Add opposing force proportionate to the degree of the turn
            rb.AddForce((turnAngle * (-x * 10)), 0, 0, ForceMode.Acceleration);
        }
        else
        {
            Debug.Log("Button released.");
        }



        //Debug.Log("RigidBody: " + rb +
        //          "   Rotation: " + x +
        //          "   turnAngle: " + turnAngle);


    }
}

推荐答案

如果你想在按住键时连续做某事,你可以使用Input.GetKey功能.如果你想在按下键时一次做某事,你可以使用 Input.GetKeyDown 函数,因为它只会在释放并按下键之前评估为真一次再次.了解两者之间的区别很重要.

If you want to continuously do stuff when key is held down, you use the Input.GetKey function. If you want to want to do something once when key is pressed, you use the Input.GetKeyDown function since that's would evaluate to true once only until the key is released and pressed again. It's important that you understand the difference between the two.

现在,要检测按钮何时被释放,请不要在检查Input.GetKey 后使用else 语句.你不会得到你想要的效果.要检测按钮何时被释放,请使用 Input.GetKeyUp 函数.在那里添加另一个 if 语句.

Now, to detect when button is released, don't use else statement after checking the Input.GetKey. You won't get the effect you are looking for. To detect when button is released use the Input.GetKeyUp function. Add another if statement there.

更像这样:

if (Input.GetKeyDown(KeyCode.A))
{
    //KEY PRESSED
}

if (Input.GetKey(KeyCode.A))
{
    //KEY PRESSED AND HELD DOWN
}

if (Input.GetKeyUp(KeyCode.A))
{
    //KEY RELEASED
}

最后,您应该始终检查 Update 函数而不是 FixedUpdate 函数中的输入.

Finally, you should always check for input in the Update function instead of the FixedUpdate function.

这篇关于C# Unity GetKeyDown() 没有被正确检测到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆