Spaceship physics, where to start?

How could I go about making spaceship physics, literally up, down, left and right I guess.

Any help appreciated!

3 Likes

Make the spaceship move when you press the keybind. I donā€™t think this is a physics related thing.

Thatā€™s not what Iā€™m asking, I understand its going to move via keybinds, Iā€™m asking what would be the most efficient way to make it move, for example I donā€™t think CFrame would be good at all.

Maybe you could use AlignPosition or LinearVelocity possibly (Iā€™m not really experienced with the physics engine).

Everything depends on your specific goals.

Does the spaceship need to be walkable? And if so, does the player need gravity in there? Does the spaceship need to be able to float in place without falling? So on and so forth. CFrame is fine, velocity stuff is fine too.

This is one of those things that depends on your approach. What kind of game are you making?

You could go for the realistic, physical approach. Engines apply force to the ship, which pushes it. This could be cool for a space combat game - blowing up an engine on a craft could make it spiral out of control. This makes accelerating and stopping take time, so flying is harder.

You can go for the programmed approach. You tell the ship to move, and the game gives the ship some velocity. If your game is about flying to planets to explore them, this would make the ships easier to fly, but theyā€™d feel lessā€¦ weighty.

Well, to be able to go up, down, turn left and right, forward and back, and not fall.

Yes

I could do that for turning left and right, forward and back, for up and down I would have to somehow make it resist gravity?

Hereā€™s a basic script that will allow a part (spaceship) to move up, down, left and right.

The script works by applying forces to the part in the desired direction when the corresponding key is pressed.

The ā€˜UserInputServiceā€™ is used to detect key presses and the ā€˜BodyVelocityā€™ object is used to apply forces to the part.

local UserInputService = game:GetService("UserInputService")
local spaceship = game.Workspace -- replace with your spaceship path

-- create a BodyVelocity object and parent it to the spaceship
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Parent = spaceship

-- set the initial velocity to zero
bodyVelocity.Velocity = Vector3.new(0, 0, 0)

-- define the speed of the spaceship
local speed = 50

-- function to handle key presses
local function onKeyPress(input)
    local key = input.KeyCode

    if key == Enum.KeyCode.W then
        -- move up
        bodyVelocity.Velocity = Vector3.new(0, speed, 0)
    elseif key == Enum.KeyCode.S then
        -- move down
        bodyVelocity.Velocity = Vector3.new(0, -speed, 0)
    elseif key == Enum.KeyCode.A then
        -- move left
        bodyVelocity.Velocity = Vector3.new(-speed, 0, 0)
    elseif key == Enum.KeyCode.D then
        -- move right
        bodyVelocity.Velocity = Vector3.new(speed, 0, 0)
    end
end

-- function to handle key releases
local function onKeyRelease(input)
    local key = input.KeyCode

    if key == Enum.KeyCode.W or key == Enum.KeyCode.S or key == Enum.KeyCode.A or key == Enum.KeyCode.D then
        -- stop moving
        bodyVelocity.Velocity = Vector3.new(0, 0, 0)
    end
end

-- connect the functions to the key press and key release events
UserInputService.InputBegan:Connect(onKeyPress)
UserInputService.InputEnded:Connect(onKeyRelease)

You can start off from here. Of course, this is not how it will actually work, Iā€™m sure you will add a seat also but this is starter script you could use.

Why not just treat the spaceship like a person and give it a ā€œflyā€ script that youā€™d see in admin cmds?