简体   繁体   English

在 Unity C# 中编写正确的代码,但没有得到预期的 output

[英]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:所以你需要两个步骤:

  1. Use Everts' suggestion 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.但是这个公式将在未来防止很多问题。
  2. Use Input.GetKey instead of Input.KeyKeyDown使用Input.GetKey而不是 Input.KeyKeyDown

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在我的C#程序中未获得预期的输出,文件中的日期格式不正确 - Not getting the expected output in my c# program, Incorrect Format of Date is getting written in the file 我已经为登录检查和用户类型检查编写了C#代码。 逻辑似乎是正确的,但是为什么输出不正确? - I have written C# code for login check and user type check. Logic seems to be correct but why is the output not correct? OnTriggerEnter2D crash.gameObject.tag无法正常工作,我已经设置了组件并编写了正确的代码Unity C# - OnTriggerEnter2D collision.gameObject.tag Not Working & I've Setted Up Components & Written Correct Code Unity C# 无法使用Unity和C#编写XML文件 - XML file not getting written using Unity and C# 输出未写入文本框C# - Output not written to textbox c# 编译器拒绝在 Unity3d C# 中编译正确的代码 - Compiler is refusing to compile correct code in Unity3d C# 为什么下面的代码没有在 C# 中返回预期的 output? - Why does the below code not returning the expected output in C#? 在Unity中让代码在C#if语句中中断 - Getting code to break inside a C# if statement in Unity 通过代码获得多个职位(unity c#) - Getting more than one possition though code (unity c#) 我想将数据从访问数据库获取到 c# 中的文本文件,我编写了以下代码,但没有得到任何输出 - I want to get the data from the access database to a text file in c#, i have written the following code but i am not getting any output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM