BITFALL/Assets/MagicaCloth2/Example (Can be deleted)/Common/Scripts/AutoRotate.cs

61 lines
1.4 KiB
C#
Raw Normal View History

2024-03-18 18:20:23 +08:00
// Magica Cloth 2.
// Copyright (c) 2023 MagicaSoft.
// https://magicasoft.jp
using UnityEngine;
namespace MagicaCloth2
{
public class AutoRotate : MonoBehaviour
{
public Vector3 eulers = new Vector3(0, 90, 0);
public Space space = Space.World;
2024-03-29 00:58:24 +08:00
public enum UpdateMode
{
Update,
FixedUpdate,
}
[SerializeField]
private UpdateMode updateMode = UpdateMode.Update;
2024-03-18 18:20:23 +08:00
[SerializeField]
[Range(0.1f, 5.0f)]
private float interval = 2.0f;
public bool useSin = true;
private float time = 0;
2024-03-29 00:58:24 +08:00
private void FixedUpdate()
{
if (updateMode == UpdateMode.FixedUpdate)
UpdatePosition(Time.fixedDeltaTime);
}
2024-03-18 18:20:23 +08:00
void Update()
2024-03-29 00:58:24 +08:00
{
if (updateMode == UpdateMode.Update)
UpdatePosition(Time.deltaTime);
}
void UpdatePosition(float dtime)
2024-03-18 18:20:23 +08:00
{
if (useSin)
{
2024-03-29 00:58:24 +08:00
time += dtime;
2024-03-18 18:20:23 +08:00
float ang = (time % interval) / interval * Mathf.PI * 2.0f;
var t = Mathf.Sin(ang);
if (space == Space.World)
transform.eulerAngles = eulers * t;
else
transform.localEulerAngles = eulers * t;
}
else
{
2024-03-29 00:58:24 +08:00
transform.Rotate(eulers * dtime, space);
2024-03-18 18:20:23 +08:00
}
}
}
}