Here the character moves towards the point where you click, it successfully does that, but the problem is i want the character’s rotation only to be setup in 0,90,180 and 270 degrees only,so i am trying to setup its value but the player is still uses default rotation.y that is any degree(26.333 for eg).
Quaternions contain values between 0 and 1, therefore, if you set 90, 180, 270 or 0 in them directly, it’ll go all drunk on you.
Instead, use EulerAngles for setting absolute values:
// Store Transform as a variable instead of calling the hidden GetComponent :)
Transform m_Transform;
void Start()
{
// Fill m_Transform
m_Transform = transform;
}
private void SnappedLookAt(Vector3 targetPos)
{
// Simple LookAt usage, remove unwanted axici if wished for
m_Transform.LookAt(targetPos);
// Easy way to snap to a value, divide it so you get a number between 0 and 4,
// round that, then multiply by 90 resulting in 0, 90, 180, 270 or 360. :)
float snappedValue = Mathf.RoundToInt(m_Transform.rotation.eulerAngles.y / 90) * 90;
// Now set the rotation (x and z unchanged, y snapped)
m_Transform.rotation = Quaternion.Euler(
new Vector3(
m_Transform.rotation.eulerAngles.x,
snappedValue,
m_Transform.rotation.eulerAngles.z));
}
That should do the trick.
I hope that helps, if you have anymore questions, feel free to ask, if this answer was satisfying enough, please accept it and move onwards with your epic game programming journeys!