Vector3.Dot 判断方位

判断方位

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

物体空间图

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

imageJPATOV8_WLLY$V(4C35`B7X

判断方法

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

posted @ 2014-01-21 15:04  赵青青  阅读(2561)  评论(0)    收藏  举报