46 lines
1016 B
C#
46 lines
1016 B
C#
using Mirror;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerMovement : NetworkBehaviour
|
|
{
|
|
[SerializeField] private NavMeshAgent _navMeshAgent;
|
|
|
|
private Camera _camera;
|
|
private Mouse _mouse;
|
|
|
|
#region Server
|
|
|
|
[Command] private void CmdMove(Vector3 destination)
|
|
{
|
|
_navMeshAgent.SetDestination(destination);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Client
|
|
|
|
[Client]
|
|
private void OnEnable()
|
|
{
|
|
_navMeshAgent = GetComponent<NavMeshAgent>();
|
|
_camera = FindObjectOfType<Camera>();
|
|
_mouse = Mouse.current;
|
|
}
|
|
|
|
[Client]
|
|
private void Update()
|
|
{
|
|
if (_mouse.leftButton.wasPressedThisFrame)
|
|
{
|
|
Ray ray = _camera.ScreenPointToRay(new(_mouse.position.x.value, _mouse.position.y.value, 0));
|
|
if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, Mathf.Infinity))
|
|
{
|
|
CmdMove(hit.point);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |