Opening and Closing door scripting{need help}

hello again…

i need help on my game that im developing right now.

im trying to create a level that the character will have the ability to open and closing a door…

im a noob at programming so this is what ive done for my open and closing script…

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"); 
          doorIsOpen = false; 

}
      if (Input.GetButtonUp("Fire1")  doorIsOpen == false) 
       { 
          door.animation.Play("close"); 
          doorIsOpen = true; 

}
}
}
}

i can open the door but it wont stay open. when i hit “Fire1” the door will play the “open” animation and jump to the “close” animation.

and the second problem is this one:

Assets/Scripts/NewBehaviourScript.js(2,7): BCE0005: Unknown identifier: ‘doorIsOpen’.

Hey Hiz
I think you will find this tutorial helpful. You can also download the project folder, which includes the java script.http://www.learnmesilly.com/index.php?currentPage=unity_lesson2&currentPart=unity2_5

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;

}
}
}
}

thanks…now my script dont have anymore error…but im having trouble with my door animation…

i have to hold down the “fire1” button to open the door…and when i let go the “fire1” button the door will close…

when animating a door…do i have to make 2 door animation on for open one for close or 1 door with 2 animation opening and closeing…

because right now im doing 1 door with 2 animation inside it…

this is my example:
http://www.lcgdi.com/question_001/question_001.html

w/a/s/d = character controller
space = jump
e = action (to open and close door)

  1. 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.

  2. 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); 
		}
}	
}

am i doing anything that is not right…?

anyone??

i am really new to the scripting side of things, but you may want to look into Input.GetButtonDown.

Input.GetKey “Returns true while the user holds down the key identified by the key KeyCode enum parameter.” (from Scripting reference)

here is the direct link to the method: Unity - Scripting API: Input.GetButtonDown

i’ve try using “GetButtonDown” but its still the same. i dont know what to do…

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);
	}
}

as for now I can open the door with a press of a key, with pressing “e” key.

after opening the door, I would like to close the door again with a press of a key again. pressing the “e” key.

so pressing both “e” key will make the door open and close by my own will.

sorry im a bit slow with coding…still new to this coding work…

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;
    }
}

Hope that helps.

is this what you mean “Camel”.

var isDoorOpen = false;
var door = gameObject.FindWithTag("pintu");
var hit : RaycastHit;

//Declare functions
function openDoor{
	door.animation.Play("Buka");
}

function closeDoor{
	door.animation.Play("Tutup");
}

function activateDoor(){
    if(isDoorOpen){
       closeDoor();
       isDoorOpen = false;
    } else {
       openDoor();
       isDoorOpen = true;
    }
} 

if(Physics.Raycast(transform.position, transform.forward, hit, 2))
{
    if(hit.collider.gameObject.tag == "player" )
  {
    if (Input.GetKeyDown ("a"))
   {
      door.activateDoor();
    }
	}
}

The pseudocode from Camel should be fine. Try to replace the opendoor() function with your function that opens the door.

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.