- Home /
How to get expanding object collide when it touches others?
I have an object that is expanding by adding to its localscale each frame. I want it to stop expanding when it hits another collider, but OnCollisionEnter never fires (neither does OnCollisionExit, or OnCollisionStay). How can I tell it to trigger something once it expands to touch another collider?
Here's my script:
using UnityEngine;
using System.Collections;
public class script_blob_player : MonoBehaviour {
private bool touchingSomething = false;
void Update () {
if (!touchingSomething) { Expand (); }
}
void Expand () {
float expansionFactor = 1.0f;
transform.localScale += new Vector3(expansionFactor, 0, expansionFactor) * Time.deltaTime;
}
void OnCollisionEnter (Collision collision) {
touchingSomething = true;
}
}
And yes, my growing object does have a rigid body, which appears to grow as my object grows. And if I turn on gravity so that it falls on another object, the OnCollisionEnter DOES fire. But I'm not using gravity here.
I also found an old similar question that was never answered: http://answers.unity3d.com/questions/142211/detect-collision-between-static-and-expanding-obje.html
Thanks!
They're not spheres; there are various objects.
I'll try the trigger thing and get back to you...
It looks like the trigger DID work. Thanks for the suggestion!
If you copy your "trigger" solution down as an Answer, I'll gladly choose it as the best answer.
Answer by OP_toss · Aug 21, 2013 at 05:58 PM
Just a thought, but what if you used a Trigger instead of a Collider?
This way it should call OnTriggerEnter when it has expanded enough to tough another collider, and since it's stopping anyways, it shouldn't need to collide with that collider. If it needs collision later, just enable it after the expansion.
You can also do a mathematical approach, by comparing your collider's position and radius to the surrounding colliders' positions and radii. Simple distance math will tell you the exact maximum value you can expand to. This assumes they're both spheres of course...
hope this helps!
Your answer
Follow this Question
Related Questions
Resizing an object on collision in unity,Resizing an Object on Collision in unity 1 Answer
OnCollisionStay2D is ignoring OnCollisionEnter2D 2 Answers
Why OnCollisionEnter cannot detect collisions between colliders? 1 Answer
Check if collider is colliding with anything non specific? 1 Answer
how to PlayOneShot from a OnCollisionEnter wiith a RaycastHit /Collider>Raycast 1 Answer