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
IT WAS ABSOLUTELY WORTH IT THOUGH!