Get Mouse Axis Value in C++ - So Character can turn with mouse movement

My character has two movement modes for unequipped and equipped states.

Unequipped - mouse movement does not affect the rotation of the camera (Default)

Equiped - player SHOULD turn with mouse movement (or controller right analog stick)

I am trying to do all this in C++.
Initially when I achieved this in Blueprints only. I had an AxisTurn variable which was getting a float value from the mouse this way.

then in my animation blueprints I was using the value in my idle → turn transition rules

Then on equipping a weapon, I was switching between Orient Rotation and Rotation Yaw with the Macro

This was done following a BlueprintGames youtube tutorial.

So as it is, I am just trying to translate all this to just using C++ and I think its safe to assume I just need to get the Mouse Axis value when I equip a weapon

Which then should affect the value in my animation blueprint. Unless I am thinking about this the wrong way? Basically the question is, whats the best way to toggle Controller Rotation Yaw on Equip/UnEquip weapons in this case?

It doesn’t appear to be a direct translation as that would be an input binding that would do the following

void APlayerCharacter::InputAxisTurn(float Value)
{
    AxisTurn = Value;
    AddControllerYawInput(Value);
}

With that said you can get the axis value from the input component

= InputComponent->GetAxisValue("AxisName");

Ah I see. I take it at this point its more than just getting the axis value. Would part of the solution be setting the Rotation from the Character Movement? i.e
bOrientRotationToMovement = true;
bUseControllerRotationYaw = true;

would something like

myVariable = Character->GetMovementComponent()

have these variables accessible?

It’s 100% not quite clear to me the overall picture of how this is set up.

Just that the direct translation of the first screenshot would be what I wrote, where AxisTurn is a member variable.

Ignoring how everything is set up so far in the first screenshots

When player has no weapon equipped, this is how they are controlled by default
(Mouse movement does not affect rotation of the player)

Once they equip a weapon, then Character rotates with mouse movement (i.e Camera stays behind players back)

Screenshots shown in initial post was how it was achieved in blueprints only, but I am trying to get this done in c++ instead. To toggle between the two modes based on equiped and unequipped weapon.

How would one go about switching between the two types of movements?
I was guessing it was something to do with
UseControllerRoationYaw and OrientRotationToMovement?
or there’s a different way?

I haven’t looked too much into it but what you outlined in your OP made sense to me. You had it working in BP, I thought the only issue left was the last screenshot which can be done via the input binding mentioned.

Its seems in addition to that I have to set the state of these two Boolean variables as well.
These would be from the Character Movement component I believe.

They appear to be public variables.

//assuming within ACharacter scope

GetMovementComponent()->bOrientRotationToMovement = /* value */;
bUseControllerRotationYaw = /* value */;

I am a bit lost with the error message I am getting here with regards to the CharacterMovementComponet

The two lines have the same error message

When the variable do exist on the component

Because I’m an idiot and wrote the wrong one

GetCharacterMovement()

That returns an UCharacterMovementComponent* the other returns UPawnMovementComponent* .

1 Like

Cheers Dan, that did the trick.

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

Privacy & Terms