I can’t find a tutorial that can learn me how to open and close doors when I press F(or any other key).I REALLY NEED HELP.A
I just someone to tell me or link me a tutorial on how to close and open doors.
Try the PlayMaker tutorials. You’ll need to buy a copy of PlayMaker, but it’s probably the easiest way to get started with all this stuff.
1 Like
Thank you!
![]()
Some examples for you to try
If door open/close is keyframed: (And the two animations are named “Open” and “Close”)
//the object with the animation on it
var Door: GameObject;
function OnTriggerEnter(){
Door.animation.Play("Open");
}
function OnTriggerExit(){
Door.animation.Play("Close");
}
^ as simple as that is, it can get broken if you trigger one animation before the other is finished.
Heres an example of a sliding door, or gate, that avoids that:
var gate: Transform;
var gateOpenPosition : float = 2.0;
var gateClosedPosition : float = 0.0;
var gateSpeed : float = 1.0;
var open : boolean = false;
function Start(){
collider.isTrigger=true;
renderer.enabled=false;
}
function OnTriggerEnter(other: Collider){
if(other.CompareTag("Player")||other.transform.name==("First Person Controller"))
{
open = true;
}
}
function OnTriggerExit(other: Collider){
if(other.CompareTag("Player")||other.transform.name==("First Person Controller"))
{
open = false;
}
}
function Update() {
if(open)
gate.position.x = Mathf.MoveTowards(gate.position.x, gateOpenPosition, gateSpeed * Time.deltaTime);
else
gate.position.x = Mathf.MoveTowards(gate.position.x, gateClosedPosition, gateSpeed * Time.deltaTime);
}
This final one doesnt need animations either, you have to set the open position yourself (Based on y rotation in the inspector). This is a rotation door open.
Make sure your door objects “center” or pivot point is where a hinge would naturally be. THis only opens once.
var door: Transform;
private var originalPos : float;
//change this as required
var openPos : float=-90;
private var triggered = false;
private var opening = false;
var openingSpeed: float;
function Start(){
origionalPos=door.localEulerAngles.y;
}
function Update () {
if(opening)
{
var angle : float = Mathf.LerpAngle(door.localEulerAngles.y, openPos, openingSpeed);
door.localEulerAngles = Vector3(0, angle, 0);
}
}
function OnTriggerEnter (other : Collider) {
if(other.CompareTag("Player"))
{
//Cleanup
if(triggered)
Destroy(gameObject);
opening=true;
triggered=true;
}
}