【发布时间】:2026-01-17 09:55:01
【问题描述】:
我想通过按左或右箭头键来旋转场景中的精灵(想想小行星中的宇宙飞船)。
我已将相关精灵放置在场景中并创建了一个脚本,但我不确定从那里去哪里。
我当前的脚本如下所示:
using UnityEngine;
using System.Collections;
public class RotateLeftRight : MonoBehaviour {
public float speed = 1.0f;
public string axisName = "Horizontal";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.LeftArrow)) {
// left
transform.Rotate(-1.0f, 0.0f, 0.0f); // does nothing, just a bad guess
}
if(Input.GetKeyDown(KeyCode.RightArrow)) {
// right
transform.Rotate(1.0f, 0.0f, 0.0f); // does nothing, just a bad guess
}
}
}
我只是在不知道会发生什么的情况下编写了上面的代码(而且,毫不奇怪,似乎根本没有发生任何事情)。
任何关于如何旋转精灵和控制旋转速度的建议将不胜感激。
【问题讨论】:
-
通读docs.unity3d.com/Documentation/ScriptReference/… 并确保脚本与您的精灵附加到同一个对象
-
我 100% 确定脚本与恶意有关。我尝试了您提供的链接中提到的几种不同的转换方法,但我仍然没有运气。
-
试试 Input.GetKey(KeyCode.LeftArrow) 而不是 getkeydown,这就是我所拥有的,它可以工作
-
它在这个链接中说 getkeydown 只在按下键的帧上触发,getkey 每帧触发docs.unity3d.com/Documentation/ScriptReference/Input.html
-
是的,GetKey 是一种更好的调用方法。也感谢您在这里的输入。