How to slowly rotate the player

  1. What do you want to achieve? Keep it simple and clear!
    I have a body velocity applied on a player at a certain moment and when that body velocity is applied, I want to make so if player presses A key for example, their character will slowly rotate to the left until the player stops pressing the button

  2. What is the issue? Include screenshots / videos if possible!
    I wanted to use ContextActionService, but I also want it to support mobile joystick movement, so IDK what else I could use.

1 Like

You could connect it to whatever, but I’d say you might want to look into modifying the player’s character’s HumanoidRootPart’s CFrame, maybe using CFrame.angles().

1 Like

You could just anchor the humanoid root part and set the cframe to

local rotation = whatever your rotation is

humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position) * CFrame.Angles(0, math.rad(rotation), 0)

You could get your rotation by adding to it when right is pressed and subtract when left is pressed.

1 Like

Try using Humanoid.MoveDirection. It should go in the direction the player is trying to move, whether from wasd, the mobile joystick, or a controller.

Note I believe you need the default character scripts for move direction to be updated, so as long as you haven’t modified those this should work.

Hello, I want to rotate the player like that, but I need to get the side that I should rotate it to

This property is read only. though I could try to get the direction that the tween should rotate to with this. How would I do it?

The MoveDirection represents the direction the user’s input is making the character move. You’d read the vector, then every render stepped:

local deltaTime -- from render stepped
local moveDirection -- from humanoid
local hrp -- the HumanoidRootPart
local hrpLookVector -- from HumanoidRootPart.CFrame.LookVector
local hrpRightVector -- from HumanoidRootPart.CFrame.RightVector

-- If it should turn left or right
if moveDirection:Dot(hrpRightVector) > 0 then -- Might need to be based on the camera CFrame or world CFrame, I forgot what moveDirection is relative to
     hrp.CFrame *= CFrame.Angles(0, math.rad(20) * deltaTime, 0)
else
     hrp.CFrame *= CFrame.Angles(0, -1 * math.rad(20) * deltaTime, 0)
end

When I try to do that, body velocity is not working so I just rotate without moving

Maybe I should try to use Align Orientation?

1 Like

Yes, you definitely should!

I used AlignOrientation for my game, and to achieve the rotation speed I like, I set the responsiveness to over 20 (Of course, adjust it to your liking)

I use this code in my own game and it allows smooth rotation based on your MoveDirection.

Here’s the code:

local rotationalForce = Instance.new("AlignOrientation")
rotationalForce.Name = "rotationalForce"
rotationalForce.AlignType = Enum.AlignType.AllAxes
rotationalForce.Mode = Enum.OrientationAlignmentMode.TwoAttachment
rotationalForce.Attachment0 = attachment
local terrainAttachment = Instance.new("Attachment") -- create an attachment in terrain
terrainAttachment.Parent = workspace.Terrain
rotationalForce.Attachment1 = terrainAttachment
rotationalForce.MaxAngularVelocity = 100
rotationalForce.MaxTorque = 10000
rotationalForce.Responsiveness = 20
rotationalForce.Parent = rootpart

-- in runservice
if moveDir.Magnitude > 0 then
		terrainAttachment.CFrame = CFrame.lookAt(Vector3.zero, moveDir)
	else
		local _, y, _ = rootpart.CFrame.Rotation:ToOrientation()
		terrainAttachment.CFrame = CFrame.fromOrientation(0, y, 0)
	end
end

What is the first attachment you use? Is it inside humanoidrootpart?

oops sorry yeah its an attachment in rootpart, you can make an attachment or simply use the RootAttachment.

local attachment = Instance.new("Attachment")
attachment.Name = "Attachment"
attachment.WorldPosition = rootpart.AssemblyCenterOfMass
attachment.Parent = rootpart

it seems to make the player face the button I am pressing, I would like it to rotate continuously untill player stops pressing the button.

Also, the body velocity seems to be a bit glitchy; maybe I should use something else instead of body velocity?
image
(here I am levitating in the air.)

I would discourage the use of depreciated bodyMovers and opt for new ones like LinearVelocity

It did work, but the rotation feels very unresponsive, and the player gets stuck on every corner and on any surface that is above the current one, and the player can still levitate.
How would I go about making sure the rotation continues with the move direction and how to stop players from levitating? I am pretty sure that making player’s rig parts not collidable except for the humanoid root part would fix the issue.

The linear velocity you have is set to continuously control the velocity in all directions to match the set one, meaning it acts against gravity (leading to the levitation problem).

Instead, set ForceLimitMode to PerAxis and MaxAxesForce to something like Vector3.new(0, math.huge, 0) (put the inf value in the axis that your velocity is going in). This means it can only control the velocity in the forward direction.

And yeah, you would need to use an AlignOrientation if you want it to move and rotate at the same time.

What kind of movement are you going for by the way? Do you have an example or can you explain what it’s for?

So there is a game out there called Team Fortress 2, I am trying too make traversal emotes that will make you move in the direction you are facing and will let you control the direction with A and D, making you slowly turn in the desired direction.

Now the issue is that sometimes the player slides randomly, like they are stuck in the ground.