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
2
Question by Michael 12 · Apr 13, 2011 at 08:16 PM · doorpickupkeyunlock

Unlocking Gate with Key

OK Got something solid to work with but not sure about getting the functionality of it working. I have a key hanging on a wall with a box collider and this script attached:

EDIT I updated my Key pickup script so now it works when you press "e".

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

}

I THINK and underline that think it's picking it up although I'm very fuzzy on how the whole pickup stuff works. I'm pretty sure I'm doing something wrong as it's supposed to pick up the key when you press "e" but instead it seems to do it when you get very close to it... so I'm not sure if it's actually working right or not.

OK so that's problem 1.

Problem 2

Here is the script attached to my Gate. I'm not sure how to get it so it recognizes that the FPS player has picked up the key and then how to get that to play the "Gate Unlock" sequence.?

enum oldGateStates {open, locked, unlocked}; var oldGateState : oldGateStates; var oldGateUnlockSound : AudioClip; var oldGateSoundOpen : AudioClip;

function Awake() { oldGateState = oldGateStates.locked; }

function Update () { }

function Unlock() { animation.Play("GateUnlock"); oldGateState = oldGateStates.unlocked; audio.PlayOneShot (oldGateUnlockSound);

}

function Open() { animation.Play("GateOpen"); oldGateState = oldGateStates.open; audio.PlayOneShot (oldGateSoundOpen);

}

Edit I forgot to add the part in my Player script that should open the gate only if my player has the key:

var hit6 : RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
        if ((hit6.collider.gameObject.tag == "OldFarmGate") && (oldFarmGateScript.oldGateState == oldGateStates.locked) ) {
            oldFarmGateScript.Open();
        }
    }

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
2
Best Answer

Answer by xCRKx TyPHooN · Apr 13, 2011 at 09:15 PM

Problem 1) In this line of code...

if (Input.GetButtonDown("Pickup"))
    gotKey = true;
    Destroy(gameObject);

You are missing the brackets on your if check. As such, the input is still checking for a button pressed, but the Destroy(gameObject) is getting called regardless of the checks because it is outside the default brackets. Thats why the key is disappearing when you run into the trigger. You actually haven't picked it up until you press 'E'.

    if (Input.GetButtonDown("Pickup")) {
         gotKey = true;
         Destroy(gameObject);
    }

That should fix that...

Problem 2)

var hit6 : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
    if ((hit6.collider.gameObject.tag == "OldFarmGate") && (oldFarmGateScript.oldGateState == oldGateStates.locked) ) {
        oldFarmGateScript.Open();
    }
}

If this code is in the same script and "gotKey" is a global variable, just add another check.

var hit6 : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
    if ((hit6.collider.gameObject.tag == "OldFarmGate") && (oldFarmGateScript.oldGateState == oldGateStates.locked) )
        if(gotKey){
        oldFarmGateScript.Open();
    }
}

If "gotKey" is in another script you are going to have to send or receive a message from that script in order to get the variable. There are lots of different methods for handling this, so look into it a little.

Comment
Add comment · Show 2 · 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 13, 2011 at 10:47 PM 0
Share

Yes Thanx I had a slight suspicion that it might have been something like that. Now how does my gate know that I have this key?

avatar image Michael 12 · Apr 13, 2011 at 11:41 PM 0
Share

$$anonymous$$y "got$$anonymous$$ey" is in my "OldGate$$anonymous$$eyPickUp" script, how do I broadcast from one to the other?

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

Problems with key script 0 Answers

How to open a gate with a key ? 1 Answer

Door Open With Object Pickup 1 Answer

Help with making his work with a key 1 Answer

Rigid body Door requiring a key? 0 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