What is the issue?
The issues is that when I make a turn, is very brusque, I want it to be more circular and slower
What solutions have you tried so far?
I’ve tried to add a lerp but you can see in the video, at the end, that the turn is not fluid.
Here is the script I tried:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local maxSpeed = 20 -- adjust this to set the maximum speed of the player
local moveDirection = Vector3.new(0, 0, 0)
local function updateMoveDirection()
local mousePos = mouse.Hit.p
local playerPos = player.Character.HumanoidRootPart.Position
local newMoveDirection = (mousePos - playerPos).unit
moveDirection = moveDirection:Lerp(newMoveDirection, 0.02) -- adjust the 0.1 value to set the smoothness of the player's movement
end
game:GetService("RunService").Heartbeat:Connect(function()
updateMoveDirection()
player.Character.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position + moveDirection * maxSpeed)
end)
Okay so what happends here is Lerping is like adding a number with provided number’s 0.02 increased.
What you should do to prevent it rotating faster when the angle between your mouse’s position and player’s position is too much is you should set a constant time and lerp it according to that.
local lerp_constant = 1; -- lerp will be done within 1 second.
moveDirection = moveDirection:Lerp(newMoveDirection, lerp_constant / (newMoveDirection - moveDirection).Magnitude); -- moveDirection will be as same as newMoveDirection within 1 second always.
After much testing, I have found that this is indeed correct and you have to use this to get the smooth, regular and circular rotating you want. In future you can use trial and error to find the optimal time yourself by simply changing it and testing it, but the value I found that was the most smooth and not too jittery was local lerp_constant = 0.1
If you are curious of how it’s working, I leave a video:
The Script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local maxSpeed = 20 -- adjust this to set the maximum speed of the player
local moveDirection = Vector3.new(1, 0, 0)
function rotateVectorAroundAxis(axis, vector, angle)
local zVector = (vector:Cross(axis)) / axis.Magnitude
return math.cos(angle) * vector + math.sin(angle) * zVector
end
local function updateMoveDirection()
local mousePos = mouse.Hit.p
local playerPos = player.Character.HumanoidRootPart.Position
local newMoveDirection = (mousePos - playerPos).unit
local lerp_constant = 0.01
local angNewMovedir = math.asin( (moveDirection:Cross(newMoveDirection)).Y )
--print(angNewMovedir)
local maxAngle = math.rad(6)
--angNewMovedir = -angNewMovedir * 0.1
if angNewMovedir > maxAngle then
angNewMovedir = maxAngle
elseif angNewMovedir < -maxAngle then
angNewMovedir = -maxAngle
end
moveDirection = rotateVectorAroundAxis(Vector3.new(0,1,0), moveDirection, -angNewMovedir )
moveDirection = moveDirection.Unit
end
game:GetService("RunService").Heartbeat:Connect(function()
updateMoveDirection()
player.Character.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position + moveDirection * maxSpeed)
end)