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