using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
private float speed = 1;
private Vector3 targetPosition;
private bool isMoveStart = false;
private bool isMoveComplete = false;
public float attackRange;
public int hp;
public int damage;
Animation anim;
//private Animation anim;
private void Awake()
{
anim = this.gameObject.GetComponent<Animation>();
}
// Start is called before the first frame update
void Start()
{
}
public void Init(Vector3 initPosition)
{
this.gameObject.transform.position = initPosition;
}
public void Move(Vector3 targetPosition)
{
this.isMoveStart = true;
this.targetPosition = targetPosition;
Debug.Log("이동시작");
//run@loop 애니메이션 시작
this.anim.Play("run@loop");
//방향바라보기
this.gameObject.transform.LookAt(this.targetPosition);
}
public void TakeDamage(int damage)
{
this.hp -= damage;
}
public void AttackMove()
{
var distance = Vector3.Distance(this.gameObject.transform.position, targetPosition);
if (this.attackRange > distance)
{
this.anim.Play("attack_sword_01");
this.isMoveComplete = true;
}
}
// Update is called once per frame
void Update()
{
if (this.isMoveStart == true)
{
if (this.isMoveComplete == false)
{
this.AttackMove();
var dir = (this.targetPosition - this.gameObject.transform.position).normalized;
this.gameObject.transform.position += dir * this.speed * Time.deltaTime;
var distance = Vector3.Distance(this.targetPosition, this.transform.position);
if (distance <= 0.1f)
{
Debug.Log("이동완료");
isMoveStart = false;
this.anim.Play("idle@loop");
}
}
else
{
this.anim.Play("attack_sword_01");
}
}
}
}