如何多次旋转游戏对象
How to rotate a GameObject more than once
尝试使用建议的选项时出现错误 vector3 right 不在其定义中
行 transform.Rotate(Vector3.Right
提前致谢
using UnityEngine;
using System.Collections;
public class ForwardBack : MonoBehaviour {
// speed variable
public float speed;
public KeyCode pressLeft;
public KeyCode pressRight;
public float rotationSpeed;
// Use this for initialization
void Start () {
speed = 1f;
}
// Update is called once per frame
void Update () {
transform.Translate(0f, speed*Input.GetAxis("Vertical") * Time.deltaTime,0f);
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.Right * rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.Left * rotationSpeed * Time.deltaTime);
}
}
}
尝试 Input.GetKey
而不是 Input.GetKeyDown
Input.GetKeyDown
检测按钮是否被按下,而 Input.GetKey
检查是否连续按下。我想这就是为什么你的气缸只转动一次。
您的代码中存在很多低效之处。您不应在 Update
循环中调用 GetComponent<>()
。而是使用 Start
函数存储对它的引用,并在 Update
循环中使用它:
public class ForwardBack : MonoBehaviour
{
private Transform thisTransform = null;
void Start ()
{
// Get refrence
thisTransform = GetComponent<Transform>();
}
void Update ()
{
//Use refrence.
thisTransform.Rotate(Vector3.right * Time.deltaTime);
}
}
注意:您可能会意识到 MonoBehaviour
的每个子项都继承了 transform
属性,它将查找对 Transform
组件的引用。使用它是低效的。你应该得到你自己的参考,就像我在这里展示的那样。
编辑:如@JoeBlow 所述,transform
属性 可能 可以正常使用。 Unity 文档中没有提到它如何 returns Transform
组件。我从几个不同的来源读到它只是 GetComponent
的包装器。请自行决定使用。
其次,不要使用eulerAngles来旋转。它甚至在 docs.
中这样说
Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.
看起来你甚至没有增加角度,只是将它设置为 new Vector3(0, 0, -90)
无论如何这意味着即使你这样做它也只会设置为那个值而不是慢慢增加。
eulerAngles
的文档还会引导您找到应该使用的 Transform.Rotate。要使其正常工作,您必须将输入法更改为 Input.GetKey
而不是比 Input.GetKeyDown
但这已经在其他答案之一中提到过。然后你可以围绕你想要的轴旋转。请记住使用 Time.deltaTime
来缩放值,以便无论您的帧速率如何,它都以相同的速度旋转。您的轮换代码可能如下所示:
public class ForwardBack : MonoBehaviour
{
public float roatationSpeed;
private Transform thisTransform = null;
void Start ()
{
thisTransform = GetComponent<Transform>();
}
void Update ()
{
if(Input.GetKey(KeyCode.RightArrow)
{
thisTransform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftArrow)
{
thisTransform.Rotate(Vector3.left * rotationSpeed * Time.deltaTime);
}
}
}
尝试使用建议的选项时出现错误 vector3 right 不在其定义中 行 transform.Rotate(Vector3.Right
提前致谢
using UnityEngine;
using System.Collections;
public class ForwardBack : MonoBehaviour {
// speed variable
public float speed;
public KeyCode pressLeft;
public KeyCode pressRight;
public float rotationSpeed;
// Use this for initialization
void Start () {
speed = 1f;
}
// Update is called once per frame
void Update () {
transform.Translate(0f, speed*Input.GetAxis("Vertical") * Time.deltaTime,0f);
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.Right * rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.Left * rotationSpeed * Time.deltaTime);
}
}
}
尝试 Input.GetKey
而不是 Input.GetKeyDown
Input.GetKeyDown
检测按钮是否被按下,而 Input.GetKey
检查是否连续按下。我想这就是为什么你的气缸只转动一次。
您的代码中存在很多低效之处。您不应在 Update
循环中调用 GetComponent<>()
。而是使用 Start
函数存储对它的引用,并在 Update
循环中使用它:
public class ForwardBack : MonoBehaviour
{
private Transform thisTransform = null;
void Start ()
{
// Get refrence
thisTransform = GetComponent<Transform>();
}
void Update ()
{
//Use refrence.
thisTransform.Rotate(Vector3.right * Time.deltaTime);
}
}
注意:您可能会意识到 MonoBehaviour
的每个子项都继承了 transform
属性,它将查找对 Transform
组件的引用。使用它是低效的。你应该得到你自己的参考,就像我在这里展示的那样。
编辑:如@JoeBlow 所述,transform
属性 可能 可以正常使用。 Unity 文档中没有提到它如何 returns Transform
组件。我从几个不同的来源读到它只是 GetComponent
的包装器。请自行决定使用。
其次,不要使用eulerAngles来旋转。它甚至在 docs.
中这样说Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.
看起来你甚至没有增加角度,只是将它设置为 new Vector3(0, 0, -90)
无论如何这意味着即使你这样做它也只会设置为那个值而不是慢慢增加。
eulerAngles
的文档还会引导您找到应该使用的 Transform.Rotate。要使其正常工作,您必须将输入法更改为 Input.GetKey
而不是比 Input.GetKeyDown
但这已经在其他答案之一中提到过。然后你可以围绕你想要的轴旋转。请记住使用 Time.deltaTime
来缩放值,以便无论您的帧速率如何,它都以相同的速度旋转。您的轮换代码可能如下所示:
public class ForwardBack : MonoBehaviour
{
public float roatationSpeed;
private Transform thisTransform = null;
void Start ()
{
thisTransform = GetComponent<Transform>();
}
void Update ()
{
if(Input.GetKey(KeyCode.RightArrow)
{
thisTransform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.LeftArrow)
{
thisTransform.Rotate(Vector3.left * rotationSpeed * Time.deltaTime);
}
}
}