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.
100 lines
3.4 KiB
100 lines
3.4 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using XWFramework.UI;
|
|
namespace Level1
|
|
{
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
Animator animator;
|
|
public Transform endWalkPos;
|
|
public Transform endRunPos;
|
|
public Transform man;
|
|
Queue<IEnumerator> queue = new Queue<IEnumerator>();
|
|
bool finishFlag = true;
|
|
private void Awake()
|
|
{
|
|
animator = GetComponentInChildren<Animator>();
|
|
GameManager.StartGame += WalkAndSquat;
|
|
}
|
|
public void WalkAndSquat()
|
|
{
|
|
queue.Enqueue(MovePosByWalk(transform, endWalkPos.position, 2));
|
|
queue.Enqueue(MovePosByRun(transform, endRunPos.position, 3f));
|
|
queue.Enqueue(Squat(transform, man));
|
|
StartCoroutine(StartQueue());
|
|
}
|
|
|
|
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);
|
|
finishFlag = true;
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator MovePosByRun(Transform ts, Vector3 targetPos, float speed)
|
|
{
|
|
animator.SetBool("Run", 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.02f);
|
|
if (Vector3.Distance(ts.position, targetPos) <= 0.1)
|
|
{
|
|
ts.position = targetPos;
|
|
animator.SetBool("Run", false);
|
|
finishFlag = true;
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator Squat(Transform my, Transform ts)
|
|
{
|
|
transform.position = endRunPos.position;
|
|
transform.localEulerAngles = new Vector3(0, 270, 0);
|
|
animator.SetTrigger("Squat");
|
|
transform.position -= Vector3.up * 0.2f;
|
|
finishFlag = true;
|
|
yield return null;
|
|
}
|
|
|
|
|
|
|
|
IEnumerator StartQueue()
|
|
{
|
|
while (true)
|
|
{
|
|
if (queue.Count!=0)
|
|
{
|
|
finishFlag = false;
|
|
StartCoroutine(queue.Dequeue());
|
|
yield return null;
|
|
}
|
|
else
|
|
{
|
|
GameManager.Instance.PlayerArrive();
|
|
yield break;
|
|
}
|
|
yield return new WaitUntil(() => { return finishFlag; });
|
|
}
|
|
}
|
|
}
|
|
}
|