Quantcast
Channel: Answers by "ThePersister"
Viewing all articles
Browse latest Browse all 99

Answer by ThePersister

$
0
0
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! :D Yours truly, ~ThePersister :)

Viewing all articles
Browse latest Browse all 99

Trending Articles