You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.1 KiB
C#
124 lines
3.1 KiB
C#
1 month ago
|
using UnityEngine;
|
||
|
|
||
|
public class CharacterMovement : MonoBehaviour
|
||
|
{
|
||
|
public enum MovementState { Walking, Waiting, PlayingAnimation, Finished }
|
||
|
public MovementState state = MovementState.Walking;
|
||
|
|
||
|
public Transform[] waypoints;
|
||
|
public float speed = 2f;
|
||
|
|
||
|
private int currentWaypoint = 0;
|
||
|
private Animator animator;
|
||
|
|
||
|
[Header("Camera Zoom")]
|
||
|
public CameraController cameraController;
|
||
|
|
||
|
private string currentAnimName = "";
|
||
|
private bool animStarted = false;
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
animator = GetComponentInChildren<Animator>();
|
||
|
|
||
|
if (waypoints.Length == 0)
|
||
|
{
|
||
|
Debug.LogError("Waypoints not assigned.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
animator.SetTrigger("StartWalking");
|
||
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
switch (state)
|
||
|
{
|
||
|
case MovementState.Walking:
|
||
|
MoveToWaypoint();
|
||
|
break;
|
||
|
|
||
|
case MovementState.PlayingAnimation:
|
||
|
WaitForAnimationToEnd();
|
||
|
break;
|
||
|
|
||
|
case MovementState.Waiting:
|
||
|
// Do nothing until told to resume
|
||
|
break;
|
||
|
|
||
|
case MovementState.Finished:
|
||
|
// All done
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MoveToWaypoint()
|
||
|
{
|
||
|
if (currentWaypoint >= waypoints.Length)
|
||
|
{
|
||
|
state = MovementState.Finished;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Transform target = waypoints[currentWaypoint];
|
||
|
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
|
||
|
transform.LookAt(target.position);
|
||
|
|
||
|
if (Vector3.Distance(transform.position, target.position) < 0.1f)
|
||
|
{
|
||
|
switch (currentWaypoint)
|
||
|
{
|
||
|
case 0:
|
||
|
PlayAnimation("OpenDoor");
|
||
|
break;
|
||
|
case 1:
|
||
|
PlayAnimation("StartWalking"); // walk inside again
|
||
|
state = MovementState.Walking;
|
||
|
break;
|
||
|
case 2:
|
||
|
PlayAnimation("SitDown");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
currentWaypoint++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PlayAnimation(string animName)
|
||
|
{
|
||
|
animator.SetTrigger(animName);
|
||
|
currentAnimName = animName;
|
||
|
animStarted = false;
|
||
|
state = MovementState.PlayingAnimation;
|
||
|
}
|
||
|
|
||
|
void WaitForAnimationToEnd()
|
||
|
{
|
||
|
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||
|
|
||
|
if (!animStarted)
|
||
|
{
|
||
|
if (stateInfo.IsName(currentAnimName))
|
||
|
{
|
||
|
animStarted = true;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (stateInfo.normalizedTime >= 1f && stateInfo.IsName(currentAnimName))
|
||
|
{
|
||
|
if (currentAnimName == "SitDown" && cameraController != null)
|
||
|
{
|
||
|
cameraController.StartZoom();
|
||
|
state = MovementState.Finished;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
animator.SetTrigger("StartWalking");
|
||
|
state = MovementState.Walking;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|