C#键盘输入方法(Input.GetKey()和Input.GetKeyUp())需要注意的一个问题(一个U3D初学者的总结)
2017-10-07 22:59
1241 查看
大家好。我用的是2017.1.1f1Personal版本。
最近在复习蓄力发射时(按下空格键的时间越长,游戏对象Sphere发射的越远),发现这样一个问题,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForceByTime : MonoBehaviour
{
float timer = 0f;
public GameObject sphere;
void Update ()
{
if (Input.GetKey (KeyCode.Space)) {
timer += Time.deltaTime;
if (Input.GetKeyUp (KeyCode.Space)) {
GameObject bullet = Instantiate (sphere, transform.position, transform.rotation);
bullet.GetComponent<Rigidbody> ().AddForce (Vector3.right * 500f * timer);
timer = 0f;
}
}
}
}
此处Input.GetKeyUp()嵌套在Input.GetKey()方法里面,感觉逻辑没有问题,但实际运行后,会出现这样一个问题:当空格键弹起时,很多时候Sphere并没有发射出去,一旦发射出去,又发射的特别远。代码改成如下后,问题解决:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForceByTime : MonoBehaviour
{
float timer = 0f;
public GameObject sphere;
void Update ()
{
if (Input.GetKey (KeyCode.Space)) {
timer += Time.deltaTime;
}
if (Input.GetKeyUp (KeyCode.Space)) {
GameObject bullet = Instantiate (sphere, transform.position, transform.rotation);
bullet.GetComponent<Rigidbody> ().AddForce (Vector3.right * 500f * timer);
timer = 0f;
}
}
}
此处Input.GetKeyUp()在Input.GetKey()方法外面,输出结果合理。到底是什么原因,我不是太清楚。请表哥道明真相。
总结:Input.GetKey()和Input.GetKeyUp()方法不能嵌套使用。
如果哪里不对,还请前辈指正,谢谢。。
最近在复习蓄力发射时(按下空格键的时间越长,游戏对象Sphere发射的越远),发现这样一个问题,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForceByTime : MonoBehaviour
{
float timer = 0f;
public GameObject sphere;
void Update ()
{
if (Input.GetKey (KeyCode.Space)) {
timer += Time.deltaTime;
if (Input.GetKeyUp (KeyCode.Space)) {
GameObject bullet = Instantiate (sphere, transform.position, transform.rotation);
bullet.GetComponent<Rigidbody> ().AddForce (Vector3.right * 500f * timer);
timer = 0f;
}
}
}
}
此处Input.GetKeyUp()嵌套在Input.GetKey()方法里面,感觉逻辑没有问题,但实际运行后,会出现这样一个问题:当空格键弹起时,很多时候Sphere并没有发射出去,一旦发射出去,又发射的特别远。代码改成如下后,问题解决:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForceByTime : MonoBehaviour
{
float timer = 0f;
public GameObject sphere;
void Update ()
{
if (Input.GetKey (KeyCode.Space)) {
timer += Time.deltaTime;
}
if (Input.GetKeyUp (KeyCode.Space)) {
GameObject bullet = Instantiate (sphere, transform.position, transform.rotation);
bullet.GetComponent<Rigidbody> ().AddForce (Vector3.right * 500f * timer);
timer = 0f;
}
}
}
此处Input.GetKeyUp()在Input.GetKey()方法外面,输出结果合理。到底是什么原因,我不是太清楚。请表哥道明真相。
总结:Input.GetKey()和Input.GetKeyUp()方法不能嵌套使用。
如果哪里不对,还请前辈指正,谢谢。。
相关文章推荐
- 控制器中方法需要注意的一个问题
- Sublime Text 写sass的时候,每次都需要输入一个分号的问题的解决方法
- PHP的foreach中使用引用时需要注意的一个问题和解决方法
- C# System.Type.GetMethod 方法需要注意的问题
- 总结做一个PHP项目需要注意哪些问题
- jquery parent()方法在table标签中使用需要注意的一个问题
- C#中使用Random方法时需要注意的问题
- c# - 外部对象作为参数调用方法时需要注意的问题
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- PHP的foreach中使用引用时需要注意的一个问题和解决方法
- 动态创建Table时,在IE中需要注意的一个问题
- 在编程过程中需要注意的火狐和IE兼容性问题和解决方法
- Openh323和Windows平台上关于G.723.1编码需要注意的一个问题
- JavaScript Tip/Trick: 动态创建Table时,在IE中需要注意的一个问题
- C#中关于接口的一个初学者问题
- JavaScript Tip/Trick: 动态创建Table时,在IE中需要注意的一个问题
- [C] 一个要注意的输入输出问题
- 对java中的String采用+=连接字符串需要注意的一个问题
- SWT绘图需要注意的一个问题
- 动态添加表单元素需要注意的一个问题