HELP - I want my player to get close when the door appears “press E” to open. and when you press “E” turn the animation of the door opening
Please in java
I want my player to get close when the door appears “press E” to open.
and when you press “E” turn the animation of the door opening
You could use a box collider with Is Trigger set: add the Box Collider component (but don’t replace the original door collider, if any), adjust its dimensions to cover the “close area” and attach a script like this to the door:
private var closed = true;
private var inTrigger = false;
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){ // remember to tag the player as "Player"
inTrigger = true;
}
}
function OnTriggerExit(other: Collider){
if (other.tag == "Player"){
inTrigger = false;
}
}
function Update(){
// if inside the trigger and the door is closed and E pressed:
if (inTrigger && closed && Input.GetKeyDown("e")){
closed = false; // the door isn't closed anymore
// play the open animation here
}
}
The “open animation” may be a standard animation, in which case you should simply play it like this:
animation.Play("Open");
or you could do the animation by code, rotating or slipping the door to the open position.
Have you made an animation of the door opening?
I think the easiest way to do this would be.
-Make a door script with a public variable to store your player
-When you push E, ray cast out of the player’s “face” and see if it hits the door within a fairly close range.
-If it does, make sure its not already open then play the animation of the door. Like animation.play(“whatever you called the door opening animation with”)
-If you want it to close just do the reverse with another animation.
Itd probably be better form to have the player script control the E pushing and the subsequent call to open the door just to keep all the input in a similar place but this is probably simpler and quicker to get off the ground
Good luck!