- Home /
How can i access blend tree in animator controller from script ?
It's not my own animator controller. And when i'm getting to the blend tree i don't see any parameters for it or/and variables to use from a script.
When i make double click on the Grounded i'm getting to this:
For example i want to make a combination of walking and idle. So if i press on the key W the character will walk and then when i leave the W key it will slowly change to idle. And same from idle to walk.
In my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CC : MonoBehaviour
{
private Animator _anim;
private void Start()
{
_anim = GetComponent<Animator>();
}
void Update()
{
var x = Input.GetAxis("Horizontal");
var y = Input.GetAxis("Vertical");
Move(x, y);
}
private void Move(float x, float y)
{
_anim.SetFloat("", x);
_anim.SetFloat("", y);
}
}
Do i need to add on my own some parameters to this blend tree like in the tutorial in minute 4:03 ?
I clicked on Parameters and see them now:
So the script part in the $$anonymous$$ove method is like this:
private void $$anonymous$$ove(float x, float y)
{
_anim.SetFloat("X", x);
_anim.SetFloat("Y", y);
}
But when running the game and using W,S,A,D it does nothing.
And then what i want to do later is when i press on W for walking if i leave the W key that it will slowly smooth change from walking to idle animation. Same from idle to walk.
Or maybe i should make my own animator controller from zero like in the tutorial ?
Answer by JaviTheGreat · May 22, 2020 at 01:02 AM
Yes, you access a BlendTree inside an AnimatorController.
Let's say you just have a state with a blend tree on the first layer. Then if you have access to the AnimatorController, then...
You can access the root stateMachine of the a layer like:
var rootStateMachine = controller.layers[0].stateMachine
Then you access an AnimationState like:
var stateWithBlendTree = rootStateMachine.states[0].state
Finally, you can access the blend tree like:
var blendTree = (BlendTree)stateWithBlendTree.motion;
can you please add me in discord and tell me more about this : Pads#0443
It's a absolutely worked solution, based on it I get the motion parameters for Player controller script for "Blend Tree 1D with two motion"
//Motion "Walk"
MaximumWalkSpeed = blendTree.children[0].threshold;
Debug.Log($"{blendTree.children[0].motion.name} - threshold={MaximumWalkSpeed}");
//Motion "Run"
MaximumRunSpeed = blendTree.children[1].threshold;
Debug.Log($"{blendTree.children[1].motion.name} - threshold={MaximumRunSpeed}");
P.S> For receiving the AnimatorController from Animator I use the following code
void GetWalkRunSpeedFromBlendTree(Animator usedAnimator)
{
RuntimeAnimatorController runtimeController = usedAnimator.runtimeAnimatorController;
if (runtimeController == null)
{
Debug.LogErrorFormat("RuntimeAnimatorController must not be null.");
return;
}
AnimatorController assetAnimatorController =UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.Animations.AnimatorController>(UnityEditor.AssetDatabase.GetAssetPath(runtimeController));
if (assetAnimatorController == null)
{
Debug.LogErrorFormat("AnimatorController must not be null.");
return;
}
//... other code
}
Answer by cgarossi · Sep 01, 2017 at 07:34 AM
You don't access blend trees directly. You adjust parameters on the Animator controller (as you would normally using _anim.SetFloat() etc) and the blend tree does the rest.
The blend tree blends animations according to a parameter value.
I asked this since this because the answer i got here: http://answers.unity3d.com/questions/1401100/how-can-i-switch-smooth-between-humanoidwalk-and-h.html
Answer by NatCou · Mar 27, 2019 at 06:40 PM
Maybe you could try blending the animation? https://docs.unity3d.com/ScriptReference/Animation.Blend.html
if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("TallPerson"))
{
personAnimation.Blend(anmName, targetweight, fadeLength);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Blend Tree Animation Problem 1 Answer
Running a script without being attached to an object? 2 Answers