Files
ClickRPG/Assets/Scripts/Player.cs
2025-10-06 15:32:09 +01:00

47 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : Character
{
[SerializeField] private float attackRange;
[SerializeField] private float attackRate;
private float lastAttackTime;
[SerializeField] private GameObject attackPrefab;
public static Player Current;
private Animator animator;
void Awake()
{
Current = this;
animator = GetComponentInChildren<Animator>();
}
void Update()
{
if (target != null)
{
float targetDistance = Vector3.Distance(transform.position, target.transform.position);
if (targetDistance < attackRange)
{
Controller.StopMovement();
Controller.LookTowards(target.transform.position - transform.position);
if (Time.time - lastAttackTime > attackRate && target.GetComponent<Enemy>() != null)
{
animator.SetTrigger("attack");
lastAttackTime = Time.time;
GameObject proj = Instantiate(attackPrefab, target.transform.position + Vector3.up, Quaternion.LookRotation(target.transform.position - transform.position));
proj.GetComponent<Projectile>().Setup(this);
proj.GetComponent<Projectile>().DamageTarget(target);
}
}
else
{
Controller.MoveToTarget(target.transform);
}
}
}
}