0
\$\begingroup\$

I have used the scripting root motion move the player when using an 'inplace' animation.

All works correctly until the players rotation is off 0,0,0. This is expected as the movement is along the transform.z axis but how can I account for the rotation in the script and apply it so the movement is correct in game?

The gorillacontroller script is the script we are trying to fix On playerGorilla. animator is on the playergorilla. The gorilla1 is the gameobject holding the mesh and the bones for the character.

I will also add the transforms are moving this was my silly mistake because I had tool handles in global instead of local.

This makes it even more confusing however because all tranform.z (blue axis) are facing the correct way for the animation to work but the animation is only ever playing in the original direction where the rotation is 0,0,0,0.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Animator))]

public class RootMotionScript : MonoBehaviour {

    void OnAnimatorMove()
    {
          Animator animator = GetComponent<Animator>(); 

          if (animator)
          {
               Vector3 newPosition = transform.position;
               newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime; 
               transform.position = newPosition;
          }
    }
}

hierarchy image

\$\endgroup\$
8
  • \$\begingroup\$ You mention below that this.transform's z direction is not rotating. Can you show us the hierarchy of your character so we can understand which part is rotating? Without this information, we have no idea where to get the correct movement direction from. \$\endgroup\$
    – DMGregory
    Sep 16, 2017 at 12:17
  • \$\begingroup\$ added image to question. \$\endgroup\$ Sep 16, 2017 at 12:30
  • \$\begingroup\$ We'll need more than that — which node(s) in that hierarchy are the ones that determine the player rotation? And where is this relative to the RootMotionScript's GameObject? Remember, the more clearly you can explain your situation, the more quickly you can get accurate, helpful answers. If we have to guess, we might guess wrong, and that's not helpful for anyone. \$\endgroup\$
    – DMGregory
    Sep 16, 2017 at 12:34
  • \$\begingroup\$ Sorry, ill try to explain further. The gorillacontroller script is the script we are trying to fix On playerGorilla. animator is on the playergorilla. The gorilla1 is the gameobject holding the mesh and the bones for the character. \$\endgroup\$ Sep 16, 2017 at 12:47
  • \$\begingroup\$ And which of those rotates, on which axis? Remember to put this information in your question via an edit, so users don't have to read a back-and-forth comment thread to understand what you need. \$\endgroup\$
    – DMGregory
    Sep 16, 2017 at 12:51

1 Answer 1

1
\$\begingroup\$

You can use transform.forward to get the local z-axis(blue) direction converted in world space. So you can just do

transform.position += transorm.forward * animator.GetFloat("Runspeed") * Time.deltaTime

Make sure you get the forward property on the right gameobject (it depends on your models and animations, usually is the root gameobject or the skeleton parent)

\$\endgroup\$
5
  • \$\begingroup\$ unfortunately this just sends the player forward thru space. the z axis on all my transform are pointing the same way and do not change when i rotate my player. This is fundamentally why it's not working. The method always moves the player forward with accounting for the rotation. \$\endgroup\$ Sep 16, 2017 at 11:44
  • \$\begingroup\$ How do you rotate your character? Is your character a skinned mesh? if you rotate your character with an animation, chanches are you are rotating one of the child transform, and not the root one \$\endgroup\$ Sep 16, 2017 at 12:01
  • \$\begingroup\$ void Turn() { if (Mathf.Abs(turnInput) > inputDelay) targetRotation *= Quaternion.AngleAxis(rotateVelocity * turnInput * Time.deltaTime, Vector3.up); transform.rotation = targetRotation; \$\endgroup\$ Sep 16, 2017 at 12:10
  • \$\begingroup\$ animations are in place, not root and the character does have a skinned mesh. \$\endgroup\$ Sep 16, 2017 at 12:18
  • \$\begingroup\$ Try to calculate the forward direction like this : targetRotation * Vector3.forward \$\endgroup\$ Sep 16, 2017 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .