我对C#很新。我在Unity中创建了一些内容来帮助我更好地学习C#和Unity。C#和Unity3D按键被按下时
我想知道为什么:
Input.GetKeyDown(KeyCode.UpArrow))
只有触发一次时,放置在:
void Update()
由于更新是一个循环,为什么当我按住按键是它不会触发(以我的情况导致球体移动)?
我已经设法通过使用两个布尔键,当按键被释放时被改变。
这里是我的,我有玩到移动领域,并模拟加速/减速完整的脚本:
using UnityEngine;
using System.Collections;
public class sphereDriver : MonoBehaviour {
int x ;
bool upPressed = false ;
bool downPressed = false ;
void Start()
{
x = 0 ;
}
void Update()
{
if(x > 0) {
x -= 1 ;
}
if(x < 0) {
x += 1 ;
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
upPressed = true ;
}
else if(Input.GetKeyUp(KeyCode.UpArrow))
{
upPressed = false ;
}
if(upPressed == true)
{
x += 5 ;
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
downPressed = true ;
}
else if(Input.GetKeyUp(KeyCode.DownArrow))
{
downPressed = false ;
}
if(downPressed == true)
{
x -= 5 ;
}
transform.Translate(x * Time.deltaTime/10, 0, 0) ;
}
}
谢谢!由于这两个函数是Unity特有的,那么我最初做的就是以最简单的方式实现C#中我想要的功能吗? – imperium2335 2012-07-07 13:45:30
是的,这将是一个常用的方法。 – redtuna 2012-08-09 03:10:37