soundvision/UnityProject/Assets/Scenes/Examples/Complex Demos/Scripts/RotateGameObject.cs

53 lines
1.2 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;
2020-01-24 17:52:56 +00:00
[SerializeField] private float factor = 1;
[SerializeField] bool tiltAngleHorizontal;
[SerializeField] bool tiltAngleVertical;
[SerializeField] private UnityRotationEvent onRotationProcessed;
private float input_;
public void OnValueChanged(float value)
{
2020-01-24 17:52:56 +00:00
input_ = Mathf.Repeat(input_, 1080) + value * (UnityEngine.Random.Range(0, 1) * 2 -1) * factor;
}
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);
}
}