Hello everyone !
I need help with a script that basically detects if the object the player collides with is bigger or smaller than the player.
If the object is bigger, the player dies. if the object is smaller, the player eats it.
C# is what I’m working with, please guys any help would really help me out.
Assuming you are using boxColliders, you can use:
private BoxCollider boxCollider;
private float volume;
void Start()
{
boxCollider = GetComponent<BoxCollider >();
Vector3 geometry = boxCollider.bounds.size;
volume = geometry.x * geometry.y * geometry.z;
}
void OnTriggerEnter(Collider otherCollider)
{
Vector3 geometry = otherCollider.bounds.size;
float otherVolume = geometry.x * geometry.y * geometry.z;
if (volume < otherVolume)
{
// die;
}
else if (volume > otherVolume)
{
// eat;
}
}
There is ways to calculate volume for spheres and cillinders (and other shapes) too, google is your friend here. Another option would be play around with the Collider.bounds to see what fits to you.
Also, the Mesh component also has a bounds.size, but I would say to you stick with colliders, as handling collisions will be easier.