Detect Volume on Collision
I'm trying to write a script that detects the volume on collision with another object. The intention is to have the player object grow in size when it collides with objects that have less volume than itself. Very new to this and managed to piece together what I have below using the Google and this site as a resource. I've had some success, but have been running into weird results where the player object will refuse to grow after colliding a few times.. I'm not sure what the issue is, but would be curious to know if there's anything i'm missing or if there's a more efficient way to do this. Any advice or suggestions would be helpful!
{
private BoxCollider playerCollider;
public float playerVolume;
void Start()
{
playerCollider = GetComponent<BoxCollider>();
Vector3 geometry = playerCollider.bounds.size;
playerVolume = geometry.x * geometry.y * geometry.z;
}
void OnTriggerEnter(Collider other)
{
Vector3 geometry = other.bounds.size;
float otherVolume = geometry.x * geometry.y * geometry.z;
if (playerVolume > otherVolume)
{
transform.localScale += new Vector3(1, 1, 1) * growthRate;
}
}
}
Answer by CatEngineDVD · Jan 14 at 12:05 PM
It's been a year but I think I know what your issue is. See, you're checking against the named variable "playerVolume", but you aren't updating the variable when you update the scale. So the player will only absorb objects that have lower volume than it's initial volume, not it's current one. So either update the playerVolume every time it changes, or perform the calculation before the condition.
Your answer
Follow this Question
Related Questions
Unity Particle Trigger detection 0 Answers
Display Hit Text On Collition 0 Answers
Collision Detection Not Working (Unity 5.2.3) 5 Answers
Destroy GameObject inconsistent with Instantiated children 1 Answer