I’m trying to make a collision/health system, however nothing is being added to the hitAmount value even when the applied force is more than 3.5.
var hitAmount : float;
var appliedForce : float;
function Update () {
if(appliedForce >= 3.5)
hitAmount +=10.0;
if(hitAmount >= 100.0)
Destroy(gameObject);
}
function OnCollisionEnter () {
appliedForce = rigidbody.velocity.magnitude / 10;
}
As @Eric5h5 and @DaveA said, you should not increment hitAmount in Update: if a collision sets appliedForce to a high enough value, hitAmount will be incremented each frame by 10, destroying your object in 10 frames or less. You should compare appliedForce in the collision event:
var hitAmount : float;
var appliedForce : float;
function Update () {
if(hitAmount >= 100.0)
Destroy(gameObject);
}
function OnCollisionEnter () {
appliedForce = rigidbody.velocity.magnitude / 10;
if(appliedForce >= 3.5)
hitAmount +=10.0;
}
But there’s another important issue: you’re just checking if the rigidbody has an absolute velocity greater than 35m/s (too high!) when colliding, no matter which’s the other object’s velocity. You should check the relative velocity, like this:
function OnCollisionEnter(col: Collision){
if (col.relativeVelocity.magnitude > someLimit){
hitAmount += 10.0;
}
}
As I said, 35m/s is a too high velocity: it’s 0.7m per physics cycle, what means that objects thinner than 0.7m may be just ignored by the collision system. You should use a smaller limit - maybe 5 or 10 m/s - as someLimit in the OnCollisionEnter event above.