Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nyhtian · Oct 26, 2015 at 12:31 PM · triggercollision detection

What is the best way to find the item underneath the player?

Alright, so here's the deal: I have a fantabulous butterfly that has a trigger collider underneath. There will be items on the ground and when they are in the collider, and the player hits the spacebar I want the item in the collider to be picked up (Vector3 grabLocation = transform.localPosition; grabLocation.y -= 2; target.transform.position = grabLocation;) by the butterfly. Can some godly C# expert emerge from the coding heavens and explain how I can tell the butterfly which item to pick up. Please.

Comment
Add comment · Show 1
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 Scribe · Oct 26, 2015 at 01:10 PM 0
Share

You are going to have to keep a list of the objects that have entered and not yet exited your collider, then run a for loop over those objects and see which is the closest valid object I guess.

Alternatively you could try using physics.spherecast so you don't have to maintain your list

1 Reply

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

Answer by fafase · Oct 26, 2015 at 03:16 PM

You would have to think of interface or abstract class (or base class...).

 public abstract class ICollectible:MonoBehaviour{
      public abstract void Collect();
 } 

All collectible items should inherit from that class and implement the collect method.

This way you can do:

 private List<ICollectible> collectibles = new List<ICollectible>();
 private void OnTriggerEnter(Collider col)
 {
      var col = col.gameObject.GetComponent<ICollectible>();
      if(col != null){
            this.collectible.Add(col);
      }
 }
 private void OnTriggerEnter(Collider col)
 {
      var col = col.gameObject.GetComponent<ICollectible>();
      if(col != null){
            this.collectible.Add(col);
      }
 }
 private void OnTriggerEnter(Collider col)
 {
      var col = col.gameObject.GetComponent<ICollectible>();
      if(col != null){
            this.collectible.Add(col);
      }
 }
  
 private void OnTriggerExit(Collider col)
 {
      var col = col.gameObject.GetComponent<ICollectible>();
      if(col != null){
            this.collectible.Remove(col);
      }
 }

 private void Update(){
       if(input.getKeyDown(KeyCode.Space)){
              foreach(ICollectible col in collectibles){
                     col.Collect();
              }
              collectibles.Clear();
       }
 }

So when an item enters the collider, if it is a ICollectible it is added to the list and when it exist it is removed. When the user presses space, all items in the list are called Collect. Because they have to implement Collect for being ICollectible, you can make them do different things, for instance if a speed item or a weapon item they will implement Collect differently and the compiler will just do it right.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't get trigger collision detection to work 1 Answer

Colliding with multiple objects 1 Answer

Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer

Setting a Prefab Clone as the Child of another Object on Collision (2D) 1 Answer

Loadlevel upon collission 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