Check if player is facing an Object with Tag and set distance away
I have a player who approaches the automatic doors in my game (think Star Trek) and I want to check he's both facing the door (with the door using a "Door" tag so I can put a few variations in the same category) and that he's near enough for it to open. This way he can't walk through it backwards for example, and doors won't open if he's too far away from them.
I also don't want to use a raycast because there's a collider box around the door, which will apparently stop raycasts working if they do a check within an object's collider box, but when the player is inside the collider box is when I actually want the door to open.
Any suggestions for a really simple and easily-understandable way to do this?
And please make all the steps really clear without skipping anything obvious, because I'm very bad at figuring other steps you might leave out because you just assume they are a given but that I will need to do to get any of the code to work.
Thanks :)
Answer by Pineapple37 · Aug 10, 2018 at 04:10 AM
You can use Layer masks to cast rays that will only hit certain objects.
If you move the collision box onto another object or onto a child of the door, you should be able to put just the collider on the Ignore Raycast layer.
From there if you use the Physics.IgnoreRaycastLayer you should be able to cast rays without any issues.
I'm not quite sure how I would actually do what you mean in practice.
If I have the following code:
void OnTriggerEnter(Collider other)
{
if (other.tag == "Door")
{
RaycastHit hit;
if (Physics.Raycast(playerTransform.position, playerTransform.forward, out hit))
{
if (hit.collider.gameObject.CompareTag("Door"))
{
Debug.Log("Door is in view"); // Just using this to see it works
}
}
}
}
What do I specifically have to do to set up and use what you are saying?
Note: This script is actually attached to the DoorTrigger child of the main door that has the trigger I'm checking.
I figured out how to get what I wanted from your answer, so thanks for that. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612172535im_/https://answers.unity.com/themes/thub/images/avi.jpg)