Files
ClickRPG/Assets/Scripts/CharacterController.cs

57 lines
1.4 KiB
C#
Raw Normal View History

2025-10-04 12:25:29 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class CharacterController : MonoBehaviour
{
private float moveToUpdateRate = 0.1f;
private float lastMoveToUpdate;
private Transform moveTarget;
private NavMeshAgent agent;
private Animator animator;
void Awake ()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponentInChildren<Animator>();
}
void Update ()
{
if (moveTarget != null && Time.time - lastMoveToUpdate > moveToUpdateRate)
{
2025-10-06 15:32:09 +01:00
animator.ResetTrigger("attack");
2025-10-04 12:25:29 +01:00
lastMoveToUpdate = Time.time;
MoveToPosition(moveTarget.position);
}
2025-10-06 15:32:09 +01:00
if (animator != null)
{
float speed = agent.velocity.magnitude;
float normalisedSpeed = speed / agent.speed;
animator.SetFloat("moveSpeed", normalisedSpeed);
}
2025-10-04 12:25:29 +01:00
}
public void LookTowards (Vector3 direction)
{
transform.rotation = Quaternion.LookRotation(direction);
}
public void MoveToTarget (Transform target)
{
moveTarget = target;
}
public void MoveToPosition (Vector3 position)
{
agent.SetDestination(position);
agent.isStopped = false;
}
public void StopMovement ()
{
agent.isStopped = true;
moveTarget = null;
}
}