[英]Written correct code in Unity C# , but not getting expected output
I have written the folowing code (given below) -我已经编写了以下代码(如下所示) -
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
void Update () {
if (Input.GetKeyDown(KeyCode.RightArrow))
transform.position += Vector3.right;
}
}
According the the code, the sprite needs to move right when right arrow is pressed.根据代码,当按下右箭头时,精灵需要向右移动。 But the sprite is not moving.但是精灵并没有移动。 So what is wrong in the code?那么代码有什么问题呢?
Note- I am using Unity version 5.4注意-我使用的是 Unity 5.4 版
According to the documentation , you can't hold the key pressed, you have to press it over and over again.根据 文档,您不能按住按键,您必须一遍又一遍地按下它。
Then, as Everts mentioned, you should always include Time.deltaTime in your calculation to avoid issues when your application is lagging for whatever reason.然后,正如 Everts 所提到的,您应该始终在计算中包含 Time.deltaTime 以避免当您的应用程序因任何原因而滞后时出现问题。
So you need two steps:所以你需要两个步骤:
transform.position += Vector3.right * Time.deltaTime * speed;
使用 Everts 的建议transform.position += Vector3.right * Time.deltaTime * speed;
and try to figure out how big speed might be.并尝试弄清楚速度可能有多大。 It might be anywhere from: 0.1, 0.01, 0.001 up to 1, 10, 100 etc, whatever suits you.它可能是任何地方:0.1、0.01、0.001 到 1、10、100 等,只要适合你。 But this formula will prevent a lot of issues in the future.但是这个公式将在未来防止很多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.