【问题标题】:How to rotate a GameObject more than once如何多次旋转游戏对象
【发布时间】:2016-04-27 01:10:29
【问题描述】:

尝试使用建议的选项时出现错误 vector3 right is not in its definition 线变换.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);
        }
    }
}

【问题讨论】:

  • (1) 它是transform.eulerAngles。但在你的情况下(2)你只想要.Rotate,而不是.eulerAngles

标签: c# unity3d unity5


【解决方案1】:

尝试Input.GetKey 而不是Input.GetKeyDown

Input.GetKeyDown 检测是否按下按钮,而Input.GetKey 检测是否连续按下。我想这就是你的汽缸只转动一次的原因。

【讨论】:

    【解决方案2】:

    您的代码中有很多低效之处。您不应该在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 文档中没有提到它如何返回Transform 组件。我已经读到它只是GetComponent 的包装器,尽管来自几个不同的来源。自行决定使用。

    其次,不要使用 eulerAngles 来旋转。它甚至在docs 中也这么说。

    仅使用此变量来读取角度并将其设置为绝对值。不要增加它们,因为当角度超过 360 度时它会失败。请改用 Transform.Rotate。

    看起来您甚至没有增加角度,只是将其设置为 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);
            }
        }
    }
    

    【讨论】:

    • 嘿@gjttt1 你确定 .transform 每次都使用 GetComponent 吗?我相信不是的情况。不要忘记,Transform 是 GameObject 的固有属性;它不是添加的组件。它只是一个变量, .transform 可以访问它。正如你所知道的,在“过去”Unity 有一些“方便的‘属性’”,它们愚蠢地只使用 GetComponent,它们只是编写 GetComponent 的简写。但据我所知,.transform 不是那种性质。
    • @JoeBlow 嗯,好点子。我想随着团结的发展,这些事情可能会变得更有效率。我从几个不同的来源读过几次,它只是GetComponent 的包装器,但这可能已经改变了。我会更新答案说它可能可以使用。我个人只是倾向于让我自己的参考是安全的,因为在统一文档中没有提到 transform 属性实际上是如何返回 Transform 的。
    • 你知道,我几乎可以肯定它只是字面上的变量“.transform”。 (值得记住的是,毕竟,GameObject ... ...“变形”!仅此而已。除了它之外,没有其他任何东西可以构成“游戏对象”有将它放置在场景中的四元数,并且每帧都扭曲,即“变换”。无论如何,这些天可以查看代码 - 但我太懒了:)无论如何是的,我同意它会如果这在 doco 中得到绝对澄清,那就太好了。干杯
    • Vector3.right Vector3 大写,right 小写。
    • 感谢您在错误的轴上旋转,我应该能够弄清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多