How would I make character face the move direction relative to the camera like when humanoid.autorotate is on or like in this video at 16:50.
Here is what I tried
Control Module
local targetMoveVector = Vector3.new()
local mainMoveVector = Vector3.new()
local lX,lY,lZ = 0,0,0
---------------------------
local function lerp(a, b, c)
return a + ((b - a) * c)
end
function ControlModule:OnRenderStepped(dt)
if self.activeController and self.activeController.enabled and self.humanoid then
-- Give the controller a chance to adjust its state
self.activeController:OnRenderStepped(dt)
-- Now retrieve info from the controller
local moveVector = self.activeController:GetMoveVector()
local cameraRelative = self.activeController:IsMoveVectorCameraRelative()
local clickToMoveController = self:GetClickToMoveController()
if self.activeController ~= clickToMoveController then
if moveVector.magnitude > 0 then
-- Clean up any developer started MoveTo path
clickToMoveController:CleanupPath()
else
-- Get move vector for developer started MoveTo
clickToMoveController:OnRenderStepped(dt)
moveVector = clickToMoveController:GetMoveVector()
cameraRelative = clickToMoveController:IsMoveVectorCameraRelative()
end
end
-- Are we driving a vehicle ?
local vehicleConsumedInput = false
if self.vehicleController then
moveVector, vehicleConsumedInput = self.vehicleController:Update(moveVector, cameraRelative, self.activeControlModule==Gamepad)
end
-- If not, move the player
-- Verification of vehicleConsumedInput is commented out to preserve legacy behavior,
-- in case some game relies on Humanoid.MoveDirection still being set while in a VehicleSeat
--if not vehicleConsumedInput then
if cameraRelative then
moveVector = calculateRawMoveVector(self.humanoid, (moveVector))
end
local willJump = self.activeController:GetIsJumping() or (self.touchJumpController and self.touchJumpController:GetIsJumping())
local player = game.Players.LocalPlayer
local Character = player.Character
local hum = Character.Humanoid
local rootPart = Character.HumanoidRootPart
local state = hum:GetState()
local rx, ry, rz = Camera.CFrame:ToOrientation()
if not JUMP_DIRECTION_FREEZE or (JUMP_DIRECTION_FREEZE and not willJump) then
if state ~= Enum.HumanoidStateType.Freefall then
targetMoveVector = moveVector
--targetMoveVector = Vector3.new(moveVector.X,moveVector.Y,moveVector.Z)
end
end
local mainMoveVector2 = Vector3.new()
if state ~= Enum.HumanoidStateType.Freefall then
mainMoveVector = lerp(mainMoveVector, targetMoveVector, math.clamp(dt * MOVE_ACCELERATION, 0, 1) )
--mainMoveVector2 = lerp(mainMoveVector, targetMoveVector, math.clamp(dt * 3, 0, 1) )
end
local X,Y,Z = mainMoveVector.X,mainMoveVector.Y,mainMoveVector.Z
--print(X,Y,Z)
--print(dt)
--print(X,Y,Z," ",lX,lY,lZ)
----set minimum value (otherwise your character will be sliding)
if (X<lX and X>0 and X<MIN_VALUE) or (X>lX and X<0 and X>-MIN_VALUE) then
X = 0
end
if Y<lY and Y>0 and Y<MIN_VALUE or (Y>lY and Y<0 and Y>-MIN_VALUE) then
Y = 0
end
if Z<lZ and Z>0 and Z<MIN_VALUE or (Z>lZ and Z<0 and Z>-MIN_VALUE) then
Z = 0
end
-------------------
mainMoveVector = Vector3.new(X,Y,Z)
lX,lY,lZ = X,Y,Z
local TARGETANGLE = math.atan2(mainMoveVector.X,mainMoveVector.Z)
if mainMoveVector.Magnitude >= 0.1 and rootPart then
rootPart.CFrame =rootPart.CFrame:Lerp(CFrame.new(rootPart.CFrame.p) * (CFrame.fromOrientation(0, math.rad(TARGETANGLE) * ry , 0)),dt)
end
self.moveFunction(Players.LocalPlayer, mainMoveVector, false)
--end
-- And make them jump if needed
self.humanoid.Jump = willJump
end
end