Here’s another idea. I didn’t like the idea of the projectile needing to know anything about the target much less that it may have a Capsule collider. Also what if the capsule is on its side? I instead created a method on Health to return its centermass.
If you wanted to be super efficient you could probably create a single method that returns centermass if object has a collider or transform.position if it’s not.
Edit: I revised to put the TryGetCenterMass method on Health script since we use seem to Health for this type of purpose.
public class Projectile : MonoBehaviour
{
private Vector3 GetAimLocation()
{ // remember target is a local var of type Health
if (target.TryGetCenterMassLocation(out Vector3 centerMass))
{
return centerMass;
}
else
{
return target.transform.position;
}
}
}
public class Health : MonoBehaviour
{
public bool TryGetCenterMassLocation(out Vector3 centerMassLocation)
{
if (TryGetComponent<Collider>(out Collider collider))
{
centerMassLocation = collider.bounds.center;
return true;
}
else
{
centerMassLocation = new Vector3(0,0,0);
return false;
}
}
}