0
\$\begingroup\$

I've been following this tutorial: Understanding Steering Behaviors: Collision Avoidance

but I'm having trouble implementing one of the functions correctly in Unity, here's the code:

private function lineIntersectsSphere(ahd:Vector3, ahd2:Vector3, sphCol:SphereCollider) : boolean
{
    return distance(sphCol.center, ahd) <= sphCol.radius || distance(sphCol.center, ahd2) <= sphCol.radius;
}

The SphereCollider variable contains the Center variable, however, it always returns a vector containing 0, 0, 0 values. I imagine it's the spheres local origin.. is there a way to get the center? Perhaps it could be through the transform.position that the sphere is attached to?

\$\endgroup\$
1
\$\begingroup\$

By default, colliders in Unity will be centered on the object's pivot point (given by transform.position). It's possible to configure a collider such that it's not centered -- in this case, that offset is given by the sphere's center property, which is why you're seeing an offset of zero.

In mooooost cases, transform.position will be fine. If your sphere is offset from the pivot, though, you'll have to account for that:

Vector3 sphereCenter = transform.TransformPoint(sphCol.center);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.