- Home /
Player Proximity Detection
Hello, I am new to Unity and I was wondering how I would go about doing this:
My aim is to detect when a player / character is near to a door so it will prompt the player with something like ('Press E to open the door'), then the player will be able to open the door whilst they are still in the proximity of the doors detection range otherwise when they press 'e' nothing would happen.
Pseudo Code example idea of what I think it might look like:
detection_range = 3
if players distance is <= detection_range alert the input message(e.g 'e') if input == input message('e') open door else do nothing
else do nothing
I know my question is a bit vague but I appreciate any help
Thanks,
Ryan.
Answer by gameplay4all · May 06, 2014 at 10:00 PM
var detectionRange : float;
var closeEnough : boolean;
var player : Transform;
function Update(){
closeEnough = false;
if( Vector3.Distance( player.position, transform.position) <= detectionRange ){
closeEnough = true
}
if( closeEnough && Input.GetKeyUp(KeyCode.E)){
//Open the door
}
}
function OnGUI{
if( closeEnough){
//Do Open message stuff
}
}
Haven't checked any of this code but I'm pretty sure it's error-proof :) P.S. This script should be on the doors ;)