Unity3D NavMeshAgent for abilities

Viewed 20

I have been working to create mouse-based movement for my Unity3D game. I have gotten the movement, but I am having trouble setting of a variable for move speed that I can modify using a skill. This is my first attempt at using C# and I am very inexperienced. This is the beginnings of a long project, so any help or advice would be greatly appreciated.

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.AI;

namespace EO.ARPGInput
{
    [RequireComponent(typeof(NavMeshAgent))]

  public class PlayerController : MonoBehaviour
{
  
    public Vector3 velocity;

    [SerializeField] private InputAction movement = new InputAction();

    [SerializeField] public LayerMask layerMask = new LayerMask();

      private NavMeshAgent agent = null;
     private Camera cam = null;


     private void Start()
   {
        
        cam = Camera.main;
        agent = GetComponent<NavMeshAgent>();
   }
   
    private void OnEnable()
    {
        movement.Enable();
    }

    private void OnDisable()
    {
        movement.Disable();
    }
    
    private void Update()
    {
        HandleInput();
    }
    
    public void HandleInput()
    {
        if (movement.ReadValue<float>() == 1)
        {
            Ray ray = cam.ScreenPointToRay(Mouse.current.position.ReadValue());
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100, layerMask))
        
            {
                PlayerMove(hit.point);
            }
        }
    }
    public void PlayerMove(Vector3 location)
    {
       agent.SetDestination(location); 
    }
} 
}
1 Answers

Sometimes Unity docs have all the answers you need. NavMeshAgent-Speed

Try using agent.speed. create a global integer "public int sp=1;" above and then in Start or in Update do

agent.speed=sp;

Then once it is working you can just change that integer's value in whatever method you want. Good luck

Related