I think I need a separate set of eyes on this to see if I’m missing something.
I have a script that when a user clicks on an object, it will animate from one angle to another angle. In the inspector, you can input the Euler angles for the start and end rotations as a Vector3, then the code converts that to a Quaternion.Euler();
For this, I have an object that I want to start at 90° on the X axis, and end at 0°. When I click on it, it rotates from 90³ to 180° instead. I’m not sure why. Can someone take a look and show me what I’m doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickRotate : MonoBehaviour
{
[Tooltip("Start rotation value for the object.")]
public Vector3 startRotation;
[Tooltip("End rotation value for the object.")]
public Vector3 endRotation;
[Tooltip("Speed to roate the object.")]
public float rotationSpeed = 2f;
private bool _onEndRotation = false;
private Quaternion startRot;
private Quaternion endRot;
private bool doRotation = false;
private float slerpPerc = 0f;
// Start is called before the first frame update
void Start()
{
startRot = Quaternion.Euler(startRotation);
endRot = Quaternion.Euler(endRotation);
this.transform.rotation = startRot;
}
// Update is called once per frame
void Update()
{
if(doRotation)
{
slerpPerc = Mathf.MoveTowards(slerpPerc, 1f, Time.deltaTime * rotationSpeed);
if (_onEndRotation)
{
transform.rotation = Quaternion.Slerp(endRot, startRot, slerpPerc);
}
else
{
transform.rotation = Quaternion.Slerp(startRot, endRot, slerpPerc);
}
if(slerpPerc >= 1f)
{
slerpPerc = 0f;
doRotation = false;
_onEndRotation = !_onEndRotation;
}
}
}
private void OnMouseUp()
{
doRotation = true;
}
}