Moving Character based off Player Rotation

  1. What do you want to achieve? Player movement based off of player’s rotation, rather than off the camera. I have it set up so that the A and S keys rotate the player.

  2. What is the issue? When I hold W or S, it goes up and down, rather than in the direction the character is facing. It is currently impossible to move left or right.

  3. What solutions have you tried so far? I have looked on the Developer forum for this type of camera/ fixes for this. Also tried getting the player’s Head CFrame and moving the player off of its rotation, but to no avail.

It looks like your movement is being overwritten by the default roblox movement. You can edit this movement script by copying the PlayerModule from your PlayerScripts when you play the game and pasting that in StarterPlayerScripts; this will overwrite the default PlayerModule roblox injects into your player.

You can then delete the ControlModule and the require for this module in the PlayerModule to totally script your own movement or you can edit the control module itself.

If you go with the second option, in the ControlModule, there should be many different Controllers and modules for each device. Find the one that says Keyboard. (Note that this will only update keyboard movement, you would need to edit the others if you want to change movement on other platforms.)

In the Keyboard module, change the Keyboard:UpdateMovement() method to this code:

local character = game:GetService("Players").LocalPlayer.Character
if not character or inputState == Enum.UserInputState.Cancel then
	self.moveVector = ZERO_VECTOR3 
else
	self.moveVector = -(self.forwardValue + self.backwardValue) * character:GetPivot().LookVector
end

This essentially takes the normal inputs of the keyboard and changes their moving vector to the character’s LookVector (which you change when you manually rotate your player with your controls). It also disables left and right movement. Then, the ControlModule will then read these values through a Controller and uses Player:Move() to then move the player. If you want to completely disable the module, I would recommend this for how you would tackle this movement.

The last thing here is that the BaseCharacterController is reading these values as relative to the camera (before you edited it this was the case). However, since we are using a world space vector to move the player forward, you have to change this. To do this, open the BaseCharacterController module script and edit line 43 to self.moveVectorIsCameraRelative = false.

Hope this helps!

Edit: I did not explain why I am using a minus sign when I set the moveVector. This is because in the Keyboard module, the forwardValue is negative and the backwardValue is positive. This is done because the LookVector of an object is technically on the negative Z axis relative to the CFrame which is the axis the module originally set the moveVector to. We aren’t using relative CFrames, so we reverse this with a minus sign and multiply by the lookVector to get a vector in world space.

2 Likes

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