简体   繁体   中英

I can't get the enemy to follow the player in unity

The enemy goes to the player at first then stops and doesn't move. The chasePlayer function is being run every frame so the enemy should update its destination every frame but this is not happening, it does it in the first instance then when it reached the first destination stops an doesn't go to the player anymore. How can I fix this?

public class Slime : MonoBehaviour, IEnemy {
    public Transform enemyTarget;
    public float maxHealth, power, toughness;
    public float currentHealth;

    private NavMeshAgent navAgent;
    private Player player;

    void Start() {
        navAgent = GetComponent<NavMeshAgent> ();
        currentHealth = maxHealth;
    }

    void Update() {
        ChasePlayer();
    }

//Makes enemy chases player
    void ChasePlayer() {
        this.player = player;
        navAgent.SetDestination(enemyTarget.position);
        Debug.Log ("Chasing player");
    }
}

As per your comment:

it does it in the first instance then when it reached the first destination stops an doesn't go to the player anymore

It seems to me what is not refreshed is the player position. So the enemy only checks the position of the player when it awakes and just move to that spot, then it stop rechecking.

Try the following and let us know how it goes. The only difference is instead of linking the player in the editor, it is linked by code inside the awake. I was using it myself and it was working fine.

using UnityEngine;
using System.Collections;

public class Slime: MonoBehaviour
{
    Transform player;        // Ref to the player's position.
    NavMeshAgent nav;        // Ref to the nav mesh agent.

    void Awake ()
    {
        // Set up the references.
        player = GameObject.FindGameObjectWithTag ("Player").transform;
        nav = GetComponent <NavMeshAgent> ();
    }


    void Update ()
    {
        //Here it would be nice to add a stop condition, like when the player is dead or when it is out of range
        ChasePlayer();

    } 

    void ChasePlayer() {
        nav.SetDestination (player.position);
        Debug.Log ("Chasing player");
    }
}

I used the following tutorial as a reference:

https://unity3d.com/learn/tutorials/projects/survival-shooter/enemy-one?playlist=17144

From the comments in the question I see originally you were trying to set an action range which will trigger the enemy. Below I give you a possible approach to calculate the distance between two GameObjects, which you can add in the Update():

float dist = Vector3.Distance(player.position, transform.position)
if(dist < 10.0)
{
        ChasePlayer();
}

As a reference: https://docs.unity3d.com/ScriptReference/Vector3.Distance.html

Look into following transform of enemyTarget. See documentation here: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

Note that you can also use the same method using Vector2 for 2D follow.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // The target marker.
    public Transform target;

    // Speed in units per sec.
    public float speed;

    void Update()
    {
        // The step size is equal to speed times frame time.
        float step = speed * Time.deltaTime;

        // Move our position a step closer to the target.
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
}

I fixed the issue. The reason i had this issue was because the model for the player was a child to the player game object which didn't move only the model did, i have since changed it so the whole player moves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM