Search Unity

how to stop navmesh agent when hit by object?

Discussion in 'Scripting' started by prxxxy, Jul 24, 2021.

  1. prxxxy

    prxxxy

    Joined:
    Jun 25, 2020
    Posts:
    16
    What do I need to add to stop my model using the navmesh agent when by and object.

    Currently it gets hit goes ragdoll and then slides along the rest of the way!

    This is the current script I am using what would I need to add in?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class RagdollOnOff : MonoBehaviour
    {
    public CapsuleCollider mainCollider;
    public GameObject ThisGuysRig;
    public Animator ThisGuysAnimator;

    void Start()
    {
    GetRagdollBits();
    RagdollModeOff();

    }
    void Update()
    {

    }
    private void OnCollisionEnter(Collision collision)
    {
    if(collision.gameObject.tag == "MyNewCollisionTag")
    {
    RagdollModeOn();
    }
    }

    Collider[] ragDollColliders;
    Rigidbody[] limbsRigidbodies;
    void GetRagdollBits()
    {
    ragDollColliders = ThisGuysRig.GetComponentsInChildren<Collider>();
    limbsRigidbodies = ThisGuysRig.GetComponentsInChildren<Rigidbody>();
    }

    void RagdollModeOn()
    {
    ThisGuysAnimator.enabled = false;

    foreach (Collider col in ragDollColliders)
    {
    col.enabled = true;
    }

    foreach (Rigidbody rigid in limbsRigidbodies)
    {
    rigid.isKinematic = false;
    }

    mainCollider.enabled = false;
    GetComponent<Rigidbody>().isKinematic = true;
    }

    void RagdollModeOff()
    {
    foreach(Collider col in ragDollColliders)
    {
    col.enabled = false;
    }

    foreach (Rigidbody rigid in limbsRigidbodies)
    {
    rigid.isKinematic = true;
    }

    ThisGuysAnimator.enabled = true;
    mainCollider.enabled = true;
    GetComponent<Rigidbody>().isKinematic = false;
    }

    }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Welcome to the forums.

    As well as using code-tags (not plain-text) mentioned above, please note that there are dedicated forums for topics such as Navmesh which will help you in the future getting the answers you need.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    (tbf, I'd still consider this scripting as it's not necessarily about the navigation itself, rather scripting around it and that forum isn't nearly as viewed as scripting)