Question by
jeromesmith · Mar 01, 2017 at 01:19 AM ·
rigidbodyenemynavmeshagentenemyai
navmeshagent and rigidbody question
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
namespace Player { public class GrenadeExplosion : MonoBehaviour {
private Collider[] hitColliders;
public float blastRadius;
public float explosionPower;
public LayerMask explosionLayers;
private Rigidbody enemyRigidBody;
private Vector3 enemyVelocity;
void ExplosionWork(Vector3 explosionPoint)
{
hitColliders = Physics.OverlapSphere(explosionPoint, blastRadius, explosionLayers);
foreach (Collider hitCol in hitColliders)
{
if (hitCol.GetComponent<NavMeshAgent>() != null)
{
hitCol.GetComponent<NavMeshAgent>().enabled = false;
}
if (hitCol.GetComponent<Rigidbody>() != null)
{
hitCol.GetComponent<Rigidbody>().isKinematic = false;
hitCol.GetComponent<Rigidbody>().AddExplosionForce(explosionPower, explosionPoint, blastRadius, 1, ForceMode.Impulse);
if (hitCol.CompareTag("Enemy"))
{
enemyRigidBody = hitCol.GetComponent<Rigidbody>();
enemyVelocity = enemyRigidBody.velocity;
if (enemyVelocity == Vector3.zero)
{
Destroy(hitCol.gameObject);
}
}
}
}
}
void OnCollisionEnter(Collision col)
{
ExplosionWork(col.contacts[0].point);
Destroy(gameObject);
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
}
desired effect: enemy object is blown up into the air on collision with grenade and lands on the floor, once its stopped tumbling, its get destroyed. actual effect: enemy object is blown up on collision with grenade.
how do I implement the desired effect?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612112207im_/https://answers.unity.com/themes/thub/images/avi.jpg)