173 lines
5.0 KiB
C#
Raw Normal View History

2025-05-27 17:01:22 +05:00
using UnityEngine;
using DG.Tweening;
using UnityEngine.Animations;
2025-05-23 20:37:10 +05:00
public class CharacterMovement : MonoBehaviour
{
2025-05-27 17:01:22 +05:00
public enum MovementState { Walking, RotatingToSit, PlayingAnimation, Finished }
2025-05-23 20:37:10 +05:00
public MovementState state = MovementState.Walking;
2025-05-27 17:01:22 +05:00
[Header("Waypoints")]
2025-05-23 20:37:10 +05:00
public Transform[] waypoints;
private int currentWaypoint = 0;
2025-05-27 17:01:22 +05:00
[Header("References")]
public GameObject rigWithAnimator;
public Animator animator;
2025-05-23 20:37:10 +05:00
public CameraController cameraController;
2025-05-27 17:01:22 +05:00
public CameraHeadBobbing cameraHeadBobbing;
public Transform mainCamera; // Camera.main.transform
public Transform cameraSitTarget; // Dummy camera transform
2025-05-23 20:37:10 +05:00
2025-05-27 17:01:22 +05:00
[Header("Settings")]
public float moveSpeed = 2f;
public float rotationSpeed = 5f;
2025-05-23 20:37:10 +05:00
private string currentAnimName = "";
private bool animStarted = false;
2025-05-27 17:01:22 +05:00
private bool cameraTransitionStarted = false;
private Quaternion targetSitRotation;
2025-05-31 05:14:48 +05:00
bool isStarted = false;
2025-05-23 20:37:10 +05:00
void Start()
{
2025-05-27 17:01:22 +05:00
if (waypoints == null || waypoints.Length == 0)
{
Debug.LogError("No waypoints assigned.");
enabled = false;
return;
}
if (animator == null && rigWithAnimator != null)
{
animator = rigWithAnimator.GetComponentInChildren<Animator>();
}
2025-05-23 20:37:10 +05:00
2025-05-27 17:01:22 +05:00
if (animator == null)
2025-05-23 20:37:10 +05:00
{
2025-05-27 17:01:22 +05:00
Debug.LogError("Animator not found.");
enabled = false;
2025-05-23 20:37:10 +05:00
return;
}
2025-05-27 17:01:22 +05:00
animator.applyRootMotion = false;
2025-05-31 05:14:48 +05:00
}
public void AnimationStarter()
{
2025-05-23 20:37:10 +05:00
animator.SetTrigger("StartWalking");
2025-05-31 05:14:48 +05:00
isStarted = true;
2025-06-05 00:11:38 +05:00
InstructionManager.Instance?.ShowScreenInstruction("mission_intro");
2025-05-23 20:37:10 +05:00
}
void Update()
{
2025-05-31 05:14:48 +05:00
if (isStarted)
2025-05-23 20:37:10 +05:00
{
2025-05-31 05:14:48 +05:00
switch (state)
{
case MovementState.Walking:
MoveToWaypoint();
break;
2025-05-23 20:37:10 +05:00
2025-05-31 05:14:48 +05:00
case MovementState.RotatingToSit:
RotateToSit();
break;
2025-05-23 20:37:10 +05:00
2025-05-31 05:14:48 +05:00
case MovementState.PlayingAnimation:
WaitForAnimationToEnd();
break;
2025-05-23 20:37:10 +05:00
2025-05-31 05:14:48 +05:00
case MovementState.Finished:
break;
}
2025-05-23 20:37:10 +05:00
}
2025-05-31 05:14:48 +05:00
2025-05-23 20:37:10 +05:00
}
void MoveToWaypoint()
{
if (currentWaypoint >= waypoints.Length)
{
state = MovementState.Finished;
return;
}
Transform target = waypoints[currentWaypoint];
2025-05-27 17:01:22 +05:00
Vector3 direction = target.position - transform.position;
direction.y = 0;
if (direction.magnitude > 0.05f)
{
transform.position += direction.normalized * moveSpeed * Time.deltaTime;
2025-05-23 20:37:10 +05:00
2025-05-27 17:01:22 +05:00
Quaternion lookRot = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, rotationSpeed * Time.deltaTime);
}
else
2025-05-23 20:37:10 +05:00
{
2025-05-27 17:01:22 +05:00
if (currentWaypoint == waypoints.Length - 1)
2025-05-23 20:37:10 +05:00
{
2025-05-27 17:01:22 +05:00
// Prepare to sit
transform.position = target.position;
targetSitRotation = target.rotation;
state = MovementState.RotatingToSit;
2025-05-23 20:37:10 +05:00
}
currentWaypoint++;
}
}
2025-05-27 17:01:22 +05:00
void RotateToSit()
{
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
targetSitRotation,
300f * Time.deltaTime
);
float angle = Quaternion.Angle(transform.rotation, targetSitRotation);
if (angle < 2f)
{
transform.rotation = targetSitRotation;
animator.applyRootMotion = true;
PlayAnimation("SitDown");
// Disable camera bobbing
if (cameraHeadBobbing != null)
cameraHeadBobbing.enabled = false;
state = MovementState.PlayingAnimation;
}
}
2025-05-23 20:37:10 +05:00
void PlayAnimation(string animName)
{
animator.SetTrigger(animName);
currentAnimName = animName;
animStarted = false;
}
void WaitForAnimationToEnd()
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
2025-05-27 17:01:22 +05:00
if (!animStarted && stateInfo.IsName(currentAnimName))
2025-05-23 20:37:10 +05:00
{
2025-05-27 17:01:22 +05:00
animStarted = true;
2025-05-23 20:37:10 +05:00
}
2025-05-27 17:01:22 +05:00
else if (animStarted && stateInfo.normalizedTime >= 1f && stateInfo.IsName(currentAnimName))
2025-05-23 20:37:10 +05:00
{
2025-05-27 17:01:22 +05:00
if (currentAnimName == "SitDown" && !cameraTransitionStarted && mainCamera != null && cameraSitTarget != null)
2025-05-23 20:37:10 +05:00
{
2025-05-27 17:01:22 +05:00
cameraTransitionStarted = true;
// Move and rotate camera to final cinematic zoom position
2025-05-31 05:14:48 +05:00
mainCamera.GetComponent<LookAtConstraint>().constraintActive = false;
2025-05-27 17:01:22 +05:00
mainCamera.DOMove(cameraSitTarget.position, 1f);
mainCamera.DORotate(cameraSitTarget.eulerAngles, 1f)
.OnComplete(() =>
{
state = MovementState.Finished;
2025-06-05 00:11:38 +05:00
InstructionManager.Instance?.ShowScreenInstruction("click_to_open");
2025-05-27 17:01:22 +05:00
});
2025-05-23 20:37:10 +05:00
}
}
}
}