- Home /
How Do I make my door always open when I press the key?
I created a javascript to make a open-able door. It works, well, usually. Sometimes when i press the key to open the door, it opens a little, then closes again. Could someone help me? Here is the code:
pragma strict
var Smooth : float = 1.0;
var OpenAngle : float = 90.0;
var CloseAngle : float = 0.0;
var OpenDoor : boolean = false;
var EnterTrigger : boolean = false;
var DoorIsOpen : boolean = false;
var DoorOpenSound : AudioClip;
function OnTriggerEnter (Col: Collider){
if(Col.gameObject.tag == "Player"){
EnterTrigger = true;
}
}
function OnTriggerExit (Col: Collider){
if(Col.gameObject.tag == "Player"){
EnterTrigger = false;
}
}
function OpenOrCloseDoor(){
if(DoorIsOpen == false){
OpenDoor = true;
audio.PlayOneShot(DoorOpenSound);
}
else if(DoorIsOpen == true){
OpenDoor = false;
audio.PlayOneShot(DoorOpenSound);
}
}
//Every Frame Do
function Update () {
//Open Door Part Starts
if (OpenDoor == true) {
var DoorOpen = Quaternion.Euler(0, OpenAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, DoorOpen, Time.deltaTime * Smooth);
DoorIsOpen = true;
}
if (OpenDoor == false) {
var DoorClosed = Quaternion.Euler(0, CloseAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, DoorClosed, Time.deltaTime * Smooth);
DoorIsOpen = false;
}
//Open Door part Ends, trigger part Starts.
if( EnterTrigger == true){
if(Input.GetKey(KeyCode.E)){
OpenOrCloseDoor();
}
}
}
//End of Update
Thanks a lot.
Assu$$anonymous$$g DoorIsOpen = false and EnterTrigger = true. Then press 'E' Cal OpenOrCloseDoor()... So OpenDoor = true then when it reaches the next frame in the update function DoorIsOpen = false so the door is open for 1 frame. If you did it procedural it would be easier for you ins$$anonymous$$d of combine 2 functionalities into 1. So, have one that focuses on when the door should open then another function focusing on when the door should close.
Answer by aldonaletto · Feb 10, 2013 at 05:11 AM
You should not use Input.GetKey: this toggles DoorIsOpen every frame while the key is pressed, and the actual door state when the key is released is unpredictable - use Input.GetKeyDown instead. Additionally, ou should not modify DoorIsOpen when opening/closing the door - by the way, DoorIsOpen and OpenDoor are redundant: keep one or another.
The whole script could be highly simplified, like below:
var Smooth : float = 1.0;
var OpenAngle : float = 90.0;
var CloseAngle : float = 0.0;
var OpenDoor : boolean = false;
var EnterTrigger : boolean = false;
var DoorOpenSound : AudioClip;
private var curAngle: float = 0;
function OnTriggerEnter (Col: Collider){
if(Col.gameObject.tag == "Player"){
EnterTrigger = true;
}
}
function OnTriggerExit (Col: Collider){
if(Col.gameObject.tag == "Player"){
EnterTrigger = false;
}
}
function Update () {
// when E is pressed...
if (EnterTrigger && Input.GetKeyDown(KeyCode.E)){
DoorIsOpen = !DoorIsOpen; // toggle DoorIsOpen...
audio.PlayOneShot(DoorOpenSound); // and start the door sound
}
var angle: float; // define the target angle:
if (OpenDoor){
angle = OpenAngle;
} else {
angle = CloseAngle;
}
// "rotate" curAngle smoothly to the target angle
curAngle = Mathf.MoveTowards(curAngle, angle, Time.deltaTime * Smooth);
// update actual door rotation
transform.localEulerAngles = Vector3(0, curAngle, 0);
}
Thanks a lot, i am still starting to learn how to program using JavaScript and, well , I'm younger then 15. So thanks for the help. Do you $$anonymous$$d if i use the above code? thx
Answer by segahogzombie · Aug 04, 2015 at 08:21 PM
using UnityEngine; using System.Collections;
public class SmallDoorExample : MonoBehaviour {
public float closedAngle = 0;
public float openedAngle = 90;
public float doorSwingSmoothingTime = 0.5f;
public float doorSwingMaxSpeed = 90;
private float targetAngle;
private float currentAngle;
private float currentAngularVelocity;
void Update ()
{
if (DoorWasInteractedWith ())
ToggleAngle ();
UpdateAngle ();
UpdateRotation ();
}
static bool DoorWasInteractedWith ()
{
return Input.GetKeyDown (KeyCode.E);
}
void ToggleAngle ()
{
if (targetAngle == openedAngle)
targetAngle = closedAngle;
else
targetAngle = openedAngle;
}
void UpdateAngle ()
{
currentAngle = Mathf.SmoothDamp (currentAngle,
targetAngle,
ref currentAngularVelocity,
doorSwingSmoothingTime,
doorSwingMaxSpeed);
}
void UpdateRotation ()
{
transform.localRotation = Quaternion.AngleAxis (currentAngle, Vector3.up);
}
}
Your answer
Follow this Question
Related Questions
A question on distance 1 Answer
Door Open on keydown and Close Door on same keydown? 1 Answer
Door Opening And Closing 2 Answers
Canceling animation.Play functions after being used 1 Answer
Setting Scroll View Width GUILayout 1 Answer