using UnityEngine;
using System.Collections;
public class TargetPoingt : MonoBehaviour
{
public Shiyepanduan shiye;
public GameObject left;
public GameObject right; //指针物体
public Transform targetTF; // 目标物体,就是判断该物体位于自身的哪个方位,被判断物体,将此脚本挂在中心物体上,以他为中心判断目标物体的位置。
void Start()
{
}
void Update()
{
checkTargetDirForMe(targetTF);
}
void checkTargetDirForMe(Transform target)
{
Vector3 dir = target.position - transform.position;
Vector3 cross = Vector3.Cross(transform.right, dir.normalized);
float dot = Vector3.Dot(transform.right, dir.normalized);
right.SetActive(true);
left.SetActive(false);
}
if (cross.y < 0)
{
left.SetActive(true);
right.SetActive(false);
}
}
}
}
using System.Collections;
public class TargetPoingt : MonoBehaviour
{
public Shiyepanduan shiye;
public GameObject left;
public GameObject right; //指针物体
public Transform targetTF; // 目标物体,就是判断该物体位于自身的哪个方位,被判断物体,将此脚本挂在中心物体上,以他为中心判断目标物体的位置。
void Start()
{
}
void Update()
{
checkTargetDirForMe(targetTF);
}
void checkTargetDirForMe(Transform target)
{
Vector3 dir = target.position - transform.position;
Vector3 cross = Vector3.Cross(transform.right, dir.normalized);
float dot = Vector3.Dot(transform.right, dir.normalized);
if (cross.y >0)
// cross.y是判断位于左侧还是右侧
//dot>0用于判断位于上下两侧。如果要判断前后两侧,只需修改 Vector3 cross = Vector3.Cross(transform.right, dir.normalized)里面的tranform.right,改成tranform.forword 或者tranform.up.
{right.SetActive(true);
left.SetActive(false);
}
if (cross.y < 0)
{
left.SetActive(true);
right.SetActive(false);
}
}
}
}