- Home /
How to open a gate with a key ?
Hi every one.
I am having problems with assigning key and stuff. So the question is that how do i assign a key to be collected then approach the door and the door opens automatically. Well i already have a script (not animation, because i am having problems with animation) so how do i add the key and unlocking script. Here is my java script to open the gate.
#pragma strict
var AngleX:float = 0.0;
var AngleY:float = 90.0;
var AngleZ:float = 0.0;
private var targetValue : float = 0.0;
private var currentValue : float = 0.0;
private var easing : float = 0.05;
var Target : GameObject;
function Update(){
currentValue = currentValue + (targetValue - currentValue) *easing;
Target.transform.rotation = Quaternion.identity;
Target.transform.Rotate(0, currentValue,0);
}
function OnTriggerEnter (other : Collider){
targetValue = AngleY;
currentValue = 0;
}
function OnTriggerExit (other : Collider){
currentValue = AngleY;
targetValue = 0.0;
}
OK, this script work with a triggered collider, which means that if you approach a collider which is triggered; the door will open very simple. What i want is that a script with a key so that when i walk to the key it collect my key and the key disappear. (Sorry i am doing to much of 'key' 'key'). My person is 3rd person
Answer by capcom · Sep 09, 2012 at 07:17 PM
Why not store whether the key has been picked up or not by the player in the player's script. So create a boolean variable like `hasKey` and set it to true if the key is picked up. The door script can access the `hasKey` variable from the player, and if the value is true, then the door opens.
i don't get it please provide with codes. And what about the key script ?
I don't have time for the code (sorry), but I'll be more specific. Consider three things: your player, the key, and the door. The player has a script called playerScript attached, the door has a script called doorScript.
When the player picks up the key, a boolean variable inside playerScript called `has$$anonymous$$ey` is set to true (keep it to false as default). Now, when the player approaches the door, the doorScript is told to check the `has$$anonymous$$ey` variable in the playerScript to check if it is set to true. If it is true, then open the door. If it is false, then do nothing.
Hope that helps!
BTW, you shouldn't need a script for the key.
If I helped, please select it as the best answer. Thanks.
ok thank you soo much for giving me time ill try it and reply back :) it will work probably :).
$$anonymous$$y pleasure. If you still need help, let me know.
Your answer
Follow this Question
Related Questions
Help with making his work with a key 1 Answer
Scripting issue with opening a door with a key. 2 Answers
Open door with key 2 Answers
Activate object with key 1 Answer
[C#] Collect Key Fragments to unlock door, and Save Progress. 1 Answer