如何基于滑块输入旋转GameObject?

时间:2014-04-16 12:20:04

标签: c# slider unity3d

我试图在滑块输入的帮助下旋转GameObject。我正确地创建了一个滑块。我能够改变GameObject的位置,但是当我试图用Slider旋转这个GameObject时,它不会发生。这是我的代码:

public GameObject gameObject;
private float m_currentValue = 20.0f;
void OnGUI() {
    m_currentValue  = GUI.HorizontalSlider(new Rect(35, 75, 200, 30), m_currentValue , 0.0F,  50.0F);
}

void Update(){}

如何根据滑块的值旋转GameObject?

1 个答案:

答案 0 :(得分:3)

如果您想要旋转GameObject,最好的办法是使用Transform's localEulerAngles。不要直接修改rotation。这是四元数,而不是度数的旋转。即使你知道四元数是做什么的,直接操纵它们也是非常不直观的。

知道这一点,你不需要为代码做更多的事情。只需确保它是正确的MonoBehaviour并执行以下操作:

using UnityEngine;
using System.Collections;

public class Rotator : MonoBehaviour 
{
    private float currentRotation = 20.0f;

    void OnGUI() 
    {
        currentRotation = GUI.HorizontalSlider(new Rect(35, 75, 200, 30), currentRotation , 0.0f,  50.0f);
        transform.localEulerAngles = new Vector3(0.0f, currentRotation, 0.0f);
    }

}

这与您的伪代码一致,根据滑块输入,将作为该脚本的对象围绕其Y轴旋转0到50度。