Ray casting

While we can detect collisions between the player character's collider and a collider that fits the door object, a more appropriate method may be to check for when the player character is facing the door we are expecting to open and is within a certain distance of this door. This can be done by casting a ray forward from the player's forward direction and restricting its length. This means that when approaching the door, the player needn't walk right up to it or bump into an extended collider in order for it to be detected. It also ensures that the player cannot walk up to the door facing away from it and still open it with ray casting they must be facing the door in order to use it, which makes sense.

In common usage, ray casting ...

Get Unity Game Development Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.