Question by
nate_unity846 · Oct 19, 2020 at 08:07 PM ·
scripting problemcollisionphysicsvelocity
Issue with impulse.magnitude and if statements?
Hey guys, I'm having a bit of an issue with this code. I have a propane tank that I want to have explode if something hits it too hard. it has a capsule collider and rigidbody attached. This cannister will explode if it touches literally anything at any speed, even when I set the threshold to an ungodly number. any idea why it's passing the if statement? Any help would be much appreciated, the debug output returns 1198.613 before and after the if statement so I'm pretty confused...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExplodingProp : MonoBehaviour
{
public float explosionForce;
public float explosionRadius;
public GameObject[] effect;
void OnCollisionEnter(Collision collision) //this returns the same number as the next log
{
float collisionforce = (collision.impulse.magnitude / Time.fixedDeltaTime);
UnityEngine.Debug.Log(collisionforce);
if (collisionforce > 2000.00f); //I can set this number to a quintillion and it wont matter
{
UnityEngine.Debug.Log(collisionforce); //force remains the same at 1100 ish
foreach (GameObject particle in effect)
{
Instantiate(particle, transform.position, transform.rotation);
}
Collider[] objects = UnityEngine.Physics.OverlapSphere(transform.position, explosionRadius);
foreach (Collider h in objects)
{
Rigidbody r = h.GetComponent<Rigidbody>();
if (r != null)
{
r.isKinematic = false;
r.AddExplosionForce(explosionForce, transform.position, explosionRadius);
}
}
Destroy(gameObject);
}
}
}
Comment