InverseTransformDirection is returning unexpected results, anyone know why this is happening?

Hi Everyone,

I think I posted in the wrong section of Unity discussions, sorry. I’m new at Unity so bear with me. For context, I’m building a top-down shooter game. The player moves on the global axes and the character faces wherever the mouse is pointing. Pretty standard stuff. For further context I followed this Unity discussion after searching for awhile. It seemed like the perfect simple solution so I followed it.

My issue is this: I want the animations to reflect whatever direction the character is facing but when I use the InverseTransformDirection(); method things get funky. Wherever the character is rotated it also sends horizontal/vertical information, when in theory it should only take the values I send it, right? When it’s not in use the animations play fine, albeit not the right directions relative to where my character faces.

For example: if I push the W Key, the character moves forward on the Z axis, and plays the RunningForward animation. If I then move my cursor to the right of the player while still moving on the Z axis it should play the left strafe animation. However, since the values are not being calculated correctly the animations do not play.

Here are two screenshots from my animation controller for parameters like the above unity discussion:

InverseTransformDirection(); is off and I am free to rotate, no additional values being sent, just the keys I push.

image_2

Now, InverseTransformDirection(); is on and when I rotate my character, additional values are being sent to my animation parameters.

image

I thought the issue may be with putting the script on the Player Prefab itself – maybe there was something wrong about the mesh, or pivot point, I do not know. I’ve placed it inside an Empty Game Object in hopes that it would fix it but it has not made a difference.

Below is my player controller code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float movementSpeed = 4;
    // private float verticalInput;
    // private float horizontalInput;


    private float verticalInputRaw;
    private float horizontalInputRaw;

    // Calculuates into Fire Rate
    public float timePassed = 0f;
    public float fireRate = .05f;

    public GameObject bulletPrefab;
    public GameObject mainCamera;

    // public Animation mainCameraAnim;
    private Animator playerAnim;
    private Rigidbody rigidBody;

    private Vector3 moveDirection = Vector3.zero;


    void Start()
    {
        playerAnim = GetComponentInChildren<Animator>();
        rigidBody = GetComponentInChildren<Rigidbody>();
        // mainCameraAnim = mainCamera.GetComponent<Animation>();
    }

    // Update is called once per frame
    void Update()
    {
         //Gets Input System
        verticalInputRaw = Input.GetAxisRaw("Vertical");
        horizontalInputRaw = Input.GetAxisRaw("Horizontal");

        Moving(horizontalInputRaw, verticalInputRaw);
        Turning();
        Animating(horizontalInputRaw, verticalInputRaw);
    }

    private void FixedUpdate() {
       
    }

    private void Moving(float horizontalInputRaw, float verticalInputRaw) {
        
        
        // Moves character
        transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed * verticalInputRaw, Space.World);
        transform.Translate(Vector3.right * Time.deltaTime * movementSpeed * horizontalInputRaw, Space.World);

        //Creates Bullet Prefab to shoot enemies
        timePassed += Time.deltaTime;
        if (timePassed > fireRate)
        {
            if (Input.GetKey(KeyCode.Mouse0))
            {
                Instantiate(bulletPrefab, transform.position + new Vector3(0, 1, 0), transform.rotation);
                timePassed = 0f;
            }

        }
    }

    private void Turning() {
        
        //Rotates character based on mouse position
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red);

        if (Physics.Raycast(ray, out hit))
        {
            Vector3 targetPosition = hit.point - transform.position;

            targetPosition.y = 0f;

            Quaternion rotation = Quaternion.LookRotation(targetPosition);

            // rigidBody.MoveRotation(rotation);
            transform.rotation = rotation;
        }
    }

    private void Animating(float horizontalInputRaw, float verticalInputRaw) {

        //SECTION FOR ATTEMPTING TO ANIMATE CHARACTER
        Debug.Log("FIGURE OUT ANIMATION CONTROLLER FOR PLAYER");

        moveDirection = new Vector3(horizontalInputRaw, 0, verticalInputRaw);
        // moveDirection = moveDirection.normalized;
        moveDirection.y = 0;

        //THIS IS DIRECTLY AFFECTING WHY THE ANIMATION WILL NOT WORK
        moveDirection = transform.InverseTransformDirection(moveDirection);
        

        Debug.Log(moveDirection);


        playerAnim.SetFloat("Horizontal", moveDirection.x, 0.0f, Time.deltaTime);
        playerAnim.SetFloat("Vertical", moveDirection.z, 0.0f, Time.deltaTime);
    }

}

Any help would great. Thank you so much.

var worldSpaceDirection = Camera.main.transform.TransformDirection(inputDirection) to turn screenspace keyboard input into camera relative direction. Then player.transform.InverseTransformDirection(worldSpaceDirection) to turn it into Horizontal and Vertical animator params that are relative to the direction the player character is facing.