- Home /
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);}
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.
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!
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.
Answer by crtapps · Jun 12, 2012 at 11:58 PM
Try to attach a Rigidbody into either of the two bubbles.
Your answer
Follow this Question
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