You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCControl : MonoBehaviour
{
public Vector3 birthPos;
public Transform pos;
Animator animator;
void OnEnable()
{
birthPos = transform.position;
animator = GetComponent<Animator>();
}
public void BackPos() {
transform.localEulerAngles += new Vector3(0,180,0);
StartCoroutine(MovePosByWalk(transform, birthPos, 2f));
}
public void StartWalk() {
StartCoroutine(MovePosByWalk(transform,pos.position,2f));
}
IEnumerator MovePosByWalk(Transform ts, Vector3 targetPos, float speed)
{
animator.SetBool("Walk", true);
while (true)
{
yield return new WaitForFixedUpdate();
Vector3 moveDir = (targetPos - ts.position).normalized;
ts.position += moveDir * Time.fixedDeltaTime * speed;
//Quaternion targetRotation = Quaternion.LookRotation(targetPos - ts.position);
//ts.rotation = Quaternion.Lerp(ts.rotation, targetRotation, 0.01f);
if (Vector3.Distance(ts.position, targetPos) <= 0.1)
{
ts.position = targetPos;
animator.SetBool("Walk", false);
yield break;
}
}
}
}