- Home /
Detect if player is facing an Object
i'm making a game where my player will change position if he is facing an object (90°) any help to make that detection ?
An alternate to @DajBuzi's solution:
var v = object.position - transform.position;
if (Vector3.Angle(v, transform.forward) <= 45.0) {
Debug.Log("I'm facing the object");
}
This is 45 degrees either size for a total of 90. If you want a full 180 degrees, you can use Vector3.Dot() and look for a positive result. Also if the pivot points are different heights, you might want to zero out the 'y' value before doing the comparison. Note sure of your goal.
Answer by DajBuzi · Aug 11, 2014 at 03:48 PM
Hello,
I would recomment you to use Raycast:
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100)){
//check if it hits desired object
// and do some logic
}
Attach this to your player and check the results.
transform.position is your player position transform.forward is your player forward direction
Regards, M.Rogalski
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Need help with screen resolution and character size 1 Answer
Glitching between box colliders. One is kinematic... Help! 1 Answer
A Question about sprites 3 Answers