Top Down Mecanim

Hi,

Is anyone familiar with project zomboid mecanim system?
How do i approach the same mecanim system to my top down game. I run around freely with asdw and when i click right mouse button character looks towards mouse position. Now i want to add mecanim walking towards the mouse position. So if mouse is on the right of the character and i press left, char does backwards animation and when mouse is on the right of the char and i press right, char does still backwards animation. Im familiar with mecanim and how it works, i just want to figure out the code to detect movement to the left right forward and backwards from every angle, no matter where my char looks. I hope im making myself clear here, sry for not perfect english xd

My current code:

move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        controller.Move(move * Time.deltaTime * playerSpeed);
        playerVelocity.y += gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);

        lookPosition = transform.position + move;
        if (Input.GetMouseButton(1)) {
            transform.LookAt(mousePos.position);
             //Here would be the code for mecanim towards mousePos
        } else {
            transform.LookAt(lookPosition);
            if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) {
            anim.SetBool("Run", true);
            } else {
                anim.SetBool("Run", false);
            }
        }

My hope dies last, but thanks in advance!

You’ll need to calculate the movement direction first, by subtracting the character position from the mouse click position (might need to actually convert character coordinates to screen coordinates like here Unity - Scripting API: Camera.WorldToScreenPoint). Then, you can get this vector’s X and Y and check if they are negative or positive, and based on that, activate the required movement animation in mecanim.

Mecanim refers to unity animation system.

What you’re describing is a fairly standard twin-stick shooter locomotion.

Your character is a capsule. The capsule comes with vectors “forward”, “right”, “up”, etc.

You project your world-space movement vector onto THOSE vectors, and feed results into animator to drive feet movement.

On the picture below, the circle is your character seen from above.
8174666--1064048--upload_2022-6-2_5-23-1.png
Blue is forward, Red is right. Green is movement vector. Gray shows projected coordinates.

This can be done with dot products, and this can be done with Transform.InverseTransformVector.

Doesn’t matter which you’re going to use.

1 Like

What worked for me.
Solution:

mecanimMove = Input.GetAxis("Vertical") * cam.forward + Input.GetAxis("Horizontal") * cam.right;
if (mecanimMove.magnitude > 1) {
                mecanimMove.Normalize();
            }
if (mecanimMove.magnitude > 1) {
            mecanimMove.Normalize();
        }

        this.movementInput = mecanimMove;
Vector3 localMovement = transform.InverseTransformDirection(movementInput);
        horizontalFl = localMove.x;
        verticalFl = localMove.z;
anim.SetFloat("Forward", verticalFL, 0.1f, Time.deltaTime);
        anim.SetFloat("Right", horizontalFl, 0.1f, Time.deltaTime);

Kindof converts movement input towards target where character looks.
Thanks for answers DimitriX89 and neginfinity