X rotation of camera affects diagonal movement of custom movement script

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!

1 Like

Are you using Humanoids? Can I see all the code that goes into calling Humanoid:Walk?

Yes, I am using humanoids. This is all that goes into calling :Move():

	game:GetService("RunService").Heartbeat:Connect(function()
		local dir = walkDirectionWorld()
		hum:Move(dir)
	end)

That should work. What kind of output do you get if you print dir and dir.Magnitude while walking diagonally?

image
Top is just dir normally, bottom is dir.magnitude

1 Like

Dang, my old code is wrong :confused:

Here’s what it does, seen from the side:

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

Could you try it and let me know if it works?

1 Like

This works!!! Thank you so much!

1 Like

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

1 Like

improvised by tilting the camera back by 0.001 which feels a little impractical but it works

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.