Using a Quaternion's one axis to set transform.rotation
Hello guys,
This is a newbie question.
I'm calculating a smooth rotation value using Quaternion.Slerp and finall setting that to my 2D gameObject's transform.rotation.
But the problem is I want to lock the x and z rotations. I couldn't set to use the assigned quaternion's only Y axis. Can you help me with the logic ?
void CalculateRotation(Transform target, bool left)
{
Quaternion targetFinal;
if (left == true)
{
Quaternion targetRotation = Quaternion.FromToRotation(transform.up, target.right) * transform.rotation;
targetFinal = targetRotation;
}
else
{
Quaternion targetRotation = Quaternion.FromToRotation(transform.up, -target.right) * transform.rotation;
targetFinal = targetRotation;
}
targetRotationY = Quaternion.Slerp(transform.rotation, targetFinal, rotationSpeed * Time.deltaTime);
transform.rotation = targetRotationY;
}
Comment
Answer by tuncbilen · Nov 18, 2018 at 10:30 AM
Hey guys,
I fixed my problem by updating the end of the code as following. Just in case somebody else needs it.
targetRotationY = Quaternion.Slerp(transform.rotation, targetFinal, rotationSpeed * Time.deltaTime);
targetRotationY = Quaternion.Euler(new Vector3(0f, targetRotationY.eulerAngles.y, 90f));
transform.rotation = targetRotationY;