Question by
Vrzasq · Jun 05, 2020 at 09:26 AM ·
animationscripting problemanimatortransitionscondition
How to modify animator transition conditions via script
Hi,
What I woulld like to achive its to create a script which will modify animator transition condition via script. Currently I have came with this:
using UnityEditor.Animations;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class AnimationController : MonoBehaviour
{
public float walkTreshold = 0.1f;
public float runTreshold = 2.0f;
void OnValidate()
{
var animator = GetComponent<Animator>();
var animatorController = animator.runtimeAnimatorController as AnimatorController;
if (animatorController == null)
return;
var baseLayer = animatorController.layers[0];
for (int i = 0; i < baseLayer.stateMachine.states.Length; i++)
{
for (int j = 0; j < baseLayer.stateMachine.states[i].state.transitions.Length; j++)
{
if (baseLayer.stateMachine.states[i].state.transitions[j].name == "run")
baseLayer.stateMachine.states[i].state.transitions[j].conditions[0].threshold = runTreshold;
}
}
}
}
Animator looks like this:
Script is attached to game object and debugger enter if statment. Unfortunetly value does not change. After 4h trials and errors I can't figure why.
capture.png
(132.2 kB)
Comment
Your answer
Follow this Question
Related Questions
Animator Position VS Script Vector 3 0 Answers
Animation not consistent 0 Answers
Animator displaying wrong states 1 Answer
Help please, movement and animator issue 0 Answers
How to stop previous animation? 1 Answer