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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Animation Blend Tree using object rotation as parameters

Discussion in 'Animation' started by Pohlerbaer, Mar 31, 2022.

  1. Pohlerbaer

    Pohlerbaer

    Joined:
    Mar 8, 2022
    Posts:
    7
    Hey guys!

    I'm working on a project, where I move a fish on a beziere-curve path. The fish automatically rotates, depending on how I rotate the normals on the path (right now they simply look in the local y direction on a given point).

    Because the swimming animation, looks like it's clearly on some kind of rail, I implemented a blend tree, which lets the fish look in every direction (blends between up&down and left&right). So far so good.

    The point I'm stuck right now is, that I don't know how to communicate the x&y-rotation parameters via code from the gameObjects rotation to the blend tree. If I simply set the parameters to the rotation of my gameObject, there comes a point where the operator of the rotation magically inverts and therefore destroying the blending.

    Is there some way to calculate the localRotation of my object, without Quaternion (at least I think they are part of the problem) interfering? Capture.PNG
     

    Attached Files:

  2. Pohlerbaer

    Pohlerbaer

    Joined:
    Mar 8, 2022
    Posts:
    7
    The blending works perfectly fine, it's just the calculation to set the params, that bugs me.... I just checked again, and in the inspector, the rotation keeps inverting when it hits -180 on an axis
     
    Last edited: Mar 31, 2022
  3. Pohlerbaer

    Pohlerbaer

    Joined:
    Mar 8, 2022
    Posts:
    7
    Hey, I already managed to find a solution myself!

    In case if anybody wonders, the reason you can't just reference the rotation of your object, is because of the math behind quaternions, also eulerAngles don't work, because eventually you'll end up with a gimbal lock.

    My solution was to just let another object with and offset run infront of my animated object. Then, I calculated two things:
    First, whether the offsetPoint is higher or lower than the animated object on the y-Axis, to determine whether it was going up, or down and then just simply give the positive-/negative difference to the yRotation value.
    For the xRotation, I offset the offsetPoint to the height of the animated object on the y-Axis, and then calculated the angle between the offsetPoint and the transform.right of the animated object. Then I simply checked, if the angle was higher, or lower a specefic number (in my case i subtracted 90 from my angle, to let it be 0, when the object moves straight forward)

    Code (CSharp):
    1.                    
    2.                                    
    3.                     float yRotation = offsetPoint.y - transform.position.y;
    4.  
    5.                     Vector3 angleCheckPoint = transform.right;
    6.                     angleCheckPoint.y = transform.position.y; // make sure on same height, if not, the angle might change
    7.                     Vector3 offsetAnglePoint = new Vector3(offsetPoint.x, transform.position.y, offsetPoint.z);
    8.  
    9.                     Vector3 checkDirection = offsetAnglePoint - transform.position;
    10.  
    11.                     float xRotation= Vector3.Angle(checkDirection, angleCheckPoint)-90f;
    12.  
    13.                     animator.SetFloat("xRotation", xRotation);
    14.                     animator.SetFloat("yRotation", yRotation);
    There are probably much better solutions for the problem, but this worked for me :)