First is the mathematical knowledge that is greatly awakened by the rain Pine, code reference from here:
Using unityengine;using System.collections;public class Testrot:monobehaviour {public Transform target; void Start () { //four rotation some operations quaternion rotation = target.rotation * Quaternion.euler (0f, 30f, 0f);// The current angle rotates 30 degrees around the y-axis Vector3 pos = rotation * New Vector3 (10f, 0f, 0f);//Current Angle forward 10 units along the x-axis debug.drawline (POS, Vector3.zero, color.red); Debug.Log ("Newpos" + pos + "Nowpos" + target.position + "Distance" + vector3.distance (POS, target.position)); The vector rotation //current vector rotates 45 degrees around the y-axis, or it can be thought that the angle is rotated 45 degrees around the y-axis, moving 1 units along the z-axis Vector3 a = Quaternion.euler (0f, 45f, 0f) * vector3.forward;//only rotates, the length of the vector is constant Debug.Log (a);} }
The original text is a method that determines whether an object is in a triangular area facing the character, but is more efficient, more accurate, and simpler:
Using unityengine;using System.collections;public class Testrot2:monobehaviour {public Transform target; public float angle = 60f; public float distance = 5f; void Update () { Vector3 direction = target.position-transform.position; if (Vector3.angle (direction, Transform.forward) < Angle) if (Vector3.distance (Target.position, Transform.position) < distance) print ("I've locked you up!" "); }}
[Unity Combat] to determine if the character faces a certain area of the existence of objects