【问题标题】:Rotate object continuously on button click Unity3D在按钮单击 Unity3D 时连续旋转对象
【发布时间】:2015-06-26 00:40:37
【问题描述】:

我有一个button 和一个PlayerObject。当我单击按钮时,对象必须连续旋转,当我再次单击同一个按钮时,对象必须停止旋转。目前,我正在使用下面给出的代码。它使物体只旋转一次到一定的角度。

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
    int a=1;
    public  void CubeRotate () {
        a++;
        transform.Rotate (new Vector3 (150, 300, 60) * Time.deltaTime);

        if (a%2==0) {
                    Debug.Log(a);
                        transform.Rotate (new Vector3 (150, 300, 60) * Time.deltaTime);

            }
    }
}

请帮忙。提前致谢。

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    您需要的是一个非常简单的切换。您的旋转之所以如此笨拙,是因为它仅在调用 CubeRotate() 时运行旋转命令,因此不会像您计划的那样连续旋转。而是将旋转命令移到 Update() 方法中,该方法在每一帧上运行。

    using UnityEngine;
    using System.Collections;
    
    public class PlayerController : MonoBehaviour {
    
        protected bool rotate = false;
    
        public void CubeRotate () {
            rotate = !rotate;
        }
    
        public void Update() {
            if(rotate)
            {
                transform.Rotate (new Vector3 (150, 300, 60) * Time.deltaTime);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 2018-05-09
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多