Quantcast
Channel: Answers by "gfoot"
Viewing all articles
Browse latest Browse all 95

Answer by gfoot

$
0
0
The simplest way to do this is to take the cube's forward and up vectors, align them to the nearest world axes, and then recalculate the cube orientation using Quaternion.LookRotation. To snap a vector along the nearest world axis, find out which of its components (x, y, or z) has the greatest magnitude (positive or negative), and zero out the other two. You don't need to normalize the result, LookRotation will handle it fine. Something like this ought to work: private static Vector3 NearestWorldAxis(Vector3 v) { if (Mathf.Abs(v.x) < Mathf.Abs(v.y)) { v.x = 0; if (Mathf.Abs(v.y) < Mathf.Abs(v.z)) v.y = 0; else v.z = 0; } else { v.y = 0; if (Mathf.Abs(v.x) < Mathf.Abs(v.z)) v.x = 0; else v.z = 0; } return v; } ... Vector3 alignedForward = NearestWorldAxis(transform.forward); Vector3 alignedUp = NearestWorldAxis(transform.up); transform.rotation = Quaternion.LookRotation(alignedForward, alignedUp);

Viewing all articles
Browse latest Browse all 95

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>