- Home /
Help with making his work with a key
OK, I was up half the night trying to get this to work. I figure OK first lets see if I can just get it to work without the key as I've never tried doing an open opperation of this type before.
So what I have is when you approach the gate and press the action key, in this case "Q" the gate's state will change from "Locked" to unlocked and it plays the animation with the lock openning and then placing itself ontop of the gate, I was originally just going to have the lock drop to the ground, eh, whatever works though ;)
next by pressing the action key again the gate will now swing open... Mmmmm lovely, brimming over with deliciosness actually.
Now I know there was another poor bastard like me on here having the same issue so if yur out there buddy and still looking for a fix here is my script, you might need to mod it a bit but it works for unlocking at a key input press:
var hit6 : RaycastHit;
Debug.DrawRay(transform.position, transform.forward * 6, Color.red);
if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
if ((hit6.collider.gameObject.tag == "OldFarmGate") && (Input.GetButtonUp("Action"))){
if (oldFarmGateScript.oldGateState == oldGateStates.locked) {
oldFarmGateScript.oldGateState = oldGateStates.unlocked;
oldFarmGateScript.Unlock();
} else if (oldFarmGateScript.oldGateState == oldGateStates.unlocked) {
oldFarmGateScript.Open();
}
}
}
Now to all you code brainiacs out there lol How can I get this to function after my FPS player has picked up a key?
now that should be straight forward enough!! Sorry if I seem a little "Shnarky" but lack of sleep will do that to a fella ;)
EDIT This is my Key pickup script which is attached naturally to the Key that is a pickup object just incase for some reason you need to see that:
private var gotKey : boolean = false;
var keyPickupSound : AudioClip;
function OnTriggerStay (){
if (Input.GetButtonDown("Pickup")){
gotKey = true;
Destroy(gameObject);
//Play Key Pickup Sound
if (keyPickupSound)
AudioSource.PlayClipAtPoint(keyPickupSound, transform.position);
print("You Picked Up The Gate Key");
}
}
EDIT #2 I uploaded a video clip to my YouTube Channel just incase you would like to see what I'm trying to do. As you can see when you approach the gate and are close enough, press "Q" and Unlock the Gate, Press "Q" again and the gate swings open. Now all I need is to get that to work with the key pick up and life will be peachy ;)
EDIT # 3 THE SOLUTION!!!
OK Mr. Dave below is correct. here is what I did I added a new variable at the top of my Player script:
var gotKeyScript : OldGateKeyPickUp;
And then tweaked this part of my Player script:
var hit6 : RaycastHit; Debug.DrawRay(transform.position, transform.forward * 6, Color.red); if (gotKeyScript.gotKey && Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
if ((hit6.collider.gameObject.tag == "OldFarmGate") && (Input.GetButtonUp("Action"))){
if (oldFarmGateScript.oldGateState == oldGateStates.locked) {
oldFarmGateScript.oldGateState = oldGateStates.unlocked;
oldFarmGateScript.Unlock();
} else if (oldFarmGateScript.oldGateState == oldGateStates.unlocked) {
oldFarmGateScript.Open();
}
}
}
And in the Inspector dragged my "Key Game Object" (Pickupable) onto the variable setup in the inspector...
Now I'm not sure if that is what one is supposed to do... but it works... The gate will not unlock unless you have picked up the key, then you can unlock and then open the gate. Sweeeeetness :) And then the pesants rejoyced ;)
Answer by DaveA · Apr 14, 2011 at 08:30 PM
I assume you have a 'player has key' var around somewhere, right? If it were on a 'Player' component, where player :Player
if (player.hasKey && Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
Which seems too simple to be what you're looking for, so maybe I missed the point?
Well I have a private var set up in the key pickup script. The portion of script above is in one of my Player scripts attached to the camera of my FPS player... if that helps any?
So you need to get at that 'key is picked up' datum, either by exposing it directly (public var) or make a get/set method for it (or just get set functions), or use Send$$anonymous$$essage or something when the key is picked up to inform your unlocking script of that fact.
Thankks Dave, And how do I do that, I'e been trying to find samples of how that's done all day?
Send$$anonymous$$essage comes in two flavors, http://unity3d.com/support/documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html and http://unity3d.com/support/documentation/ScriptReference/Component.Send$$anonymous$$essage.html and you can see how to set vars on other objects/scripts here: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html and about variables: http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html
And $$anonymous$$R dave gets another cookie ;) Thanks buddy, yur a life saver :D
Your answer
Follow this Question
Related Questions
How to open a gate with a key ? 1 Answer
key to open gate 1 Answer
Press an in-game switch to open a door - help? 2 Answers
Door Open With Object Pickup 1 Answer
GUI text, door and object collider 2 Answers