Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by elfinger · Oct 11, 2011 at 12:10 PM · colliderstriggerspickups

pickups used to open doors

I want my player to be able to pick up an object and use it to open a door (make a plane or box dissappear or be destroyed) when the player gets near it.

Comment
Add comment · Show 2
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 LegionIsTaken · Oct 11, 2011 at 12:18 PM 1
Share

A question usually ends with a ? I don't see one.

avatar image BerggreenDK · Oct 11, 2011 at 01:15 PM 0
Share

"pick up an object", could this be like a $$anonymous$$EY-object perhaps?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by wccrawford · Oct 11, 2011 at 12:30 PM

Then you'll need a few scripts.

When the player collides with the 'object', the script needs to set a variable (probably on the player) that says the player has the object.

When the player activates the door, the door's script needs to determine if the player has the object.

Comment
Add comment · 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
0

Answer by syclamoth · Oct 11, 2011 at 12:33 PM

 public Transform assignThisInTheEditorItGetsDestroyed;
 void OnTriggerEnter(Collider other)
 {
     Destroy(assignThisInTeEditorItGetsDestroyed);
     Destroy(gameObject);
 }


You put that script on a little trigger volume (collider with 'isTrigger' set to true), and then link it to a door object in the inspector. Then, if something enters the trigger, it'll delete the door, and then itself.

OR to do Zelda style one-use-antimatter-keys-

On your player (somewhere in your player script)

 int keys = 0;

 public void AddKey()
 {
     keys++;
 }

 public bool CanOpenDoor()
 {
     if(keys > 0)
     {
         keys--;
         return true;
     }
     return false;
 }


Then, on your pickup a slight variant on the first one-

 void OnTriggerEnter(Collider other)
 {
     (name of your player script) player = other.GetComponent<Put the name of your player script here>();
     if(player)
     {
          player.AddKey();
          Destroy(gameObject);
     }
 }

That gives you a key, and destroys the pickup.

Then on your door, something else similar (have a trigger volume along with an actual door- so that if the player enters the trigger with a key, the door opens)

 public Transform thisIsTheDoorAssignItInInspector;
 void OnTriggerEnter(Collider other)
 {
     (name of your player script) player = other.GetComponent<Put the name of your player script here>();
     if(player)
     {
          if(player.CanOpenDoor())
          {
              Destroy(thisIsTheDoorAssignItInInspector);
              Destroy(this);
          }
     }
 }

That's a little more sophisticated!

If you wanted to check for specific keys, you could go even further and do something like this-

 [System.Serializable]
 public enum KeyType
 {
     Red,
     Blue,
     Yellow,
     Master
 }

 public int[] keys = new int[];
 public void AddKey(KeyType type)
 {
     keys[(int)type]++;
 }

 public bool CanOpenDoor(KeyType type)
 {
     if(keys[(int)type] > 0)
     {
          keys[(int)type]--;
          return true;
     } else if (keys[(int)KeyType.Master] > 0)
     {
         keys[(int)KeyType.Master]--
         return true;
     }
     return false;
 }

Then in your triggers for the pickups and the doors, use-

 public KeyType key; // Assign this in the editor! 
 //blah blah then in the trigger bit
 player.AddKey(key);

 // and then in the door;

 if(player.HasKey(key))
 {
     //Do things
 }

(Thanks, @wccrawford, for the suggestion!)

Comment
Add comment · Show 4 · 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 syclamoth · Oct 11, 2011 at 12:34 PM 0
Share

Whoops, that's not very good. Let's do it in a more complex way!

avatar image wccrawford · Oct 11, 2011 at 12:49 PM 0
Share

And of course, you could modify that last version to actually keep track of specific keys, ins$$anonymous$$d of just counting how many you have.

avatar image syclamoth · Oct 11, 2011 at 12:50 PM 0
Share

Oh yeah! I should post one of them as well.

avatar image syclamoth · Oct 11, 2011 at 12:58 PM 0
Share

There you go! I need to go home now.

avatar image
0

Answer by elfinger · Oct 11, 2011 at 02:27 PM

Thanks for your help, I'm just getting started with level design, and all suggestions are appreciated.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

physics.OverlapSphere colliders 1 Answer

Use Of OnCollisionStay()? 2 Answers

IsTouching and IsTouchingLayers for Colliders are not defined? 1 Answer

How to Specify a Collider in OnTriggerStay function? 1 Answer

2D - Two objects, each having two trigger colliders - interacting 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