0
\$\begingroup\$

I am using the code below to have the character's directional movement match its movement animations but when the player model has a non-zero y-axis rotation, the animations become mismatched.

Code:

    moveVector = new Vector3 (movX, 0, movZ).normalized * (movementSpeed * (1 - pInv.totalMass/20)) * (1 + bonusSpeed);

    animBector = pBody.transform.rotation * moveVector;
    moveVector = gameObject.transform.rotation * moveVector;

    pAnim.SetFloat ("moveSpeed", movementSpeed/2);
    pAnim.SetFloat ("Vertical", animBector.normalized.z);
    pAnim.SetFloat ("Horizontal", animBector.normalized.x);

Note that pBody is the mesh body of the playerObject and is a child of gameObject. The gameObject does not rotate since this is a top-down project & the moveVector = gameObject.transform.rotation * moveVector; part ensures the player moves towards its respective parts of the screen on pressing a directional button.

e.g. forward input while facing North will display a forward animation; forward+right input while facing North East will display a sideways (or strafe) animation, the correct one should be a forward animation.

\$\endgroup\$
1
\$\begingroup\$

It looks like you want animBector to be the movement vector, but local to the apparent direction that the player is facing. This would mean that you need to reverse-apply the rotation of pBody.

Please try replacing this line:

animBector = pBody.transform.rotation * moveVector;

with this one:

animBector = pBody.transform.InverseTransformDirection(moveVector);

If the move vector and the player object might be scaled, but you want Vertical and Horizontal animation parameters to be unscaled, use InverseTransformVector instead.

\$\endgroup\$
  • 1
    \$\begingroup\$ I used your suggestion but had to swap the animBector axes input into the pAnim float parameters to get the directional animations to match. Thanks for your answer! \$\endgroup\$ – Aeiou Aug 26 '18 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.