判断方位

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

物体空间图

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

Vector3.Dot 判断方位Vector3.Dot 判断方位

判断方法

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的不同位置

Vector3.Dot 判断方位Vector3.Dot 判断方位

相关文章:

  • 2022-02-10
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2021-11-17
  • 2021-10-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2021-12-05
  • 2021-06-10
  • 2022-12-23
相关资源
相似解决方案