- Home /
Door Opening Script isn't working.
Hi there,
Admittedly, I'm an artist, not a programmer, but I had a game made with some help from a programmer previously. I took a script I used for opening a door in my project from a year ago and adapted it to my current project. However, while I am able to make the parameters work, since I'm able to get the open variable to turn false and true on trigger, I can't seem to get the door I'm applying the script on to open. Furthermore, oddly enough, there are random select times when the game will freeze for a moment and the door will just appear open when I get near it. I can't control if or when that happens. I'm very confused and would like some help. Here's the script.
using UnityEngine;
using System.Collections;
public class DoorOpenScript : MonoBehaviour {
public float smooth = 3.50f;
public float DoorOpenAngle = 90.0f;
public bool open;
//public bool opening = false;
private bool enter;
public Quaternion defaultRot;
private Quaternion openRot;
public GameObject CameraMarker;
public Vector3 CamPosition;
public bool spawnOnce = false;
public AudioClip openEffect;
//GUI Door Prompt Parameters
public int buttonW = 120;
public int buttonH = 45;
public int GUIPromptCentre = 56;
private bool unused = false;
// Use this for initialization
void Start ()
{
defaultRot = transform.rotation;
openRot = Quaternion.Euler (new Vector3 (defaultRot.eulerAngles.x,
defaultRot.eulerAngles.y + DoorOpenAngle, defaultRot.eulerAngles.z));
open = false;
}
// Update is called once per frame
void Update ()
{
if (open == true) {
//Open door
transform.rotation = Quaternion.Slerp (transform.rotation, openRot, Time.deltaTime * smooth);
open = true;
} if(open == false) {
//Close door
transform.rotation = Quaternion.Slerp (transform.rotation, defaultRot, Time.deltaTime * smooth);
}
}
void OnGUI ()
{
if (enter == true){
GUI.Button (new Rect (Screen.width/2, Screen.height/2, buttonW, buttonH),
"Test");
}
}
//Activate the Main function when player is near the door
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == "Player") {
enter = false;
}
}
}
Answer by KingFriggy · Jun 14, 2015 at 01:56 PM
The variable 'enter' is set true/false in your trigger functions. While it is the open bool that is being tested in Update(). Change open to enter in Update().
Unless you are looking for the button to be pressed to open the door, then change the OnGUI() to:
if(enter) { if(GUI.Button(#parameters#)) { open = true; } }
See, I did do that before I posted this here, but then I undoed alot because something broke. I did try it again and still no dice. I have no idea why.
Ah, okay. $$anonymous$$y bad. I tried your method again but the reason it wasn't working is because I messed with the open rotation number in the Inspector. I set it back to 90 and now it works fine. Thanks! :)
That being said, there is an issue that it twitches because it's based on the player's proximity to the door and the trigger box collider in front of the door moves with it. So, if the player stops in the middle of it opening or moves backwards, it will twitch like crazy. Any way around that?
So the collider moves as the door rotates.
$$anonymous$$ake an empty object as a child to the door and add the trigger collider to it. Attach the above script to the empty object.
$$anonymous$$ake sure to change all the transforms to transform.parent in the script, as the door is now the parent.
And to keep the collider's rotation constant stick this into the update:
transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
$$anonymous$$ake sure the empty object's rotation is zeroed out by default. Hope this helps!
Your answer
Follow this Question
Related Questions
open and close a door with a keypress 6 Answers
Door not opening and closing correctly? 2 Answers
How to make 2 doors with 2 diffrent keys 2 Answers
Why is my door opening and closing at the same time????????? 2 Answers
Opening door with the same key? 1 Answer