64 lines
1.5 KiB
C#
64 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class ClickToMove : MonoBehaviour
|
|
{
|
|
private Camera cam;
|
|
private NavMeshAgent agent;
|
|
private Animator anim;
|
|
|
|
public Vector3 CurrentVelocity;
|
|
public float CurrentVelocityMagnitude;
|
|
|
|
public bool leftPressed = false;
|
|
public bool casting = false;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
agent = GetComponent<NavMeshAgent>();
|
|
cam = Camera.main;
|
|
anim = GetComponentInChildren<Animator>();
|
|
|
|
InputHandler.instance.LeftMouseButtonPressed.AddListener(ToggleMouseState);
|
|
InputHandler.instance.LeftMouseButtonReleased.AddListener(ToggleMouseState);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (leftPressed)
|
|
{
|
|
MoveToClickedPoint();
|
|
}
|
|
CurrentVelocity = agent.velocity;
|
|
CurrentVelocityMagnitude = agent.velocity.magnitude;
|
|
anim.SetFloat("VelocityMagnitude", CurrentVelocityMagnitude);
|
|
}
|
|
|
|
public void ToggleMouseState()
|
|
{
|
|
leftPressed = !leftPressed;
|
|
}
|
|
|
|
public void MoveToClickedPoint()
|
|
{
|
|
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(ray, out hit, 100))
|
|
{
|
|
agent.SetDestination(hit.point);
|
|
}
|
|
}
|
|
|
|
public void ForceStop()
|
|
{
|
|
agent.isStopped = true;
|
|
}
|
|
public void RestartAgentMovement()
|
|
{
|
|
agent.isStopped = false;
|
|
}
|
|
}
|