unity 协程能写GetKey吗,战旗游戏GetKey移动光标速度太快了,有什么方法能让Getkey慢一点,或者将地图放大。
可以使用协程来实现GetKey,以下是示例代码:
IEnumerator MoveCursor()
{
while(true)
{
if(Input.GetKey(KeyCode.A))
{
// 光标左移
}
else if(Input.GetKey(KeyCode.D))
{
// 光标右移
}
else if(Input.GetKey(KeyCode.W))
{
// 光标上移
}
else if(Input.GetKey(KeyCode.S))
{
// 光标下移
}
yield return null; // 等待下一帧
}
}
如果想要减缓GetKey的速度,可以在协程中添加等待时间,例如:
IEnumerator MoveCursor()
{
while(true)
{
if(Input.GetKey(KeyCode.A))
{
// 光标左移
}
else if(Input.GetKey(KeyCode.D))
{
// 光标右移
}
else if(Input.GetKey(KeyCode.W))
{
// 光标上移
}
else if(Input.GetKey(KeyCode.S))
{
// 光标下移
}
yield return new WaitForSeconds(0.1f); // 等待0.1秒
}
}
如果您想要将地图放大,可以通过修改相机的尺寸来实现,例如:
Camera.main.orthographicSize = 5; // 将相机尺寸设置为5
这将使相机显示区域变大,从而将地图放大。