I know we should get inputs in Update and do physics stuff in FixedUpdate.but it’s my code I don’t Know that I should put it in Update or FixedUpdate.
public void RunAnimation(){
animator.SetFloat ("Speed",Mathf.Abs( GetComponent<Rigidbody2D>().velocity.x));
}
It’s better to use Update. You should avoid FixedUpdate as much as possible. If you have a framerate drop for some reason it doesn’t help when you have many heavy things inside FixedUpdate. Animations are mainly a visual thing so it’s best to have it updated with the visual update rate. Even when the velocity occasionally does not change between two Update calls it won’t really hurt. Though at a low frame rate you will get several FixedUpdate calls per Update call. However updating the animation settings several times during one visual update makes no sense.
FixedUpdate should only be used for continuously added forces. Applying a single impulse force (like a jump) can and should be done in Update instead. Almost everything can be done in Update. FixedUpdate is only used in some rare exceptions that involve phyics.
Your question title asks where to check animator conditions. You should do that in LateUpdate(), since that occurs after the internal animation update in each loop. See Unity - Manual: Order of execution for event functions for more info.
If you need to start an animation based on something that occurs within FixedUpdate() which I need for various reasons, a good approach is to just create an animation control class like this I reckon:
public class AnimationController : MonoBehaviour
{
[SerializeField]
private Animator _animator;
private bool _isAwaiting = false;
private int _triggerCount;
private List<(string name, bool value)> _bools = new List<(string name, bool value)>();
private List<(string name, float value)> _floats = new List<(string name, float value)>();
private List<string> _triggers = new List<string>();
public void SetAnim(string name, bool value)
{
if (_bools.FindIndex(x => x.name == name) == -1)
_bools.Add((name, value));
if (!_isAwaiting)
StartCoroutine(RunAnimations());
}
public void SetAnim(string name, float value)
{
if (_floats.FindIndex(x => x.name == name) == -1)
_floats.Add((name, value));
if (!_isAwaiting)
StartCoroutine(RunAnimations());
}
public void SetAnim(string name)
{
if (_triggers.IndexOf(name) == -1)
_triggers.Add(name);
if (!_isAwaiting)
StartCoroutine(RunAnimations());
}
private IEnumerator RunAnimations()
{
_isAwaiting = true;
yield return new WaitForEndOfFrame();
_bools.ForEach(x => _animator.SetBool(x.name, x.value));
_floats.ForEach(x => _animator.SetFloat(x.name, x.value));
_triggers.ForEach(x => _animator.SetTrigger(x));
_bools.Clear();
_floats.Clear();
_triggerCount = _triggers.Count;
_isAwaiting = false;
yield return new WaitForEndOfFrame();
if (_triggerCount != 0 && _triggerCount <= _triggers.Count)
{
for (int i = 0; i < _triggerCount; i++)
{
_animator.ResetTrigger(_triggers*);*
}
_triggers.RemoveRange(0, _triggerCount);
_triggerCount = 0;
}
}
}
And then just call SetAnim for the relevant animations…
yes,you’re right, Thanks.