I need help breaking down code for Flying Script

I’ve been working on a flying script looking up resources and coding it on my own and have been stuck on making it go towards what your camera is looking at but like still relative to where you’re moving if that makes sense and this script from a tutorial captured it perfectly but I can’t exactly understand this code majority of it is in one line confusing.

This is the snippet of code from it

local function u2()
	if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		return Humanoid.MoveDirection
	end
	local v12 = (Camera.CFrame * CFrame.new((CFrame.new(Camera.CFrame.p, Camera.CFrame.p + Vector3.new(Camera.CFrame.lookVector.x, 0, Camera.CFrame.lookVector.z)):VectorToObjectSpace(Humanoid.MoveDirection)))).p - Camera.CFrame.p;
	if v12 == Vector3.new() then
		return v12
	end
	return v12.unit
end

This is what my script currently looks like

This is the video

The GetMovementDirection function seems to be doing the brunt of the work. Since its on github it’s a lot harder to quote, but it returns the character’s movement direction if they’re moving, and if not, it returns the direction of the camera. I would assume that means the player would start moving in the direction of the camera but accessing it after that is unhelpful, right?

If im not misunderstanding this, why not just have the bodyvelocity move in the direction of the Camera’s lookvector? the camera itself is attached to the player, and is always looking at it. so if you just attach the movementdirection to the camera’s lookvector, the movement will already be relative to the player’s movement. let me know I’f im misunderstanding what you’re trying to do.

If it’s not, I’d start off by utilizing the function CFrame.LookAlong which just takes the LookVector instead of a place to look at, just makes the code a lot easier to read. My best guess is that you’re trying to have the camera face a certain way, and that you’d be able to press A or D so that you can change direction. If that’s what you’re talking about, you can use the LookAlong function again to comine your inputs. Instead of using the lookVector, you can use the RightVector. Instead of going in the Camera’s LookVector, you can set the movementdirection to the camera’s RightVector if the D key is pressed, and negate the RightVector (-) if the A key is pressed. You can actually just add the RightVector (or negated RightVector) and LookVector, and since they’re both Vector3s, you can use .Unit so that if W is pressed at the same time as A or D, the returned movement direction is still forwards but can be straying.

I really hope this helps! And I commend you for actually taking the time to learn how to code, you really dont see that here anymore.

1 Like