-1
\$\begingroup\$

I have a NavMesh agent and I would like forces to be applied to it during an explosion. Currently the NavMesh agent isn't interacting with the exploding object. The explosion occurs and agent takes damage but no pushback occurs on the agent.

using UnityEngine;
using System.Collections;

public class explosion : MonoBehaviour {
public LayerMask enemy;
public AudioSource explosionAudio;
public ParticleSystem explosionParticals;
public float maxdamage = 40f;
public float explosionforce = 1000f;
public float maxlifetime = 5f;
public float explosionradius = 5f;
// Use this for initialization
void Start () 
{
    Destroy (gameObject, maxlifetime);
}

private void OnTriggerEnter(Collider other)
{
    Collider[] colliders = Physics.OverlapSphere (transform.position, explosionradius, enemy);
    for (int i = 0; i < colliders.Length; i++)
    {
        Rigidbody targetRigidbody = colliders [i].GetComponent<Rigidbody> ();
        if (!targetRigidbody)
            continue;
        targetRigidbody.AddExplosionForce (explosionforce, transform.position, explosionradius);
        enemyhealth targethealth = targetRigidbody.GetComponent<enemyhealth> ();
        if (!targethealth)
            continue;
        float damage = calculateDamage (targetRigidbody.position);
        targethealth.Takedamage (damage);
    }
    explosionParticals.transform.parent = null;
    explosionParticals.Play ();
    explosionAudio.Play ();
    Destroy (explosionParticals.gameObject, explosionParticals.duration);
    Destroy (gameObject);
}
private float calculateDamage (Vector3 targetPosition)
{

    Vector3 explosiontotarget = targetPosition - transform.position;
    float explosiondistance = explosiontotarget.magnitude;
    float relativedistance = (explosionradius - explosiondistance) / explosionradius;
    float damage = relativedistance * maxdamage;
    damage = Mathf.Max (0f, damage);
    return damage;


}


// Update is called once per frame
void Update () 
{




}
}

navmesh agent

using UnityEngine;
using System.Collections;

public class enmymove : MonoBehaviour {

Transform player;
NavMeshAgent nav;
healthscript Health;
Animator anim;





void Awake ()
{
    player=GameObject.FindGameObjectWithTag ("Player").transform;
    nav = GetComponent<NavMeshAgent>();
    Health =player.GetComponent<healthscript> ();
    anim = GetComponent<Animator>();


}


void Update () {

     if (Health.currenthealth > 0 ) 
        {
            nav.SetDestination (player.position);
        }
        else
    {
        GetComponent<NavMeshAgent> ().enabled = false;
        anim.SetTrigger ("idle");
    }

    }
  }
\$\endgroup\$
  • 1
    \$\begingroup\$ Low quality. Take your time to polish questions. This makes it easier for both of us. \$\endgroup\$ – S. Tarık Çetin Oct 8 '16 at 15:22
0
\$\begingroup\$

You need to disable the navagent before applying the explosion and make sure it has a rigidbody attached.

explodingobject:

private void OnTriggerEnter(Collider other)
{
    Collider[] colliders = Physics.OverlapSphere (transform.position, explosionradius, enemy);
    for (int i = 0; i < colliders.Length; i++)
    {
        Rigidbody targetRigidbody = colliders [i].GetComponent<Rigidbody> ();
        if (!targetRigidbody)
            continue;

        NavMeshAgent nav = targetRigidbody.GetComponent<NavMeshAgent>();
        nav.enabled = false;
        targetRigidbody.AddExplosionForce (explosionforce, transform.position, explosionradius);
        enemyhealth targethealth = targetRigidbody.GetComponent<enemyhealth> ();
        if (!targethealth)
            continue;
        float damage = calculateDamage (targetRigidbody.position);
        targethealth.Takedamage (damage);
    }
    explosionParticals.transform.parent = null;
    explosionParticals.Play ();
    explosionAudio.Play ();
    Destroy (explosionParticals.gameObject, explosionParticals.duration);
    Destroy (gameObject);
}

navagent:

using System.Collections;

public class enmymove : MonoBehaviour {

Transform player;
NavMeshAgent nav;
healthscript Health;
Animator anim;
Rigidbody rigidBody;




void Awake ()
{
    player=GameObject.FindGameObjectWithTag ("Player").transform;
    nav = GetComponent<NavMeshAgent>();
    Health =player.GetComponent<healthscript> ();
    anim = GetComponent<Animator>();
    rigidBody =  GetComponent<Rigidbody>();

}


void Update () {

     if (Health.currenthealth > 0 ) 
        {
            if(nav.isActiveAndEnabled)
               nav.SetDestination (player.position);
        }
        else
        {
           GetComponent<NavMeshAgent> ().enabled = false;
           anim.SetTrigger ("idle");
        }

    }

}

Make sure to enable the navagent again after x seconds or whatever is the functionality you desire

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.