Hey Everyone,
I’ve created a script to rotate a menu around a player and it works the way I want as long as the rotation is a positive value. The moment it goes negative, it spins over and over again. I think it has to do with the LerpUnclamped() but I’m not sure. Any help is appreciated.
Thanks!
-Kaze-
Here’s my code:
using UnityEngine;
using System.Collections.Generic;
public class MenuController : MonoBehaviour
{
[SerializeField] private List<GameObject> menuObjList = new List<GameObject>();
[SerializeField] private GameObject menuObjContainer;
[SerializeField] private float rotationAmount;
[SerializeField] private float rotationSpeed;
[SerializeField] private float currentDegree;
private void Awake()
{
CalcRotationAmount();
CalcRotationSpeed();
}
private void Update()
{
CalculateDegree();
RotateMenu();
}
private void CalcRotationAmount()
{
rotationAmount = 360 / menuObjList.Count;
}
private void CalcRotationSpeed()
{
rotationSpeed = rotationSpeed * Time.deltaTime;
}
private void CalculateDegree()
{
if(Input.GetKeyDown(KeyCode.RightArrow))
{
currentDegree -= rotationAmount;
if(currentDegree <= -360)
{
currentDegree = 0;
}
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
currentDegree += rotationAmount;
if(currentDegree >= 360)
{
currentDegree = 0;
}
}
}
private void RotateMenu()
{
Vector3 rotationDestination = new Vector3(0, 0, currentDegree);
menuObjContainer.transform.eulerAngles = Vector3.LerpUnclamped(menuObjContainer.transform.rotation.eulerAngles, rotationDestination, rotationSpeed);
}
}