// find the angle and the front, back, left and right directions public void checkTargetDirForMe(Transform target) { //xuqiTest: target.position = new Vector3(3, 0, 5); Vector3 dir = target.position - transform.position; //Position difference, direction //method 1 dot multiplication //The calculation method of the dot product is: a · b =| a | · | b | cos < a,b > where | a | and | b | represent the modulus of the vector. float dot = Vector3.Dot(transform.forward, dir.normalized);//The dot product is judged before and after//dot >0 is in the front <0 is in the back float dot1 = Vector3.Dot(transform.right, dir.normalized);//Dot multiplication to judge left and right//dot1>0 is on the right<0 is on the left float angle = Mathf.Acos(Vector3.Dot(transform.forward.normalized, dir.normalized)) * Mathf.Rad2Deg;//Calculate the angle by dot product //method 2 cross product //The cross product satisfies the right-hand criterion formula: modulo length |c|=|a||b|sin<a,b> Vector3 cross = Vector3.Cross(transform.forward, dir.normalized);////Dot multiplication to judge left and right// cross.y>0 is on the left<0 is on the right Vector3 cross1 = Vector3.Cross(transform.right, dir.normalized);////The dot product is judged before and after// cross.y>0 is in the front<0 is in the back angle = Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(transform.forward.normalized, dir.normalized))) * Mathf.Rad2Deg; }
20161202 15:13