Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 $$anonymous$$ · Aug 07, 2015 at 04:10 PM · rigidbodycollidertriggercharactercontrollerpickup

Picking up items with Rigidbodies

In my scene I have a Standard Assets' First Person Controller (with a kinematic Rigidbody and a CharacterController component).

At a certain point, I spawn an arbitrary number of pick-up-able objects. These objects need to be affected by physics, and therefore have a (non-kinematic) Rigidbody and a physical BoxCollider. They also have a SphereCollider marked as Trigger that is significantly larger than their BoxCollider, and marks the zone the player has to enter to pick up the object.

In case it's relevant, the objects' Rigidbodies are restricted to only Y movement (rotation is free in all directions), and everything uses Discrete collision detection.

The problem I am facing is that the OnTriggerEnter method in the script attached to of these objects does not get called consistently when my character enters the SphereCollider. In fact it generally only gets called after I've bumped into the object several times with my character.

Now according to the table in the manual, OnTriggerEnter should be getting called in this case: alt text

However it is not working properly.

I have tried a few alternatives, like:

  • attaching the OnTriggerEnter script to the character instead of the objects

  • putting one or both of the object's colliders onto (distinct) child objects

  • removing the SphereCollider entirely and only using the BoxCollider with the OnCollisionEnter method instead of OnTriggerEnter

The first two had the exact same result as the original setup, while the third didn't work at all.

The solution I'm currently using is a raycasting in the character's Update towards all pickup objects (which only have their BoxColliders, no trigger SphereColliders) in the scene to see if they're close enough, and picking up those that get hit by the raycasts. This does work properly, but it seems like a very crude and inelegant solution, and with a massive performance hit in the case of a larger number of pickup objects (calling GameObject.FindGameObjectsByTag every frame, and raycasting towards all of them).

Why isn't the original setup working, and is there a better way to approach this problem?

P.S. In the same scene I use a very similar setup with the tool rack (which spawns the tools when approached), except that it doesn't have a Rigidbody component (as it is stationary and doesn't need to be affected by physics). That one works without problems with a physical BoxCollider and a trigger SphereCollider.

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 Glurth · Aug 07, 2015 at 05:19 PM 0
Share

Could you please clarify what type of collider you have on your character? (I'm not very familiar with that asset you mentioned.) I suspect you want to have BOTH a trigger and non-trigger collider on your character, (just like you already do for your spawned objects). When the two trigger-colliders touch, an OnTriggerEnter message should always be called for both (though you don't need to actually handle the event in both).

P.S. rather than use raycasts, since you are trying to simulate sphere colliders, perhaps you can simply compute the distance to the object?

avatar image $$anonymous$$ · Aug 07, 2015 at 07:56 PM 0
Share

I have a CharacterController component on my character, which serves as a collider.

Fair point about the distance calculation. I went with the raycast approach to prevent being able to pick up objects through the wall (was planning to do that in OnTriggerEnter anyway), but distance calculation would indeed work to simulate SphereColliders. However it still requires a call to GameObject.FindGameObjectsByTag every frame, and iterating through all of them.

2 Replies

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

Answer by dsmeathers · Aug 07, 2015 at 08:34 PM

Sounds like what your doing should work. If you're using a capsule collider for the player, try changing it to a box. Capsule colliders vs triggers have always been really buggy in Unity.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Aug 08, 2015 at 08:34 AM 0
Share

I've removed the SphereColliders from the objects and ins$$anonymous$$d added a trigger Box Collider to the player object and it works now. Cheers.

avatar image
0

Answer by curiouspers · Aug 07, 2015 at 05:16 PM

I think you have unpredictable results because you have two Colliders on object. Putting one of the object's colliders onto (distinct) child objects, AND correcting your code should help. OR maybe this is your solution: here It'll be so much simpler to say if you've attached example scene.

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 $$anonymous$$ · Aug 07, 2015 at 08:12 PM 0
Share

I don't know how to attach a scene, as if I try to edit the question with Attachment (ctrl + U) and add a .unity scene file, it says "This attachment is not permitted because the file type is invalid."

I've seen a similar thread to the one you linked. I've tried putting transform.position = transform.position + Vector3.zero in the tool's Update function, but it didn't help.

avatar image dsmeathers · Aug 08, 2015 at 06:03 AM 0
Share

Can you upload a video of the issue?

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

Trigger Rigidbody with CharacterController 0 Answers

Character Controller Object Randomly Falls Through Colliders 3 Answers

Rigidbody.SweepTest Hit Trigger Colliders 0 Answers

CharacterController vs Rigidbody (photo included) 2 Answers

Trigger Collision with Character Controller 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