hmm looks like the door script I’ve written last year …
this should work (untested):
private var doorIsOpen = false;
function Update(){
print(doorIsOpen);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 2))
{
if(hit.collider.gameObject.tag == "door")
{
if (Input.GetButtonDown("Fire1") )
{
door.animation.Play("open");
yield WaitForSeconds(10); // change this value as you want or use a var instead
doorIsOpen = true;
}
if (Input.GetButtonUp("Fire1") doorIsOpen == false)
{
door.animation.Play("close");
doorIsOpen = false;
}
}
}
}
w/a/s/d = character controller
space = jump
e = action (to open and close door)
I can open the door with “e” button but when the door open it will play both open and close animation.there is no pause between open and close door animation.
this is my script:
private var doorIsOpen = false;
var hit : RaycastHit;
function Update(){
print(doorIsOpen);
if(Physics.Raycast(transform.position, transform.forward, hit, 2))
{
if(hit.collider.gameObject.tag == "pintu" doorIsOpen == false);
var door = gameObject.FindWithTag("pintu");
if (Input.GetKey ("e"))
{
door.animation.Play("open");
doorIsOpen = false;
print(doorIsOpen);
}
else if(Input.GetKey("e") doorIsOpen == false)
{
door.animation.Play("close");
doorIsOpen = true;
print(doorIsOpen);
}
}
}
Why don’t you try to add a yield (of a few seconds) after launching your opening animation (as someone showed it in a previous example) ? This will permit to the door to stay opened during a moment before the next update close it.
This script helps you make a sliding door without animations.
You make a trigger and a door mesh as a child of the trigger and attach this script to the trigger. When you approach to the door, it will slide open and when you step away, it will slide close.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Tamer/Prefabs/DoorSlide1")]
public class DoorSlide1 : MonoBehaviour {
public float speed = 1.0F;
public float slideDistance = 1.0F;
private Vector3 slideBegin, slideEnd;
private Transform door;
private bool canMove = false;
// Use this for initialization
void Awake () {
door = transform.GetChild (0);
slideBegin = door.localPosition;
slideEnd = slideBegin;
slideEnd.x = slideBegin.x + slideDistance;
}
// Update is called once per frame
void Update () {
if (canMove)
DoorSlide (slideEnd);
else
DoorSlide (slideBegin);
}
void OnTriggerEnter (Collider other) {
if (other.tag == "Player") canMove = true;
}
void OnTriggerExit (Collider other) {
if (other.tag == "Player") canMove = false;
}
void DoorSlide (Vector3 targetPos) {
door.localPosition = Vector3.Lerp(door.localPosition, targetPos, speed * Time.deltaTime);
}
}
What you want is a boolean (true/false) value (let’s call it isDoorOpen) to determine whether the door is open or not. If isDoorOpen is true, then the door is open (obviously).
The value should start out as false (the door is NOT open). When the character approaches the door and the player hits the E button, then you check to see whether the door is open or not. If the door is open (isDoorOpen == true) then run the code to close the door. If the door is closed (isDoorOpen == false) then open the door.
Pseudo-code might look like this:
var isDoorOpen = false;
function activateDoor(){
if(isDoorOpen){
closeDoor();
isDoorOpen = false;
} else {
openDoor();
isDoorOpen = true;
}
}
hello guys.i have 2 elevator doors and they open slide. and i have box collider front of door. when FPS enter to collider i want that 2 doors open slide to near. and exit to collider close doors. can somebody send simple code? and explain how works.