- Home /
Making opening door requires a Key
Greetings, I'm trying to make a door that opens only if the player has the key I made the door and the key and the inventory script, and i have been facing problems with the door Script, Here is the code i made so far but it is not working ( I'm new at all this so sorry if you find my code is a big mess ) var smooth = 2.0; var DoorOpenAngle = -89.0;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
private var playerInventory : PlayerInventory;
public var requireKey : boolean;
public var doorSwishClip : AudioClip;
public var accessDeniedClip : AudioClip;
function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
//Main function
function Update (){
if(open){
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("e") && enter){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 200, 150, 30), " ");
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player")
{
if(playerInventory.hasKey)
{
enter = true;
}
else
{
audio.clip = accessDeniedClip;
audio.Play();
}
}
}
Answer by meakeel · Jul 01, 2014 at 12:15 PM
I would personally use the trigger events to tell when a player is near the door then you can just call the open or close function.
If you add Debug.Log("Some Text Here"); then while playing the game you can see the text appear in the console when an event is triggered
Can you tell what part of the code isn't working?
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
private var playerInventory : PlayerInventory;
public var requireKey : boolean;
public var doorSwishClip : AudioClip;
public var accessDeniedClip : AudioClip;
function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
//Main function
function Update (){}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 200, 150, 30), " ");
}
}
function OnTriggerEnter (other : Collider){
Debug.Log("Door Collider Triggered");
if (other.gameObject.tag == "Player")
{
if(playerInventory.hasKey)
{
open();
}
else
{
audio.clip = accessDeniedClip;
audio.Play();
}
}
}
public void open ()
{
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}
public void close ()
{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
$$anonymous$$y code is in JS, so the "public void open" and "public void close" dose not work at start
Dam sorry, I'll post a js version in the morning for you.
Should just be a case of switching "public void" for "function"
Hi Tronicx, Please find the code below, let me know how you get on with it
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
public var playerInventory : boolean;
public var require$$anonymous$$ey : boolean;
public var doorSwishClip : AudioClip;
public var accessDeniedClip : AudioClip;
public var smooth : int;
public var DoorOpenAngle: int;
public var Door_Position: Transform;
function Start () {
smooth = 2;
DoorOpenAngle = -89;
Door_Position = GameObject.FindWithTag("door1").transform;
playerInventory = true;
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
function Update () {
}
function OnTriggerStay (other : Collider){
Debug.Log("Door Collider Triggered");
if (other.gameObject.tag == "Player")
{
yield $$anonymous$$eyCheck();
}
}
function OnTriggerExit (other : Collider){
//Close the door when the player leaves the door collider if the door is open
if (other.gameObject.tag == "Player" && open)
{
close_door();
}
}
function open_door ()
{
// Open door
Door_Position.eulerAngles = openRot;
open = true;
}
function close_door ()
{
//Close door
Door_Position.eulerAngles = defaultRot;
open = false;
}
function $$anonymous$$eyCheck()
{
//Checkif the e key is pressed
while (Input.Get$$anonymous$$eyDown("e"))
{
//While the E key is pressed check for a key
if (playerInventory.has$$anonymous$$ey)
{
Debug.Log("Door Open");
open_door();
yield;
}
else
{
Debug.Log("Door Denied");
audio.clip = accessDeniedClip;
audio.Play();
}
}
}
Hey man thank you very much for you help, i just need one last thing, I'm using an Inventory System form Asset Store https://www.assetstore.unity3d.com/en/#!/content/10384
I just need to add somthing to my door Script that check if i have the key in my inventory to open the door( if i have the key only) So, what should i add to check the inventory if it has like "white_key" to open the door ?
Answer by Tronicx · Jul 03, 2014 at 03:51 AM
Thank you very much man you are help me alot, I just need one last thing, I'm using an Inventory System from the Asset Store https://www.assetstore.unity3d.com/en/#!/content/10384 And i don't know what to add to my Door Script that check the inventory if it has the key ( that i already looted) I just need to add something to check, if there's let say item called"white_key" then open the door if not don't I hope you could help me with that last thing :) Here's the key Script ( i don't know if that help )
#pragma strict
var smooth = 2.0;
var DoorOpenAngle = 90.0;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
//Main function
function Update (){
if(open){
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("f") && enter){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
And here's the Inventory Script :
//This is the central piece of the Inventory System.
var Contents : Transform[]; //The content of the Inventory
var MaxContent : int = 12; //The maximum number of items the Player can carry.
var DebugMode = false; //If this is turned on the Inventory script will output the base of what it's doing to the Console window.
private var playersInvDisplay : InventoryDisplay; //Keep track of the InventoryDisplay script.
static var itemHolderObject : Transform; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.
@script AddComponentMenu ("Inventory/Inventory")
//Handle components and assign the itemHolderObject.
function Awake ()
{
itemHolderObject = gameObject.transform;
playersInvDisplay = GetComponent(InventoryDisplay);
if (playersInvDisplay == null)
{
Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
}
}
//Add an item to the inventory.
function AddItem(Item:Transform)
{
var newContents = new Array(Contents);
newContents.Add(Item);
Contents=newContents.ToBuiltin(Transform); //Array to unity builtin array
if (DebugMode)
{
Debug.Log(Item.name+" has been added to inventroy");
}
//Tell the InventoryDisplay to update the list.
if (playersInvDisplay != null)
{
playersInvDisplay.UpdateInventoryList();
}
}
//Removed an item from the inventory (IT DOESN'T DROP IT).
function RemoveItem(Item:Transform)
{
var newContents=new Array(Contents);
var index=0;
var shouldend=false;
for(var i:Transform in newContents) //Loop through the Items in the Inventory:
{
if(i == Item) //When a match is found, remove the Item.
{
newContents.RemoveAt(index);
shouldend=true;
//No need to continue running through the loop since we found our item.
}
index++;
if(shouldend) //Exit the loop
{
Contents=newContents.ToBuiltin(Transform);
if (DebugMode)
{
Debug.Log(Item.name+" has been removed from inventroy");
}
if (playersInvDisplay != null)
{
playersInvDisplay.UpdateInventoryList();
}
return;
}
}
}
//Dropping an Item from the Inventory
function DropItem(item)
{
gameObject.SendMessage ("PlayDropItemSound", SendMessageOptions.DontRequireReceiver); //Play sound
var makeDuplicate = false;
if (item.stack == 1) //Drop item
{
RemoveItem(item.transform);
}
else //Drop from stack
{
item.stack -= 1;
makeDuplicate = true;
}
item.DropMeFromThePlayer(makeDuplicate); //Calling the drop function + telling it if the object is stacked or not.
if (DebugMode)
{
Debug.Log(item.name + " has been dropped");
}
}
//This will tell you everything that is in the inventory.
function DebugInfo()
{
Debug.Log("Inventory Debug - Contents");
items=0;
for(var i:Transform in Contents){
items++;
Debug.Log(i.name);
}
Debug.Log("Inventory contains "+items+" Item(s)");
}
//Drawing an 'S' in the scene view on top of the object the Inventory is attached to stay organized.
function OnDrawGizmos ()
{
Gizmos.DrawIcon (Vector3(transform.position.x, transform.position.y + 2.3, transform.position.z), "InventoryGizmo.png", true);
}