Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Michael 12 · Apr 14, 2011 at 06:42 PM · doorkeyopenunlock

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michael 12 · Apr 14, 2011 at 08:37 PM 0
Share

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?

avatar image DaveA · Apr 14, 2011 at 10:15 PM 0
Share

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.

avatar image Michael 12 · Apr 14, 2011 at 10:42 PM 0
Share

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

avatar image DaveA · Apr 14, 2011 at 11:46 PM 0
Share

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

avatar image Michael 12 · Apr 14, 2011 at 11:52 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges