- Home /
how i can make a gate destroy when the door open ?
Hello, I have finish My door and key script , (when i get a key i can open a door ) But i want the gate that is on the door, to Destroy when the door open And also how i can put a sound of the door when he open and close . Here is the scripts if you can help me, by edit the scripts or give me a script to do that , or tell me how to do that ... Many Thanks for your help .
The Scripts : door: using UnityEngine; using System.Collections;
public class DoorScript : MonoBehaviour {
public static bool doorKey;
public bool open;
public bool close;
public bool inTrigger;
void OnTriggerEnter(Collider other)
{
inTrigger = true;
}
void OnTriggerExit(Collider other)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
if (close)
{
if (doorKey)
{
if (Input.GetKeyDown(KeyCode.E))
{
open = true;
close = false;
}
}
}
else
{
if (Input.GetKeyDown(KeyCode.E))
{
close = true;
open = false;
}
}
}
if (open)
{
var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, -90.0f, 0.0f), Time.deltaTime * 200);
transform.rotation = newRot;
}
else
{
var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 200);
transform.rotation = newRot;
}
}
void OnGUI()
{
if (inTrigger)
{
if (open)
{
GUI.Box(new Rect(0, 0, 200, 25), "Press E to close");
}
else
{
if (doorKey)
{
GUI.Box(new Rect(0, 0, 200, 25), "Press E to open");
}
else
{
GUI.Box(new Rect(0, 0, 200, 25), "Need a key!");
}
}
}
}
}
Key: using UnityEngine; using System.Collections;
public class DoorKey : MonoBehaviour {
public bool inTrigger;
void OnTriggerEnter(Collider other)
{
inTrigger = true;
}
void OnTriggerExit(Collider other)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
if (Input.GetKeyDown(KeyCode.E))
{
DoorScript.doorKey = true;
Destroy(this.gameObject);
}
}
}
void OnGUI()
{
if (inTrigger)
{
GUI.Box(new Rect(0, 60, 200, 25), "Press E to take key");
}
}
}
Answer by hectorux · Jun 28, 2018 at 10:55 PM
With the sound you can atach a AudioSource on the door and when you press e, tell it to play, also instead of using a bool called isTrigger, you could use OnTriggerStay() and put there your code instead on the Update. To destroy the gate when is fully open, first put the max rotation in an "if" and then there tell it to destroy
Your answer
Follow this Question
Related Questions
Key script not working right! 1 Answer
looking for help with item pickups 1 Answer
Multi Switch Door 4 Answers
Scene Change OnCollision Not Working 2 Answers
Opening door with the same key? 1 Answer