45 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using UnityEngine;
 | |
| using System.Collections.Generic;
 | |
| 
 | |
| public class CivilianIdleManager : MonoBehaviour
 | |
| {
 | |
|     [Tooltip("Idle clips to randomly assign")]
 | |
|     public List<AnimationClip> idleClips = new List<AnimationClip>();
 | |
| 
 | |
|     [Tooltip("How often to switch to a new random idle (seconds)")]
 | |
|     public Vector2 changeInterval = new Vector2(5f, 10f);
 | |
| 
 | |
|     private Animator[] civilians;
 | |
| 
 | |
|     void Start()
 | |
|     {
 | |
|         civilians = GetComponentsInChildren<Animator>();
 | |
| 
 | |
|         foreach (var civ in civilians)
 | |
|         {
 | |
|             if (civ == null) continue;
 | |
|             StartCoroutine(ChangeIdleRoutine(civ));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private System.Collections.IEnumerator ChangeIdleRoutine(Animator civ)
 | |
|     {
 | |
|         AnimatorOverrideController overrideController = new AnimatorOverrideController(civ.runtimeAnimatorController);
 | |
|         civ.runtimeAnimatorController = overrideController;
 | |
| 
 | |
|         while (true)
 | |
|         {
 | |
|             if (idleClips.Count > 0)
 | |
|             {
 | |
|                 // Pick a random idle clip
 | |
|                 AnimationClip chosen = idleClips[Random.Range(0, idleClips.Count)];
 | |
| 
 | |
|                 // Replace the default state animation with this clip
 | |
|                 overrideController[overrideController.animationClips[0].name] = chosen;
 | |
|             }
 | |
| 
 | |
|             float wait = Random.Range(changeInterval.x, changeInterval.y);
 | |
|             yield return new WaitForSeconds(wait);
 | |
|         }
 | |
|     }
 | |
| } |