x


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

more ▼

asked Mar 06 '11 at 10:44 AM

Paul Mac gravatar image

Paul Mac
3 2 2 3

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Mar 06 '11 at 11:09 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 16 '12 at 07:45 AM

inejwstine gravatar image

inejwstine
4 2 2 3

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 06 '11 at 12:08 PM

RampantStudios gravatar image

RampantStudios
116 3 4 9

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 12 '12 at 11:58 PM

crtapps gravatar image

crtapps
39 4 5 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2584
x231
x181

asked: Mar 06 '11 at 10:44 AM

Seen: 1648 times

Last Updated: Jun 16 '12 at 07:45 AM