Home > Software design >  Unity blend tree in animator not working/jittering
Unity blend tree in animator not working/jittering

Time:12-09

I am using unity version: 2021.3.6f1 I have setup indevidual animations for the player (four-way idle and walk) and I am using two blend trees which the script accesses and changes the variables moveX, moveY and is_idle. I am using the new Unity input system: Screenshot of new Unity input system. and cinemachine (cinemachine isn't causing problems though). When I run the script, it has a weird jittering effect as if the animator keeps reseting the animation playback. Below is a video. Link to video Screenshot of the animator: Screenshot of the animator Here is the code in the player controller:


using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    public float moveSpeed = 1f;
    public ContactFilter2D movementFilter;
    public float collissionOffset = 0.05f;
    
    
    
    
    Vector2 inputaxis;
    Vector2 movementInput;
    Vector2 prevMovementInput;
    Rigidbody2D rb;
    
    Animator animator;
    
    
    List<RaycastHit2D> castCollissions = new List<RaycastHit2D>();
    
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }
    
    
    private void FixedUpdate() {
        if(movementInput != Vector2.zero){
            bool success = TryMove(movementInput);
    
            if(movementInput != prevMovementInput)  {          
                animator.SetFloat("moveX", movementInput.x);
                animator.SetFloat("moveY", movementInput.y);
                prevMovementInput = movementInput;
            }
    
        
            if(!success){
                success = TryMove(new Vector2(movementInput.x, 0));
    
                if(!success){
                    success = TryMove(new Vector2(0, movementInput.y));
                }
            }
            animator.SetBool("is_idle",false);
        } else {
            //animator.SetBool("is_idle", true);
        }
    
        
    }
    
    
    
    private bool TryMove(Vector2 direction){
        int count = rb.Cast(direction,
                movementFilter,
                castCollissions,
                moveSpeed * Time.fixedDeltaTime   collissionOffset);
    
    
        if(count == 0){
            rb.MovePosition(rb.position   direction * moveSpeed * Time.fixedDeltaTime);
            return true;
        } else {
            return false;
        }
        
    }
    
    
    void UpdateAnimation_DONT_USE(){
        // this is my origional function that would be called when I didn't have a blend tre
        //animation direction
        if(movementInput.x == 0.00d){
            if(movementInput.y ==1.00d){
                //up
                animator.SetInteger("direction",1);
            }
        }else if(movementInput.x == 1.00d) {
            if(movementInput.y == 0.00d){
                //right
                animator.SetInteger("direction",2);
            }
        }else if(movementInput.x == 0.00d) {
            if(movementInput.y == -1.00d){
                //down
                animator.SetInteger("direction",3);
            }
        }else if(movementInput.x == -1.00d){
            if(movementInput.y == 0.00d){
                //left
                animator.SetInteger("direction",4);
                
            }
        } 
        
    
        
    
    
    }
    
    void OnMove(InputValue movementValue) {
        movementInput =  movementValue.Get<Vector2>();
    }

}

The youtube video I used First, I had lots of transition arrows going from the Any State block to all of the animation phases. When I tried it, the same result occured. I then watched a different youtube video which showed how to use a blend tree I still have no idea what the problem is after trying a lot of different things. If anyone can help, it would be much appreciated. :)

CodePudding user response:

I have found a solution meaning that I have to remake the game. Even though I still don't know what the problem is, I used the default input system in a new prodject and it works fine. Thanks to everyone who tried to help me! :)

  • Related