Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help-me Script AI NavMesh Follow Player

Discussion in 'Scripting' started by juniorforestS, Feb 20, 2017.

  1. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;

    public class DamageHandler_Enemy : vp_DamageHandler
    {
    private Animator _animator;
    private NavMeshAgent _navMeshAgent;
    public GameObject Player;
    public float AttackDistance = 10.0f;

    protected override void Awake()

    {
    base.Awake();
    _navMeshAgent = GetComponent<NavMeshAgent>();
    _animator = GetComponent<Animator>();
    }


    public void Update()

    {

    if (_navMeshAgent.enabled)

    {
    float dist = Vector3.Distance(Player.transform.position, this.transform.position);
    if (dist < AttackDistance)

    {

    _navMeshAgent.SetDestination(Player.transform.position);
    _animator.SetBool("Attack", false);
    _animator.SetBool("IsFollow", true);

    }
    else
    {

    _animator.SetBool("Attack", false);
    _animator.SetBool("IsFollow", false);
    _navMeshAgent.SetDestination(transform.position);

    }


    if (dist < 3.0f)

    {
    _animator.SetBool("Attack", true);
    _animator.SetBool("IsFollow", false);
    }
    }
    }


    public override void Damage(vp_DamageInfo damageInfo)

    {

    if (CurrentHealth > 0 )

    {

    AnimatorStateInfo si = _animator.GetCurrentAnimatorStateInfo(0);
    if (!si.IsName("Attack"))

    {


    base.Damage(damageInfo);
    _animator.Play("Hit", 0, 0.25f);

    }

    }

    }
    public override void Die()

    {
    if (!enabled || !vp_Utility.IsActive(gameObject))

    return;


    if (m_Audio != null)

    {

    m_Audio.pitch = Time.timeScale;
    m_Audio.PlayOneShot(DeathSound);

    }

    _navMeshAgent.enabled = false;
    _animator.SetBool("IsFollow", false);
    _animator.SetBool("Attack", false);
    _animator.SetTrigger("Dying");
    Destroy(GetComponent<vp_SurfaceIdentifier>());
    }
    public void EndAttack()

    {
    float dist = Vector3.Distance(Player.transform.position, this.transform.position);

    if(dist < 3.0f)

    {
    Player.SendMessage("Damage", 4.0f, SendMessageOptions.DontRequireReceiver);
    }

    }

    }



    ERROR:

    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
    DamageHandler_Enemy:Update() (at Assets/DamageHandler_Enemy.cs:69)

    LINE 69:

    _navMeshAgent.SetDestination(Player.transform.position);
     
  2. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    It seems your eyes are bigger than stomach. There are Masses of informative lessons that can speed up your development time, try them out. A community example would be HERE.

    From the compiler error it seems you need to bake a navmesh on your scene. In the top tool bar navigate to Window/Navigation/Bake.
     
    juniorforestS likes this.
  3. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47