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 Paul Mac · Mar 06, 2011 at 10:44 AM · collisiononcollisionenterdetection

collision detection

I am trying to get 2 bubbles to notice when they have collided. Can anyone tell me why this code isnt firing collision detection. The inspector windows is below.

var bubbleSpeed : Vector3 ;

function Start() { bubbleSpeed= Vector3(0,Random.Range(1.0,10.0),0); rigidbody.MovePosition(rigidbody.position + bubbleSpeed * Time.deltaTime); }

function FixedUpdate () { //move the bubble up the screen

rigidbody.MovePosition(rigidbody.position + bubbleSpeed Time.deltaTime); transform.Translate(bubbleSpeed Time.deltaTime); transform.Rotate( bubbleSpeed* Time.deltaTime); //slowly rotate the bubble

// check for top of screen if (transform.position.y > 50) { ResetBubble (); } }

function ResetBubble() { //Reset the position of the bubble back to bottom of screen var sphereColor : Color;

rigidbody.position.y = 2; rigidbody.position.x = Random.Range(15.0,33.0);

bubbleSpeed= Vector3(0,Random.Range(1.0,10.0),0);

rigidbody.MovePosition(rigidbody.position + bubbleSpeed * Time.deltaTime);

}

function OnCollisionEnter (collision : Collision) //why isnt this colliding ?????? { Debug.Log("collided with "+collision.gameObject.tag);}

alt text

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Mar 06, 2011 at 11:09 AM

Using Translate and Rotate defeats the purpose; only use MovePosition and MoveRotation for rigidbodies if you want them to interact correctly. (Also, using MovePosition in Start and your ResetBubble function isn't really doing anything, though that's not really relevant.) However, you're better off just applying a force once when the bubble is created and not moving it manually.

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
1

Answer by inejwstine · Jun 16, 2012 at 07:45 AM

I was recently trying to do something similar and found out two ways to accomplish it, depending on how you want your "bubbles" to behave. In both cases, your bubble object will have both a collider and a rigid body.

(1) If you want them to collide - meaning, they bump into each other and even push each other around - then make sure the collider's Is Trigger is turned off and your rigid body's Is Kinematic is turned off. Keep the rigidbody.MovePosition in your FixedUpdate, but remove the transform.Translate. OnCollisionEnter should now be called whenever the rigid bodies collide, and they'll bump around as if acted upon by physical forces.

(2) If you don't want your objects to collide but just want to know when they've touched, do basically the opposite: Turn the collider's Is Trigger ON and the rigid body's Is Kinematic ON. Remove MovePosition and Translate code from FixedUpdate. Instead, put transform.Translate in the Update function. Finally, instead of using OnCollisionEnter, you'll be using OnTriggerEnter, and it will be called whenever a rigid body touches the bubble; and since your bubbles have rigid bodies attached to them (they're just kinematic) it will even detect if one bubble touches another.

Check out this page for more info about colliders and such. (Thanks to sven1994 for posting it in this question.) Hope that helps!

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 RampantStudios · Mar 06, 2011 at 12:08 PM

I think your main problem is that you've set your collider to be a Trigger (The little check box saying "Is Trigger")

If you were calling the OnTriggerEnter function I think it'd fire, whereas your calling the OnCollisionEnter function, which wont fire so long as your set to a trigger.

Your looking to a rigidbody and apply forces to it - http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html

If your directly changing the transform of the object; i.e. this.transform.position = new Position(x,y,z); it wont trigger the collision events your requiring.

I'd say stick with the current code, but remove the Translate part.

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 crtapps · Jun 12, 2012 at 11:58 PM

Try to attach a Rigidbody into either of the two bubbles.

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

2 People are following this question.

avatar image avatar image

Related Questions

Detect collision from a another object,Detect collision from another script 2 Answers

Collision Detection 2 Answers

Collision detections fails to re-detect an object 1 Answer

onCollisionEnter 1 Answer

WALL collision detection problem... help!!!! 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