I would like to activate the shoulder canon of my model by rotating it around it’s local X axis 180 degrees. The model is in an idle animation state when “2” is pressed so I’ve read that the bone needs to be transformed in LateUpdate(). I’ve also read that Lerp() is necessary to achieve a smooth/natural rotation. Here is my code:
public class WarriorController : MonoBehaviour
{
private bool shooting;
private Transform gunBone;
private float _startRotateTime;
private Quaternion _startRotation;
private Quaternion _endRotaion;
void Start()
{
gunBone = GameObject.Find("gun").transform;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha2))
{
shooting = true;
_startRotateTime = Time.time;
_startRotation = gunBone.rotation;
_endRotaion = Quaternion.Euler(gunBone.localEulerAngles.x + 180, gunBone.localEulerAngles.y, gunBone.localEulerAngles.z);
}
}
void LateUpdate()
{
if (shooting == true)
{
float timeSinceStarted = Time.time - _startRotateTime;
float percetageComplete = timeSinceStarted / 1f;
gunBone.rotation = Quaternion.Lerp(_startRotation, _endRotaion, percetageComplete);
//gunBone.localEulerAngles = new Vector3(gunBone.localEulerAngles.x, 0, 0);
}
}
}
The rotation rotates smoothly but seems to be rotating on the Z axis and not a full 180?
Here is the beginning of the rotation:
Here is the end of the rotation:
I believe I’m failing to understand how to work with Quaternions. Please advise! Thank you!