- Home /
Question by
ines____ · May 07, 2014 at 04:35 PM ·
javascriptdoorkey
Door key scripting problem
Ok, so I know there are loads of examples here on unity answer but i cant for the life of me make anything work. So I want to make a door open after I found the key. I know that I have to access one script from another but i cant do that This is the code from my door:
var open : boolean;
var enter : boolean;
var defaultRot : Vector3;
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);
}
if(Input.GetKeyDown("e") && enter && GetComponent(Apanharchave).hasKey){
open = !open;
}
}
icon = Resources.Load("abriraporta");
function OnGUI(){
if(enter){
GUI.Label(Rect(200, 500, 1000, 200), icon);
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
if (open){
enter = false;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
And this is the script of my key
var hasKey :boolean= false;
var icon : Texture2D;
var enter : boolean;
function Update ()
{
if(Input.GetKeyDown("e") && enter)
{
Destroy (gameObject);
hasKey=true;
}
}
icon = Resources.Load("apanharobjeto");
function OnGUI()
{
if(enter)
{
GUI.Label(Rect(200, 500, 1000, 200), icon);
}
}
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Player")
{
enter = true;
}
}
function OnTriggerExit (other : Collider)
{
if (other.gameObject.tag == "Player")
{
enter = false;
}
}
I have the tag Player on the player, the tag Door on my door and the tag Key on my key. Thanks in advance!
Comment