Needs script trigger animation?
Hello,
I'm building a pet simulator based off of the Chao Garden from the Sonic games, and right now I'm working on scripting a needs system that will trigger an animation on the pet once the need is empty. This is what I have so far:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ChaoNeeds : MonoBehaviour { public Animator anim; public Slider happinessSlider; public int maxHappiness; public int hapFallRate;
public Slider sleepSlider;
public int maxSleep;
public int slpFallRate;
public Slider hungerSlider;
public int maxHunger;
public int hgrFallRate;
private float haplow;
private float slplow;
private float hgrlow;
private bool run;
void Start()
{
anim = GetComponent <Animator>();
happinessSlider.maxValue = maxHappiness;
happinessSlider.value = maxHappiness;
sleepSlider.maxValue = maxSleep;
sleepSlider.value = maxSleep;
hungerSlider.maxValue = maxHunger;
hungerSlider.value = maxHunger;
run = false;
}
void Update()
{
//HAPPINESS CONTROLLER
if(hungerSlider.value <= 0 && (sleepSlider.value <= 0))
{
happinessSlider.value -= Time.deltaTime / hapFallRate * 2;
}
else if(hungerSlider.value <= 5 || sleepSlider.value <= 5)
{
happinessSlider.value -= Time.deltaTime / hapFallRate * 2;
}
if(happinessSlider.value <= 0)
{
anim.Play("Crying");
}
//HUNGER CONTROLLER
if (hungerSlider.value >= 0)
{
hungerSlider.value -= Time.deltaTime / hgrFallRate;
}
else if (hungerSlider.value <= 0)
{
hungerSlider.value = 0;
anim.Play("Crying");
}
else if (hungerSlider.value >= maxHunger)
{
hungerSlider.value = maxHunger;
}
//SLEEP CONTROLLER
if (sleepSlider.value >= 0)
{
sleepSlider.value -= Time.deltaTime / slpFallRate;
}
else if (sleepSlider.value <= 0)
{
sleepSlider.value = 0;
}
else if (sleepSlider.value >= maxSleep)
{
sleepSlider.value = maxSleep;
}
}
void ChaoFoodSearch ()
{
//CHAO WILL BEGIN TO EAT FRUIT ONCE WITHIN IT'S RANGE
}
}
The needs system is working and the pet needs are degrading as they should be. Where I've hit a wall is getting the animations to play once the need reaches a certain point. Part of the reason is because I'm not entirely sure how to set up my animator for something like this. This is the animator currently:
If anyone thinks they can be of assistance it would really help me out a lot.
Answer by UnityCoach · Jan 23, 2017 at 09:52 PM
First, you need to add Animation parameters to your Animator Controller, and set those parameters as conditions for your transitions. Then, through code, you simply use SetTrigger to trigger your transitions. Note, you can also use Boolean, or Float, or Int parameters.
Thanks. I actually found a tutorial on this after an hour or so and got it working using Boolean,
Your answer
Follow this Question
Related Questions
How to know the animator's bones 0 Answers
Script Variable not transfering 0 Answers
RayCast and Animation not working 0 Answers