Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Approach for Blend Trees without the use of Rigidbodies

Discussion in 'General Discussion' started by denali95, Dec 12, 2022.

  1. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I have a bipedal boss in my game that's moving around the arena using A * Pathfinding, and I want to use a Blend Tree to allow his walk animation to show movement to the right/left/etc. To do this, I've made the blend tree be 2 dimensional and calculated the velocity in the x and z directions by doing the following calculation in update:
    Code (CSharp):
    1.  
    2.            if (previous != null)
    3.             {
    4.                 currentVelocity = (transform.position - previous) / Time.deltaTime;
    5.                 animator.SetFloat("Blend", currentVelocity.x);
    6.                 animator.SetFloat("Blend2", currentVelocity.z);
    7.             }
    8.             previous = transform.position;
    Am I approaching this correctly? I'm new to Blend Trees, so my confidence isn't so strong.

    Here's my blend tree for reference. I've got my idle animation in the middle, and my animations for forward-left, forward-right, etc in what I believe to be the right places to track with this setup.
     

    Attached Files:

    Last edited: Dec 12, 2022
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    543
    The code looks correct. Should output the same results as Rigidbody.velocity would (velocity in units per second). Does the result behave as you want?
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    12,917
    You're not approaching this correctly, because your character can turn, and you are specifying absolute velocity. The parameters should be relative to character's left/forward. Additionally, you need either 4 or 8 blended animations in addition to idle for a character movement, currently you have six, and you're missing "walk left" and "walk right" animations.

    If "previous" is a Vector3, then it is a struct, and it cannot be null.

    Instead of calculating velocity based on position, you should either take velocity stored in a rigidbody, OR use player inputs.
     
  4. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    543
    I admit that I missed the fact that the vector should be in local space instead of world space. Can be fixed with Transform.InverseTransformDirection command
    Code (CSharp):
    1. currentVelocity = this.transform.InverseTransformDirection(currentVelocity);
    However, nothing wrong with using a formula for velocity instead of Rigidbody.velocity (provided you convert it to local space). If you are using built in pathfinding, you can also use NavMeshAgent.velocity. (I think player inputs are non-option since it is supposed to be an NPC)