Door Script Problem
Hello there, I need help due to my door script problems. Opening doors with E key button works definitely fine except first try. That means that when i launch game mode, go to the collider and press E nothing opens (but audio is played). You have to double tap the button to open doors for first time, but after that everything works normal (one tap opening door). Any suggestions ? Script : {
public bool open;
public bool close;
public bool inTrigger;
public Transform doorHinge;
public AudioSource audio;
void Start ()
{
}
void OnTriggerEnter(Collider other)
{
inTrigger = true;
}
void OnTriggerExit(Collider other)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
if (close)
{
if (Input.GetKeyDown(KeyCode.E))
{
open = true;
close = false;
audio.Play ();
}
}
else
{
if (Input.GetKeyDown(KeyCode.E))
{
close = true;
open = false;
audio.Play ();
}
}
}
if (open)
{
var newRot = Quaternion.RotateTowards(doorHinge.rotation, Quaternion.Euler(0.0f, 90.0f, 0.0f), Time.deltaTime * 250);
doorHinge.rotation = newRot;
}
else
{
var newRot = Quaternion.RotateTowards(doorHinge.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 250);
doorHinge.rotation = newRot;
}
}
void OnGUI()
{
if (inTrigger)
{
if (open)
{
GUI.Box(new Rect(0, 0, 400, 50), "Press E to close");
}
else
{
GUI.Box(new Rect(0, 0, 400, 50), "Press E to open");
}
}
}
}
private void Start()
{
this.open = this.doorHinge.rotation == Quaternion.Euler(0.0f, 90.0f, 0.0f);
this.close = this.doorHinge.rotation == Quaternion.identity;
}
using UnityEngine;
public class DoorTest : $$anonymous$$onoBehaviour
{
public bool open;
public bool close;
public bool inTrigger;
public Transform doorHinge;
public AudioSource audioSource;
private readonly Quaternion openRot = Quaternion.Euler(0.0f, 90.0f, 0.0f);
private readonly Quaternion closeRot = Quaternion.Euler(0.0f, 0.0f, 0.0f);
private void Start()
{
this.open = this.doorHinge.rotation.y >= 90.0f;
this.close = this.doorHinge.rotation.y >= 0.0f;
}
private void OnTriggerEnter(Collider other)
{
this.inTrigger = true;
}
private void OnTriggerExit(Collider other)
{
this.inTrigger = false;
}
private bool Set$$anonymous$$eOnly()
{
this.open = this.close = false;
return true;
}
private void Update()
{
if (this.inTrigger)
if (this.close && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
this.open = this.Set$$anonymous$$eOnly();
this.audioSource.Play();
}
else if (this.open && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
this.close = this.Set$$anonymous$$eOnly();
this.audioSource.Play();
}
this.doorHinge.rotation = Quaternion.RotateTowards(this.doorHinge.rotation, this.open ? this.openRot : this.closeRot, Time.deltaTime * 250);
}
private void OnGUI()
{
if (!this.inTrigger) return;
GUI.Box(new Rect(0, 0, 400, 50), this.open ? "Press E to close" : "Press E to open");
}
}
Answer by Rhombuster · Mar 29, 2017 at 07:25 PM
It may have to do with the value of what open and close start at. Neither is instantiated and I can't remember if they are defaulted to true or false. Try setting one to true, and one to false before the game runs. So either at the top of the class or like this:
private void Start()
{
open = true;
close = false;
}
It depends on the state of your door at the start, so if it starts closed, set close to true and open to false.