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 saamp · May 21, 2013 at 10:02 PM · gameobjectfirstcoin

OnCollisionEnter multiple objects

Hi, first post here.

I'm having a problem getting OnCollisionEnter to work with another object. Basically in my game I have coins that fall down from the sky. The player is supposed to collide with the coins and the coins should disappear (basic mario stuff). Now these coins have a rigidBody on them because I want to use gravity to make them fall. The issue I run into is once the Coin has reach the ground it enters collision with the ground and The player cannot pick it up. However if the player reaches the coin before it touches the ground it works properly and disappears. I'm not sure what's going on with OnCollisionEnter and why the player can't just pick up the coin when it's on the ground.

Here's my code for the Coin...

 function Start () 
 {
     
     Physics.gravity = Vector3(0,gravityValue,0);
 }
 
 function OnCollisionEnter(other:Collision)
 {
     
         print("COLLiDING");
     if(other.gameObject.tag == "Player")
     {
         print("touching coin");
         Destroy(gameObject);
     }
     
     
 }
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 saamp · May 21, 2013 at 10:56 PM 0
Share

well I decided to AddForce of 0 to the coin once it's velocity reaches 0. This wakes up the coin so the collisions still work.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · May 21, 2013 at 10:08 PM

because when rigidbodies stop moving they go to sleep to improve performance.

http://docs.unity3d.com/Documentation/Components/RigidbodySleeping.html

kinematic rigidbodies will wake up sleeping ones.

but there are other solutions

you can easily simulate gravity

during update simply move down if they aren't grounded.

if you want to use rigidbodies do a line where you go

 if (mathf.approximately(rigidbody.velocity, vector3.zero) //so if your not basically stopped
 {
 //remove the rigidbody component
 
 }

now it doesn't have a rigidbody and the gravity is over you don't need it now you can collide.

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 aamesxdavid · May 21, 2013 at 10:17 PM

Basically, what sparkzbarca said, but personally, I've had more luck with keeping all of that code on the player end. That is, have an OnCollisionEnter() function in your player object, and tag the coins. Then, send a message to the coin to destroy it, and add your score/points wherever. So for your player:

 function OnCollisionEnter(other:Collision)
 {
     if(other.gameObject.tag == "Coin")
     {
        print("touching coin");
        other.SendMessage("PickUpCoin");
        myScore += 1;
     }
 }

And then in your coin object:

 function PickUpCoin()
     {
         Destroy(gameObject);
     }
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

15 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

HELP Find gameObject With tag in another array 1 Answer

How to tell if 2 blocks are next to each other in a 2d game? 1 Answer

GameObject block particles?! help 4 Answers

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer


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