var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
//Main function
function Update (){
if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}
if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}
if(enter == true){
if(Input.GetKeyDown("f")){
open = !open;
}
}
}
//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;
}
}
What should i do to make key which I collect and with him I open doors, pls help
You need a script attached to the character that tracks if the character has a key…perhaps a boolean value like hasTheKey which starts out false and is true when the character has found the key. Then in your code (assuming the script is called “PlayerData”):
The way I did the locked doors in my games is the following- ( I am only 12 lol :D)
I have two Prefabs, one door that Opens and one door that is locked both in the same place, only One Active. ( e.g if I wanted the door to be locked I would Keep the locked door active and the Unlocked door Un-active. (If any one wants a post about how to make a door Open I will reply to you when I have time) Here is the Code I Made-
Attach to your Key Object-
Make Sure you have A Box collider attatched to the key Object, and check the “Is Trigger” and Make it big enough for the range
var InRange : boolean; //Determines If your Character is near
var DoorUnlocked : GameObject; // assign the Unlocked Door
var DoorLocked : GameObject; // assign The Locked Door
var ForCorrutine = false; // random Shiz ( will explain if you ask :D)
function Start(){
DoorLocked.SetActive(true);
DoorUnlocked.SetActive(false);
ForCorrutine = false;
}
function Update (){
if ( Input.GetMouseButtonDown(0)&& InRange){
GetComponent.<MeshRenderer>().enabled = false;
DoorLocked.SetActive(false);
DoorUnlocked.SetActive(true);
GetComponent.<AudioSource>().Play();
ForCorrutine = true;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == “Player”) {
InRange = true;
if(ForCorrutine == true){ yield WaitForSeconds(1);
Destroy(gameObject);
}
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == “Player”) {
InRange = false;
}
}