So I am trying to create a system that overrides the default Roblox movement system which if you don’t already know the character constantly moves in the direction of the camera. What I am trying to achieve is a way to lock the movement to an initial camera position and orientation so when the camera changes the character continues to move in the original direction, and once the character stops moving it will adjust its movement direction to the new Camera orientation and position. An example of this would be with Persona 5. In Persona 5 there may be an area where your camera is locked to a certain corner of the room and you will move your player model around like usual. But once you walk to a new area say through a door, You camera will change to a different locked position in the new area but your character will continue moving forward relative to the original camera orientation but once the character stops moving it will realign the movement to the new camera position. That is essentially what I am trying to accomplish here.
Although the issue I am running into so far is when the camera orientation and position changes, The character movement does not realign to the new camera when the character stops moving.
I’ve tried to use Chat GPT to revise my code although it seems to just make the issue worse and I cannot find any other posts trying to replicate what I am trying to achieve here.
Here is my code so far:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
local function calculateMoveDirection()
local forward = Vector3.new(camera.CFrame.LookVector.X, 0, camera.CFrame.LookVector.Z).Unit
local right = Vector3.new(camera.CFrame.RightVector.X, 0, camera.CFrame.RightVector.Z).Unit
local moveInput = Vector3.zero
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
moveInput += forward
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
moveInput -= forward
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
moveInput -= right
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
moveInput += right
end
if moveInput.Magnitude > 0 then
return moveInput.Unit
end
return nil
end
-- Update character movement on every frame
RunService.RenderStepped:Connect(function()
local direction = calculateMoveDirection()
if direction then
humanoid:Move(direction, true)
else
humanoid:Move(Vector3.zero, true)
end
end)
I do apologize for the long post as I have been at this for hours and cannot find a proper solution to this and I know what I am describing is very specific, So I had to go into detail to explain it as much as possible as I am sure nobody wants to go out and buy a $60 game just to understand the movement system to resolve a DevForum issue LOL