-2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Enemy : MonoBehaviour
{

    public NavMeshAgent Ninja;
    public GameObject Player;
    public float NinjaDistanceRun = 30.0f;

    void Start()
    {
        Ninja = GetComponent<NavMeshAgent>();
    }


    void Update()
    {
        float distance = Vector3.Distance(transform.position, Player.transform.position);

        //Run towards player

        if(distance < NinjaDistanceRun)
        {
            Vector3 dirToPlayer = transform.position - Player.transform.position;

            Vector3 newPos = transform.position - dirToPlayer;

            Ninja.SetDestination(newPos);
        }
    }


}

The code shown above is what I use to make the enemy follow the player when in range. Can I make my enemy go faster without trashing this whole script and making a new one that has a different line of thought?

2 Answers2

0

Set the maximum speed for your ninja(NavMeshAgent.speed). Here is the documentation for more information: https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-speed.html

0

In the START method you can set a new speed to your Navmesh agent, and this character will walk or run in the player direction.

InfJ
  • 43
  • 1
  • 5