I am trying to make a spaceship fly where you can press W and the ship goes forward and you can move your mouse to steer the ship
The issue is that the script barely responds to the mouse moving
So far I’ve tried using the 3D world space position of the mouse using the origin point of camera:ScreenPointToRay() and then moving it forwards a bit and using it in the second argument of the CFrame.new() function so it points there as you move your mouse but it’s not really responsive.
I am really lost and I haven’t found any other code which I can reference off of for this
runService.RenderStepped:Connect(function()
if uis:IsKeyDown(Enum.KeyCode.W) then
rootPart.BodyVelocity.Velocity = rootPart.CFrame.LookVector * 50
rootPart.BodyGyro.CFrame = CFrame.new(rootPart.Position,workspace.CurrentCamera:ScreenPointToRay(mouse.X,mouse.Y).Origin * 10)
end
camera.CFrame = rootPart.CFrame * offsetCFrame
end)
That’s not how you move a ray forward. You’re just ignoring the ray and getting the position it starts at, then multiplying each of the coordinates in the position it starts at by 10. That would be, for example, taking any ray that starts at (1,1,1) (which can face any direction from that point) and turning it into (10, 10, 10). If you want it to move in the direction of the mouse, you’ll have to actually take the ray’s direction into account. Here’s an example of how you could do it:
runService.RenderStepped:Connect(function()
if uis:IsKeyDown(Enum.KeyCode.W) then
rootPart.BodyVelocity.Velocity = rootPart.CFrame.LookVector * 50
local ray = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y)
rootPart.BodyGyro.CFrame = CFrame.new(rootPart.Position, (rootPart.Position + ray.Direction))
end
camera.CFrame = rootPart.CFrame * offsetCFrame
end)
Also, the CFrame.new(pos, lookAt) constructor is deprecated. Here’s the devhub recommended equivalent:
local function lookAt(target, eye)
local forwardVector = (eye - target).Unit
local upVector = Vector3.new(0, 1, 0)
local rightVector = forwardVector:Cross(upVector)
local upVector2 = rightVector:Cross(forwardVector)
return CFrame.fromMatrix(eye, rightVector, upVector2)
end
That’s an easy fix though, just swap out CFrame.new with lookAt