Files
jongjae0305 d8aabbd428 [Update] 에셋 및 애니메이션
1. 캐릭터, 배경 에셋 변경
2. 캐릭터 애니메이션 적용
3. 방향키에 따른 캐릭터 회전 적용
2026-04-15 12:59:37 +09:00

57 lines
2.0 KiB
C#

using UnityEngine;
namespace SazenGames.Skeleton
{
/// <summary>
/// This script replays a specified animation state on an Animator component at regular intervals.
/// It also provides an option to reset the GameObject's position before each replay.
/// </summary>
[RequireComponent(typeof(Animator))]
public class AnimationReplayer : MonoBehaviour
{
[Header("Animation Settings")]
[SerializeField] private string animationStateName = "YourAnimationState"; // Replace with your actual animation state name
[SerializeField] private float replayDelay = 2f; // Delay in seconds before replaying
[Header("Reset Settings")]
[SerializeField] private bool resetPositionOnReplay = false; // Toggle to enable/disable position reset before each replay
private Animator animator;
private Vector3 initialPosition;
void Start()
{
animator = GetComponent<Animator>();
if (animator == null)
{
Debug.LogError("Animator component not found on this GameObject!");
return;
}
initialPosition = transform.position; // Store the initial position
// Start the first play
PlayAnimation();
}
private void PlayAnimation()
{
// Reset position to initial if enabled
if (resetPositionOnReplay)
{
transform.position = initialPosition;
}
animator.Play(animationStateName, 0, 0f); // Play the specified state in layer 0 from normalized time 0
// Schedule the next replay after the delay
Invoke(nameof(PlayAnimation), replayDelay);
}
// Public method to manually reset position (e.g., call from another script or UI button)
public void ResetPosition()
{
transform.position = initialPosition;
}
}
}