Player Rotation Snapping

  1. What do you want to achieve? Keep it simple and clear!

I want to achieve a smooth and eased transition from one direction to the other. Instead of the jolt that roblox gives you.

  1. What is the issue? Include screenshots / videos if possible!

The issue is shown in the video. It’s that roblox automatically snaps your player when you’re looking the other direction and suddenly press a key.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried using tweens to smoothly rotate it, going into the control script and editing it there, and even add body gyros. But none of them worked. And I already looked on the dev hub.

Any help is welcome, thanks!

2 Likes

Could you provide the source code?

Oh yea my bad. Here it is:

local Player = game.Players.LocalPlayer
local Real = Player.Character
local root = Real.HumanoidRootPart
local CC = game.Workspace.CurrentCamera
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

S = false

local function ShiftLock()
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
	S = true
	Real.Humanoid.CameraOffset =  Vector3.new(2.5,1,1)
end

local function UnShiftLock()
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
	S = false
	Real.Humanoid.CameraOffset =  Vector3.new(2.5,1,1)
end

UnShiftLock()

RS.Heartbeat:Connect(function()
	if S == true then
		local _, y = workspace.CurrentCamera.CFrame.Rotation:ToEulerAnglesYXZ() --Get the angles of the camera
		root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0,y,0) --Set the root part to the camera's rotation
	end
	
	
	local speed = Player.Character.PrimaryPart.Velocity.Magnitude

	if speed > 1 and speed < 16 then
		if S == false then
			S = true
			ShiftLock()
			print("Walking")
		end
	elseif speed <= 1 then
		if S == true then
			S = false
			UnShiftLock()
			print("Idle")
		end
	end
end)

The code is meant to provide shiftlock while your moving and a sort of free cam while your standing still

2 Likes

Do you want the character to rotate with the camera smoothly at all times or just while moving?
Edit: (Nvm im dumb)

2 Likes

I will get back to you soon! I am beginning the work

1 Like

Ok, thanks in advance! I’m about to go to bed so I won’t be able to reply until tomorrow.

1 Like

The script now works under StarterPlayerScripts and here it is!:

--!strict
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local CurrentCamera = workspace.CurrentCamera

local Character: Model
local Humanoid: Humanoid
local Root: BasePart

local walking = false

local function characterAdded(character: Model): ()
	Character = character
	Humanoid = character:WaitForChild("Humanoid") :: Humanoid
	Root = character.PrimaryPart :: BasePart
	
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	
	Humanoid.AutoRotate = false
	Humanoid.CameraOffset =  Vector3.new(2.5,1,1)
end

if Player.Character then
	task.spawn(characterAdded, Player.Character)
end

Player.CharacterAdded:Connect(characterAdded)

RunService.RenderStepped:Connect(function(deltaTime: number): ()
	if not Humanoid or not Root then
		return
	end
	
	local rootAssemblyLinearVelocity = Root.AssemblyLinearVelocity
	local rootMagnitude = rootAssemblyLinearVelocity.Magnitude

	if rootMagnitude > 1 then
		if not walking then
			walking = true
		end
	else
		if walking then
			walking = false
		end
	end
	
	if walking then
		local cameraCFrame = CurrentCamera.CFrame
		local cameraLookVector = cameraCFrame.LookVector
		
		Root.CFrame = Root.CFrame:Lerp(CFrame.new(Root.Position, Root.Position + Vector3.new(cameraLookVector.X, 0, cameraLookVector.Z)), deltaTime * 10)
	end
end)
1 Like

I tried it out and it’s still snapping when your doing a 360. Does it work for you?

That’s just how lerp works, the greater the difference between the starting and target CFrame, the faster the interpolation appears, because the change is so dramatic.

However, it still appears smooth, or atleast better than what you had before.