I was following a tutorial for a custom character movement script in this post, the outcome worked pretty well, but for some reason, when I inputted 2 movement keys to make the character move diagonally, it almost didnt turn at all. I went into freecam and noticed that when i tilted the camera up more, it would make the character turn more diagonally. Here is an example of what I mean:
Here is the code that determines the movement direction relative to the camera:
local walkKeyBinds = {
Forward = { Key = Enum.KeyCode.W, Direction = Enum.NormalId.Front },
Backward = { Key = Enum.KeyCode.S, Direction = Enum.NormalId.Back },
Left = { Key = Enum.KeyCode.A, Direction = Enum.NormalId.Left },
Right = { Key = Enum.KeyCode.D, Direction = Enum.NormalId.Right }
}
local function walkDirectionCamera()
local walkDir = Vector3.new()
for name, key in pairs(walkKeyBinds) do
if UIS:IsKeyDown(key.Key) then
walkDir += Vector3.FromNormalId(key.Direction)
end
end
if walkDir.Magnitude > 0 then
walkDir = walkDir.Unit
end
return walkDir
end
local function walkDirectionWorld()
local walkDir = camera.CFrame:VectorToWorldSpace(walkDirectionCamera())
walkDir *= Vector3.new(1,0,1)
if walkDir.Magnitude > 0 then
walkDir = walkDir.Unit
end
return walkDir
end
I’m not very good with this type of math, so I hope someone else could help me solve this issue. Thanks!
It effectively squishes the blue Camera.LookVector into the horizontal plane, producing the short red vector, which explains why the front/back (or up/down) movement is too slow.
It should be producing the longer, dashed red vector which is more like a rotated version and less like a squished one. It can be done like this:
local function walkDirectionWorld()
local horizontalCameraDir = camera.CFrame.LookVector * Vector3.new(1, 0, 1)
if horizontalCameraDir.Magnitude == 0 then
return Vector3.zero
end
local cameraPos = camera.CFrame.Position
local horizontalCameraCF = CFrame.LookAt(cameraPos, cameraPos + horizontalCameraDir)
local walkDir = horizontalCameraCF :VectorToWorldSpace( walkDirectionCamera() )
return walkDir
end
Slight issue that I’ve now just noticed: If you have your camera pointing directly down on the character, then it returns NaN as the walkdirection. Currently trying to figure out how to fix it, however might be caused by my own additions