Rotating an Animation

Hello fellas. Alright, I’ll keep this as simple as possible. I’m using my cursor to rotate my player to point towards wherever he fires an arrow off the back of whatever animal he’s mounting, but right now he fires at his ‘forward’ direction, and this looks completely off.

To keep it simple, how do I get him to rotate his body when he’s firing the arrow towards the direction he’s supposed to be looking at? I tried the following (along with a million other failed attempts), but to no avail

I won’t show you the entire code, so as not to overwhelm everyone, but here are the important parts:

in ‘PlayerOnAnimalRangerAimingState.cs’

    private void HandleUpperBodyRotation()
    {
        if (stateMachine.PlayerBodyRoot != null)
        {
            // Get the forward direction of the camera
            Vector3 targetDirection = Camera.main.transform.forward;

            // Flatten the target direction on the horizontal plane to avoid unwanted pitch
            targetDirection.y = 0;

            if (targetDirection.sqrMagnitude > 0.001f)
            {
                // Calculate the target rotation for the upper body
                Quaternion targetRotation = Quaternion.LookRotation(targetDirection);

                // Smoothly interpolate the spine's local rotation towards the target rotation
                stateMachine.PlayerBodyRoot.rotation = targetRotation;
            }
        }
    }

    private void HandleRotation()
    {
        float mouseXDelta = Input.GetAxis("Mouse X") * sensitivity;
        float mouseYDelta = Input.GetAxis("Mouse Y") * sensitivity;

        mouseX += mouseXDelta;
        mouseY -= mouseYDelta;

        mouseY = Mathf.Clamp(mouseY, minXRotation, maxXRotation);

        // Rotate the Cinemachine FreeLook Camera
        // cinemachineFreeLook.m_XAxis.Value += mouseXDelta; // Inversed direction (for the psychopaths)
        cinemachineFreeLook.m_XAxis.Value -= mouseXDelta; // Original direction
        // cinemachineFreeLook.m_YAxis.Value -= mouseYDelta; // Inversed direction (for the psychopaths)
        cinemachineFreeLook.m_YAxis.Value += mouseYDelta; // Original direction
    }

and then it’s fed to ‘PlayerOnAnimalRangerFiringState.cs’, as follows:

        if (stateMachine.InputReader.IsOnAnimalAttacking && !stateMachine.CooldownTokenManager.HasCooldown("OnAnimalRangerFiringCooldown"))
        {
            // Cooldown is over, and player is attempting to attack? Go back to firing arrows:
            stateMachine.SwitchState(new PlayerOnAnimalRangerFiringState(stateMachine, stateMachine.PlayerBodyRoot.rotation));
            return;
        }

which uses it as the following:

    // ROTATION
    private Quaternion rotation;

    public PlayerOnAnimalRangerFiringState(PlayerOnAnimalStateMachine stateMachine, Quaternion rotation) : base(stateMachine)
    {
        hasFired = false;
        this.rotation = rotation;
    }

        // ROTATION (in 'PlayerOnAnimalRangerFiringState.Enter()')
        stateMachine.PlayerBodyRoot.rotation = rotation;

but the rotation fails. Any solutions to help fix this?

(I’m at the final 1/2 steps for this system that I’ve been coding for 2 weeks now. I got nearly everything I needed to fix, fixed (thanks to a kind team member from the Synty community, combined with the Unity forums, Discord and this forum… you guys are all amazing!), just gotta fix this bug, and another bug that somehow resulted from me fixing something else)

Edit: I solved the other bug (it was an animation tag naming mistake from my side). This is the last one remaining for this… annoyingly long system that I accidentally thought would take 2-3 days to write, at most :sweat_smile:

IT WAS ABSOLUTELY WORTH IT THOUGH!

And… I solved the Rotation problem as well. It’s all completely fleshed out now, hopefully no one will be able to catch any bugs with this one :slight_smile:

It took me two weeks, but a proper ranger aiming state is entirely possible :stuck_out_tongue:

Off to Dynamic Respawners, I go

On a side note, here’s what I wrote and called in ‘Enter’ (if you don’t do it in Enter, the first frame will seem unnatural because you didn’t do the rotation early on) and ‘Tick’ to fix that, in the ‘PlayerOnAnimalRangerFiringState.cs’ script:

    private void HandleUpperBodyRotation() 
    {
        if (stateMachine.PlayerBodyRoot != null) 
        {
            Vector3 targetDirection = Camera.main.transform.forward;
            targetDirection.y = 0; // you don't want to rotate on the y-axis, trust me

            if (targetDirection.sqrMagnitude > 0.001f) 
            {
                Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
                stateMachine.PlayerBodyRoot.rotation = targetRotation;
            }
        }
    }

Would’ve been nice though, to find a way to stop the FreeLook Camera from rotating when he’s aiming, just to give the player an opportunity to focus on the goal he’s aiming at, otherwise that would be a little hard, because players will find it hard to properly aim during combat… I’ll find my way around it

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms