Ejemplo n.º 1
0
        /// <summary>
        /// Computes the forward, right and up vectors from the original hit normal.
        /// </summary>
        /// <param name="hitNormal"></param>
        private void ComputeAllAxisFromHitNormal(Vector3 hitNormal)
        {
            bool normalAproxWorldUp = Maths.ApproxByComponents(hitNormal, Vector3.up);

            // if the hit normal matches with the world up just match the world axis
            if (normalAproxWorldUp)
            {
                up    = Vector3.up;
                right = Vector3.right;
                fwd   = Vector3.forward;

                return;
            }

            // Note: we are considering that objects will be rotated only either in the X axis or Z axis
            bool zCompGreater = Mathf.Abs(hitNormal.z) >= Mathf.Abs(hitNormal.x);

            if (zCompGreater)
            {
                up = new Vector3(0.0f, hitNormal.y, hitNormal.z).normalized;
            }
            else
            {
                up = new Vector3(hitNormal.x, hitNormal.y, 0.0f).normalized;
            }

            if (zCompGreater)
            {
                right = Vector3.Cross(Vector3.up, up).normalized *Mathf.Sign(hitNormal.z);
                fwd   = Vector3.Cross(right, up).normalized;
            }
            else
            {
                fwd   = Vector3.Cross(up, Vector3.up).normalized *Mathf.Sign(hitNormal.x);
                right = Vector3.Cross(up, fwd).normalized;
            }
        }