Files
ScienceLab.TypesOfMotion/Assets/Scripts/GirlExercise.cs
2026-02-18 00:08:49 +02:00

74 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GirlExercise : MonoBehaviour
{
[Header("Àí³ìàö³¿")]
[SerializeField] private string idleAnimation = "idle_selfcheck_1_300f";
[SerializeField] private string exerciseAnimation = "exercise_warmingUp_170f";
private Animator animator;
private AnimationClip[] animationClips;
private float currentAnimationLength;
private float animationTimer;
private bool isPlaying = false;
void Start()
{
animator = GetComponent<Animator>();
if (animator != null)
{
animationClips = animator.runtimeAnimatorController.animationClips;
PlayRandomAnimation();
}
}
void Update()
{
if (isPlaying)
{
animationTimer += Time.deltaTime;
if (animationTimer >= currentAnimationLength)
{
PlayRandomAnimation();
}
}
}
private void PlayRandomAnimation()
{
string selectedAnimation;
if (Random.value > 0.5f)
{
selectedAnimation = idleAnimation;
}
else
{
selectedAnimation = exerciseAnimation;
}
animator.Play(selectedAnimation);
currentAnimationLength = GetAnimationLength(selectedAnimation);
animationTimer = 0f;
isPlaying = true;
}
private float GetAnimationLength(string animationName)
{
foreach (AnimationClip clip in animationClips)
{
if (clip.name == animationName)
{
return clip.length;
}
}
return 5f;
}
}