Search Unity

NavMesh flee from Player

Discussion in 'Navigation' started by MikeyJY, Feb 20, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I don't know if this forum should be posted on Navigation or Animation topic.
    I'm trying to make an Flee From Player AI with animation for rabbit, the running part is working, but the animation switched very fast from run, idle.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;  
    5.  
    6. public class rabbit_ai : MonoBehaviour {
    7.     private NavMeshAgent agent;
    8.     public GameObject Player;
    9.     public float EnemyDistanceRun = 6.0f;
    10.     private Vector3 newPos;
    11.     public Vector3 pos;
    12.     public Vector3 lastPos;
    13.     public float dist;
    14.     // Use this for initialization
    15.  
    16.    
    17.  
    18.     void Start () {
    19.         agent = GetComponent<NavMeshAgent>();
    20.         agent.height = 0.5f;
    21.         agent.baseOffset = 0;
    22.        
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.         float distance = Vector3.Distance(transform.position, Player.transform.position);
    28.  
    29.         if(distance < EnemyDistanceRun){
    30.             Vector3 dirToPlayer = transform.position - Player.transform.position;
    31.             Vector3 newPos = transform.position + dirToPlayer;
    32.             agent.SetDestination(newPos);
    33.             //GetComponent<Animator>().speed = 0.2f;
    34.            
    35.             GetComponent<Animator>().Play("run");
    36.         }
    37.         dist = agent.remainingDistance;
    38.         if(dist > 0.5){
    39.             GetComponent<Animator>().Play("run");
    40.         }
    41.         if (dist < 0.5) {
    42.             GetComponent<Animator>().Play("idle");
    43.          
    44.         }
    45.  
    46.     }
    47. }
    48.  
    The run animation isn't fnished, because idle plays continuously

    That's transiton inspector (idle to run):
    upload_2020-2-20_20-47-37.png

    And run to idle:
    upload_2020-2-20_20-48-32.png
     
  2. DavieCud

    DavieCud

    Joined:
    Sep 26, 2020
    Posts:
    2
    Don't know if you got this fixed, but looking at your Transitions you have not set the Running bool condition in your idle and run state conditions list at the bottom. In fact on second looks at your code you are using the .Play(" ") instead of .Setbool(" ", true/false) if you are going to use .Play() you don not need the transitions set from idle to run. You are trying to do a mixture of the both in above code.
     
  3. japonskO75

    japonskO75

    Joined:
    Sep 26, 2015
    Posts:
    17
    Code (CSharp):
    1. private NavMeshAgent agent;
    2.     public GameObject Player;
    3.     public float EnemyDistanceRun = 6.0f;//aku vzdialenost ma utiect od hraca
    4.     private Vector3 newPos;
    5.     private Vector3 pos;
    6.     private Vector3 lastPos;
    7.     public float dist;
    8.     // Use this for initialization
    9. Animator animator;
    10.  
    11.     void Start () {
    12.           animator= GetComponent<Animator>();
    13.         agent = GetComponent<NavMeshAgent>();
    14.        agent.height = 1.5f;
    15.     agent.baseOffset = 0;
    16.      
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         float distance = Vector3.Distance(transform.position, Player.transform.position);
    22.         if(distance < EnemyDistanceRun){
    23.             Vector3 dirToPlayer = transform.position - Player.transform.position;
    24.             Vector3 newPos = transform.position + dirToPlayer;
    25.             agent.SetDestination(newPos);
    26.           //  GetComponent<Animator>().speed =4.0f;
    27.                animator.SetBool("iswalking",true);
    28.                animator.SetBool("idle",false);
    29.            // GetComponent<Animator>().Play("Running");
    30.         }
    31.         dist = agent.remainingDistance;
    32.         if(dist > 0.5){
    33.             //GetComponent<Animator>().Play("Running");
    34.             animator.SetBool("iswalking",true);
    35.                animator.SetBool("idle",false);
    36.            
    37.         }  
    38.         if (dist < 0.5) {
    39.  
    40.            // GetComponent<Animator>().Play("Zombie Idle Alert");
    41.                      animator.SetBool("iswalking",false);
    42.                animator.SetBool("idle",true);
    43.         }      
    44.     }
     
  4. japonskO75

    japonskO75

    Joined:
    Sep 26, 2015
    Posts:
    17
    And Has Exit Time uncheck,and idle ,running clip check FootIK