Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Blendtree broken. Why is this happening?

Discussion in 'Animation' started by mattseaton22, Jan 21, 2023.

  1. mattseaton22

    mattseaton22

    Joined:
    Sep 12, 2019
    Posts:
    43
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Animations : MonoBehaviour
    4. {
    5.     [SerializeField] Transform lookTarget;
    6.     GroundCheck groundCheck;
    7.     Animator animator;
    8.  
    9.     int turning;
    10.  
    11.     void Awake()
    12.     {
    13.         turning = Animator.StringToHash("Turning");
    14.  
    15.         groundCheck = GetComponent<GroundCheck>();
    16.         animator = GetComponentInChildren<Animator>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (groundCheck.isGrounded)
    22.         {
    23.             Vector3 sideProjection = Vector3.ProjectOnPlane(lookTarget.position - animator.transform.position, transform.up);
    24.             float turn = Vector3.Dot(sideProjection, animator.transform.right);
    25.             animator.SetFloat(turning, turn);
    26.         }
    27.     }
    28. }
    This script breaks my blendtree and makes my character sink into the floor. Can someone explain why?

    If I disable it and set the turning parameter by hand in the animator it works as expected. But now my character is no longer looking at her look target, but above it.

    What am I missing?
     

    Attached Files: