x


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 ;)

more ▼

asked Apr 14 '11 at 06:42 PM

Michael 12 gravatar image

Michael 12
132 85 98 106

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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?

more ▼

answered Apr 14 '11 at 08:30 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

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?

Apr 14 '11 at 08:37 PM Michael 12

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 SendMessage or something when the key is picked up to inform your unlocking script of that fact.

Apr 14 '11 at 10:15 PM DaveA

Thankks Dave, And how do I do that, I'e been trying to find samples of how that's done all day?

Apr 14 '11 at 10:42 PM Michael 12

And MR dave gets another cookie ;) Thanks buddy, yur a life saver :D

Apr 14 '11 at 11:52 PM Michael 12
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x193
x166
x94
x17

asked: Apr 14 '11 at 06:42 PM

Seen: 647 times

Last Updated: Apr 14 '11 at 11:51 PM