53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
![]() |
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
public class FeedingManager : MonoBehaviour
|
||
|
{
|
||
|
public static FeedingManager Instance; // Singleton
|
||
|
|
||
|
public Image appetiteBar;
|
||
|
public Transform moleTransform; // The capsule or model you want to rotate
|
||
|
public float rotateAmount = 30f;
|
||
|
public float shakeDuration = 0.5f;
|
||
|
|
||
|
private bool isShaking = false;
|
||
|
|
||
|
void Awake()
|
||
|
{
|
||
|
Instance = this;
|
||
|
}
|
||
|
|
||
|
public bool TryFeed()
|
||
|
{
|
||
|
if (appetiteBar.fillAmount >= 1f)
|
||
|
{
|
||
|
if (!isShaking) StartCoroutine(ShakeHead());
|
||
|
return false; // Reject the food
|
||
|
}
|
||
|
|
||
|
appetiteBar.fillAmount += 0.1f;
|
||
|
appetiteBar.fillAmount = Mathf.Min(appetiteBar.fillAmount, 1f);
|
||
|
return true; // Accept the food
|
||
|
}
|
||
|
|
||
|
private System.Collections.IEnumerator ShakeHead()
|
||
|
{
|
||
|
isShaking = true;
|
||
|
|
||
|
float timer = 0f;
|
||
|
Quaternion originalRotation = moleTransform.rotation;
|
||
|
|
||
|
while (timer < shakeDuration)
|
||
|
{
|
||
|
float angle = Mathf.Sin(timer * 20f) * rotateAmount;
|
||
|
moleTransform.rotation = originalRotation * Quaternion.Euler(0f, angle, 0f);
|
||
|
|
||
|
timer += Time.deltaTime;
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
moleTransform.rotation = originalRotation;
|
||
|
isShaking = false;
|
||
|
}
|
||
|
}
|