How would I get my character rotation to match the gamepad stick orientation?

Hi, I’m making a shoot’em up in in the style of Beat Hazard. I’ve set up my character movement on my Xbox360 Gamepad Left stick and his rotation on my right stick.

The problem is, if I hold my right stick for example on the right, my character rotate clockwise endlessly. I just want my character to point in my stick’s direction.

Right now, My PlayerController BP is set like that : Screenshot - 01aabe3c259863603ee70fa7a395250f - Gyazo

“Rotate” is for rotating on the left and the right and “Look up” is for rotating toward top and bot.
It’s probably a simply thing but i’ve already lost a work day on it so…

I had done a similar thing in UDK about a year ago. But unfortunately I dont have the code with me. But I think I can give a basic idea of how I did it.

So in my UDK project which was a side scroller, the player controlled a ship with left and right sticks. The left stick controlled the ships height (UP-DOWN) and moved it to the right or left. The right stick controlled whcih direction the ship was facing and thus where the ship would fire bullets.

I have a float called facingDirection, which is 1 when ship is facing towards right hand side and -1 when facing towards the left hand side. Then I switched between 1 and -1 based on the right analog stick horizontal axis value. Multiply this value with the normalized vector that points to the right hand direction (got from the camera). Convert this to a Rotator and set as the Rotation of the Actor.

PS: I will see if I can find the original code and post it here if I find it.

You must also check FaceRotation() function in APawn

I don’t find anyhing that refers to the stick orientation in the blueprint function, or I do not use the well

Sorry, I could not find the original code. Anyway it was pure unrelascript and may not be applicable here.

So try this:
Bind an AxisMapping on right anologue stick to ‘CharacterFacing’. Now you can create an event node Character Facing in blueprint. It will have an input value ranging from 1 to -1. You can use that to decide the rotation of your character.
But you should not use the value right away. YOu only need the sign of the value. IF the sign chages, then you set the rotation of charcter (but dont feed it into Move Input. or it will rotate for ever).

Ok, I am not sure if this is what you want, but here is a crude implementation that does the follwoing:

  • Use left analogue stick to control the charcter (move left and move right). Will not change charcter’s orientation

  • Use right analogue stick to decide the facing direction. ie where the charcter looks at.

  • This based on the side-scroller template from unreal engine. I added two new Action mappings (FaceRight to AnalogueRightStkRight and FaceLeft to ANalogueRightStkLeft)

i don’t have a controller so i could not verify this but with the gampad input events you get an axis…

so i would use a 2dvector for your stick orientation…
on “gamepad right x” you set the vectors xcomponent and on “gamepad right y” you set the vectors y component…
(these events have an axis value)

then you can use this vector to set your pawn’s orientation…

I’ve tried that, It’s not really working well but it’s may be kind of a beginning.
I can only rotate on the left, and I can do it only one time… and of course, because of the Yaw value that we’ve added in the Make Rot node, it rotate -90°, so I also have to find a way to get my stick orientation to rotate the character smoothly.

So,it’s half working as expected now thanks to your advices.

When I rotate my Gamepad right stick in any direction, my character rotate but… it’s reversed relative to my stick orientation.

Secondly, when I release the stick, the character goes back to his initial orientation. Of course, I’d like to keep his rotation even if I release the stick. Any idea ?

Here’s what I’ve got now

1 Like

Hey ,

Here’s one possible solution, I’ll include a short primer for how this is setup:

  • Using Third Person Blueprint Template as the base
  • Modified ProjectSettings > Input to have FaceNorth and FaceEast events for Y-axis and X-axis of the Right Gamepad Stick, respectively
  • After removing some of the old Axis Events from the below list, you’ll want to delete their Events from the Graph (if you compile after removing them from the settings list, they’ll be tagged yellow for you by the editor so you can just delete them)

Edit: Here’s what all of my Axis events look like, just to be clear on how they’re setup and what I removed from the Template:

6003-twinstickinputs.png

  • In the MyCharacter Blueprint, changed spring arm component to not use controller view rotation and adjusted the length of the spring arm to 700 and the rotation of the spring arm to (0, -90, 0) (this step keeps the camera in a fixed, top-down position)

  • In the MyCharacter Blueprint Defaults, search for Orient Rotation to Movement and make sure it is un-checked.

I think that’s the only other modifications I made outside of this setup in the Eventgraph in MyCharacter:

This setup only rotates the Mesh (and not the capsule component, which is the root of the whole blueprint). Basically, the axis values make an input (like was being done in some of the other screenshots in this example), then the inputs are made into a 2-d vector (Z = 0). We then take the Actor’s location and add this new vector to get a target location to look at, which is fed into a Find Look at Rotation node (super handy node, btw) and we set the Mesh’s world rotation to that new rotation.

If you have any questions about this setup, please let us know!

Hope this helps out!

-Steve

I was trying to adapt this to the sidescroller template. However I wanted the character to retain his last rotation when the stick is released instead of restting to default rotation.

And since we are actually rotating the Mesh, does it affect where the character will fire his gun?

that should be managable…

it sounds like you need a static vector and a threshold…

so

  1. Create a vector-variable which holds your gamepad-vector
  2. check the Axis values delivered by your events for it’s amplitude (how big or small is getting the value? from -1 to 1 or something else?)
  3. decide upon this how great your threshold value should be
  4. update your vector value only if it’s greater than your threshold

when your orientation is of by 180 degree then you have to flip both x and y by multiplying with -1
another thing i did not include here is, when your axisvalues reach from -1 to 1 you have to check if the absolute value is greater than your threshold… othervise it would only work in the positive half

another thing is that i dont know how exacly the vector rotation thing works… so maybe there should be some changes, too. What i described so far gives you a vector which will always point in your last gamepad direction…

good luck and tell if it worked or if you need some more help

I’ve reproduced exactly the same way you explained it and my character do not even rotate

To keep the last rotation when the stick is released, you would just want to put in a check that uses the last rotation when it detects the stick is reset back to it’s default position. I can provide a work around if you hit a wall trying to implement that functionality.

Also, you can use the mesh’s rotation to calculate the vector in which you wish to fire your gun.

Hey Frank Fitrzyl,

I amended a couple of things in my steps list and ran through it again, there were a couple of items I omitted and my 2nd screenshot was missing a pin connection. The key item I added was to make sure to uncheck the Orient Rotation to Movement option from MyCharacter’s defaults. Try out these steps again and see if that works for you.

Thanks.

I got it working in a different way. Mine was a side scroller so I did not have to worry about the analogue value of right stick. So all I did was setup a boolean variable FacingRight and switched it when the stick value changed signs. I know this wont work for a 3D game.

This is how I’ve chosen to do this. It seems to be working pretty well. Adjust the branch conditions to control the “dead” spot.

Does anyone know how to make this work on third person in a way that will change direction in between two anim montages?

Just wanted to say thanks! This perfectly solved my problem!

a mi me funciono este que es para rotar por el eje Z utilizando los valores de localización local de el joysitck y se le sumo 90° al eje Z para que se reoriente

1 Like