soundvision/UnityProject/Assets/Scenes/Examples/Max Silly Demos/Scripts/RotateGameObject.cs

52 lines
1.1 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
using UnityEngine.Events;
[Serializable]
public class UnityRotationEvent : UnityEvent<float>
{ }
public class RotateGameObject : MonoBehaviour
{
[SerializeField] float smooth = 5.0f;
[SerializeField] bool tiltAngleHorizontal;
[SerializeField] bool tiltAngleVertical;
[SerializeField] private UnityRotationEvent onRotationProcessed;
private float input_;
public void OnValueChanged(float value)
{
input_ = Mathf.Repeat(input_, 360) + value;
}
private void Update()
{
2020-01-21 22:31:06 +00:00
float tiltAroundY;
float tiltAroundX;
// Smoothly tilts a transform towards a target rotation.
if (tiltAngleHorizontal)
{
2020-01-21 22:31:06 +00:00
tiltAroundY = input_;
}
2020-01-21 22:31:06 +00:00
else tiltAroundY = 0;
if (tiltAngleVertical)
{
tiltAroundX = input_;
}
else tiltAroundX = 0;
2020-01-21 22:31:06 +00:00
Quaternion target = Quaternion.Euler(tiltAroundX , tiltAroundY, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
}