最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unity

旗下网站admin51浏览0评论

Unity

Unity

Unity - 检测键盘输入(Unity - Detecting keyboard input)

我是Unity的新手,并且一直在线查找大量的教程/指南。 我的问题是,由于某些原因,当我使用下面的代码时,它不会检测键盘是否被点击。 也许我做错了键盘检测。 这是我的代码:

using UnityEngine;using System.Collections;public class Player : MonoBehaviour { Vector3 player = GameObject.FindGameObjectWithTag("Player").transform.position; void Update () { if (Input.GetKeyDown(KeyCode.D)) { player.x += 0.01F; } }}

I'm fairly new to Unity and have been looking up tons of tutorials/guides online. My issue is that for some reason when I use the below code it doesn't detect if the keyboard is clicked. Maybe I am doing keyboard detection wrong. Here is my code:

using UnityEngine;using System.Collections;public class Player : MonoBehaviour { Vector3 player = GameObject.FindGameObjectWithTag("Player").transform.position; void Update () { if (Input.GetKeyDown(KeyCode.D)) { player.x += 0.01F; } }} 最满意答案

您的输入代码是正确的,但仍然是一些不正确的事情。 首先,你在任何函数之外写了一个初始化器(静态方法)。 请记住,当您在Unity3d C#中执行此操作时,它始终会给您一个警告/错误。

如果您使用的是C#,请不要在构造函数或字段初始值设定项中使用此函数,而是将初始化移至Awake或Start函数。

所以首先在任一函数中移动那种行。

第二件事你得到Vector3并尝试使用它作为参考,这意味着你得到了Vector3形式的位置引用,并且该变量中的每个更改都将有效,但事实并非如此,它不会。

但是,你可以通过获取Transform或GameObject ,他们会为你做到这一点。

第三个也是最后一个,你试图直接改变Vector3组件(在你的情况下为“x”),这对Unity来说也是不可接受的。 你可以做的是用new Vector3分配位置或创建一个单独的Vector3变量,改变它,然后将其分配到位置。

所以在所有这些地址之后你的代码应该是这样的,

using UnityEngine;using System.Collections;public class NewBehaviourScript : MonoBehaviour{ Transform player; // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag ("Player").transform; } // Update is called once per frame void Update () { if (Input.GetKeyDown (KeyCode.D)) { // Remove one of these two implementations of changing position // Either Vector3 newPosition = player.position; newPosition.x += 0.01f; player.position = newPosition; //Or player.position = new Vector3 (player.position.x + 0.01f, player.position.y, player.position.z); } }}

Your input code is correct but still couple of things that are not at right place. First you wrote an initializer (static method) outside any function. Remember when you do it in Unity3d C# then it will always give you a warning/error.

If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.

So first move that sort of lines in either functions.

Second thing you are getting Vector3 and trying to use it as reference, that means you got a position reference in form of Vector3 and every change made in that variable will be effective, that is not the case, it won't.

But yes you can do it by getting Transform or GameObject, they will do it for you.

Third and last thing, you are trying to alter Vector3 component ("x" in your case ) directly, that'd also not acceptable for Unity. What you can do is either assign position with new Vector3 or create a separate Vector3 variable, alter that, then assign it to position.

So after all of these addresses your code should be look like this,

using UnityEngine;using System.Collections;public class NewBehaviourScript : MonoBehaviour{ Transform player; // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag ("Player").transform; } // Update is called once per frame void Update () { if (Input.GetKeyDown (KeyCode.D)) { // Remove one of these two implementations of changing position // Either Vector3 newPosition = player.position; newPosition.x += 0.01f; player.position = newPosition; //Or player.position = new Vector3 (player.position.x + 0.01f, player.position.y, player.position.z); } }}

发布评论

评论列表(0)

  1. 暂无评论