Vector3.Dot(lhs ,rhs)判断方位

假设空间中有这几个坐标,判断一个物体在另一个物体的左边还是右边,前后还是后面

imageJPATOV8_WLLY$V(4C35`B7X

假如以C为中心,判断L是在它的左边还是右边

using UnityEngine;

using System.Collections;



public class GetDirection : MonoBehaviour

{

        public Transform cubeF;

        public Transform cubeB;

        public Transform cubeL;

        public Transform cubeR;

        public Transform cubeC;

    

        void Update ()

        {

                Vector3 forward = transform.TransformDirection (Vector3.forward);

                Vector3 toOther = cubeB.position - transform.position;

                if (Vector3.Dot (forward, toOther) < 0) {

                        print ("The other transform is behind me !");

                } else {

                        print ("The other transform is ahead me !");

                }



                Vector3 left = transform.TransformDirection (Vector3.right);

                Vector3 toOtherL = cubeL.position - transform.position;

                if (Vector3.Dot (left, toOtherL) < 0) {

                        print ("The other transform is left me !");

                } else {

                        print ("The other transform is right me !");

                }

        }



}



运行拖动物体在cubeC的不同位置
imageimage

你可能感兴趣的:(vector)