问题描述
我有一个 button 和一个 PlayerObject.当我单击按钮时,对象必须连续旋转开发学习,当我再次单击同一个按钮时unity 点击物体拖动,对象必须停止旋转.目前,我正在使用下面给出的代码.它使物体只旋转一次到一定的角度.
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); } } }
请帮忙.提前致谢.
推荐答案
您需要的是一个非常简单的切换.您的旋转之所以如此笨拙,是因为它仅在调用 CubeRotate() 时运行旋转命令unity 点击物体拖动开发学习,因此不会像您计划的那样 连续 旋转.而是将旋转命令移到 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); } } }