Unity3D NavMeshAgent for abilities



我一直在努力为我的Unity3D游戏创建基于鼠标的运动。我已经获得了移动,但我在设置移动速度变量时遇到了问题,我可以使用技能来修改。这是我第一次尝试使用c#,我非常缺乏经验。这是一个长期项目的开始,所以任何帮助或建议将非常感激。

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); 
}
} 
}

有时候Unity文档有你需要的所有答案。NavMeshAgent-Speed

尝试使用agent.speed。创建一个全局整数"public int sp=1;"然后在Start或Update do

agent.speed=sp;

一旦它工作了,你就可以在任何你想要的方法中改变这个整数的值。祝你好运

最新更新