- Home /
Applying the Bounds class to a Sphere without using the Collision class
I am trying to set up a bounds for a sphere made using gameObject to use to detect when the sphere intersects the ground. This is what my code looks like so far
public class Particle{
public GameObject sphere;
public Bounds sphereBound;
}
Particle aSphere = new Particle();
aSphere.sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
float radius = Random.Range (0f, 0.5f);
aSphere.sphere.transform.localScale = new Vector3 (radius, radius, radius);
aSphere.sphere.transform.position = new Vector3 (0f, 10.0f, 0f);
aSphere.sphereBound = new Bounds (aSphere.sphere.transform.position, aSphere.sphere.transform.localScale);
aSphere.sphere.transform.parent = transform;
Is this the right way of adding Bounds to my sphere or is there something I'm missing? Also, this is a project in which I have to figure out the collision detection myself instead of using the classes in Unity so I am not allowed to use the Rigidbody or Collision class.
Answer by robertbu · Oct 05, 2014 at 11:18 PM
The code you have will correctly build a bounds for the sphere at the point of creation (like you have here). There are some issues:
If you are trying to update the bounds due to object size changes you may have trouble if the parent (transform) does not have localScale of (1,1,1).
You will need to update the bounds.center each time the object is moved.
Using bounds for detecting collisions will produce false positives and make applying the force for the collision difficult. For example, say the corners of two bounds brushed each other. You would detect a collision, but the object would not have touched.
If all of your objects are spheres, it would be better to save the world radius of each 'Particle'. Then say you had the transforms of two objects, t1 and t2, and the radius of each object r1 and r2. You would know if they collided if:
if (Vector3.Distance(t1, t2) < r1 + r2) {
Deubg.Log("The two objects collided");
}