How to have correct strafe animation based on the players rotation?

I’am making a third person shooter game where my camera is following but not rotating with my character. I want my character to be able to move and shoot while having the correct blended (strafe) moveAnimations. Watch this at 3:36 Converting your game to DOTS - Unity at GDC 2019 - YouTube to see what I mean.
I’am using two floats based on the joysticks input to determine witch animation should play. I successfully made those two floats relative to the camera position, because of this my player is always walking away from the camera when moving forward no matter the cameras position. To have correct strafe motion u will need to know what the rotation is of player and in witch way it is moving. How can I achieve this? the joystick I’am using: up =1, down =-1, right = 1, left = -1.

private void Update()
    {
        
        lX = JoystickLeft.Horizontal;
        lY = JoystickLeft.Vertical;
        rX = JoystickRight.Horizontal;
        rY = JoystickRight.Vertical;
        movement.Set(lX, 0f, lY);
        rotation.Set(rX, 0f, ry);

        Rotating();
        dirspeed = Vector2.SqrMagnitude(Joystick.Direction);
           
            movement = movementOrientationCam(); //making input relative to cam.   
        }

        void Rotating()
        {
        if (Input.touchCount > 1f)// player is pressing Ljoystick and Rjoystick? then we can Strafe. (for testing)
        {
            float i = lX * rY;                                                                                                                                                // the players left and right walkAnim are now switched when he is facing the camera.
            float ii = lY * rY;                                                                                                                                                //the players foreward and backward walkAnim are now switched when he is facing the camera.
            // But when he is facing left or right relative to the cam rY becomes 0 so (lX * 0 or lY * 0 = 0f) therfore he wont be playing his strafe left or right walkAnim when moving up or down (while facing left or right relative to cam).
            anim.SetFloat(xhash, i);
            anim.SetFloat(yhash, ii);
            
            if (rotation != Vector3.zero)
            {
                Quaternion newrotation = Quaternion.LookRotation(rotation);
                playerRig.MoveRotation(newrotation);}
             }
        else
        { if(movement != Vector3.zero)
            {
                Quaternion newrotation = Quaternion.LookRotation(movement);                                                  //No strafing just rotate towards ur moving.
                playerRig.MoveRotation(newrotation);}
           }
         }

    private Vector3 movementOrientationCam()
    {
        if (CameraTransform != null)
        {
            Vector3 dir = CameraTransform.TransformDirection(movement);                                            //making input relative to cam. But I still dont know how this function is working/caculating it, anyone?
            dir.y = 0;

            return dir.normalized * movement.magnitude;
        }
        else
        {
            CameraTransform = Camera.main.transform;
            return movement;
        }
    }

i don’t really have an idea but this section of unity can help you maybe, it’s in the animator, it’s where you can choose animation for your character based on the way he’s walking, no need for a lot of coding, just do more research maybe you’ll find what you’re asking for 143535-untitled.png

I’ve seen this question edited or reposted by you for over a week now, if nobody can/wants to help, respect that and maybe seek a different solution, but just reposting this over and over and over is redundant, especially as it has been uncommented or unanswered for as long as I have noticed this question.


Unity has a standard assets package which has a third person controller in it, and I have used it and it behaves just like every other third person game I’ve played (GTA, Red Dead, Metal Gear, Dark Souls, etc) and that should at least give you an idea of a working third person script that you can reference for the one you are writing.

If you want another option, for a third party asset that is free, the reviews on this one look good and is another option if the Unity third person controller script isn’t pointing you in the right direction.


If you’re new to coding, use the free assets available because they will save you a ton of time and help you out if you still need to customize. For example, if you wanted to build a piece of furniture, you wouldn’t go to a forest, chop down a tree, remove the bark, process the wood; then go to a mine and prospect for iron or whatever and smelt that to make nails, combine the wood and smelted iron to make a hammer, etc., etc., etc.; you would just go to the store and buy some processed wood, a hammer, and nails and then build from there; treat your code the same way and make it easy when you can and if you need to customize or add flare, do that to something existing and make that existing code yours.