I am currently making a Part in the sky that the player can control with WASD, with No Up or down movement.
The player’s Camera Subject is set to the part, and the part rotates with the camera.
They are added together then applied to the Part to move it.
Right now the movements are relative to the world so W, will always move you in the same ( -Z ) direction in the world despite where your facing.
My problem is the I want to move in the direction that the Part / Camera is facing, W for Forward, A for Left, etc.
Figured it out using trig. For those that stumble upon this topic, use the script in the link then change the 2 parts to this instead.
local keyCodeToDirectionMap = {
[Enum.KeyCode.W] = 90,
[Enum.KeyCode.A] = 180,
[Enum.KeyCode.S] = -90,
[Enum.KeyCode.D] = 0,
}
-- and
local function managePartMovement(step)
local directions = getDirectionsToProcess()
local targetPosition = Vector3.new(0,0,0)
local PartRotation = SkyPart.Orientation.Y
for _, direction in ipairs(directions) do
--Create a vector 3 based of the rotation, Unit Circle
--Radius = 1
--X = Cos(Angle + direction force)
--Z = -Sin(Angle + direction force)
local X = math.cos(math.rad(PartRotation + direction))*-1
local Z = math.sin(math.rad(PartRotation + direction))
local NewVector = Vector3.new(X,0,Z)
local offset = (targetVelocity * step) * NewVector
targetPosition = targetPosition + offset
end
SkyPart.Position += targetPosition
--Rotating the part with the camera
SkyPart.CFrame = CFrame.new(SkyPart.Position,Camera.CFrame.Position)
end