Alright, so my aim here is to create a spaceship that flies and moves as if one would move in space. You go up, it goes up and stays in place(It goes forward a little after you stop but you get what I’m saying). I am super SUPER new to body movers and I know that this sorta thing is required through body movers. The only problem here is I have no idea where to start, I’ve been trying to mess with it, use the wiki and look through other topics on here for a few days and I haven’t really gotten anywhere. Right now I am using the client to control the ship and the body movers(I haven’t replicated anything to the server yet I am just trying to figure out these body movers). When the player enters the ship, it creates a body mover and that’s where I am stuck. Are there any good resources outside the wiki for this sorta stuff? What body movers would be useful for this? I assume this is going to require a lot of physics but I don’t really know where to start. (Also trying to challenge myself and go into zones that I am not comfortable with)
I would recommend trying out the different body movers and decide on which one(s) you want the most. For flying ships and aircraft, the main two body movers that I use are BodyVelocity (for forward and backwards motion) and BodyAngularVelocity (for steering the craft). Those two can do just about anything in terms of flying vehicles, from my experience.
A really lag free way to do this is to have a BodyVelocity and a BodyGyro inside of the plane (inside a part inside the plane, not just inside the model)
Then have a script inside the plane that does two things:
- Sets the network ownership of the plane to the player (so that the player can move the plane locally)
- Inserts a localscript inside the player’s character
from the localscript just set the BodyVelocity and BodyGyro and because we have networkownership the plane’s movement will replicate to the server (other players see it)
heres a simple example of how to set the BodyGyro and BodyVelocity
local player = game.Players.LocalPlayer local mouse = player:GetMouse() game:GetService("RunService").RenderStepped:connect(function() plane.BodyVelocity.Velocity = Mouse.Hit.LookVector * 50 -- 50 is our flying speed -- this will move the plane towards the mouse's position plane.BodyGyro.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position, workspace.CurrentCamera.CFrame.Position + workspace.CurrentCamera.CFrame.LookVector * 20 ) -- this will tilt the plane to where the camera is facing end)
The best body movers for this sort of job would be
The BodyGyro smoothly rotates the ship to a desired orientation and the BodyVelocity applies a force onto an object to bring it to a specific velocity value.
Here’s an example:
local player = game.Players.LocalPlayer local gyro = ship.BodyGyro -- these body movers are to be placed in the main part inside the model local bodyvelocity = ship.BodyVelocity local function flyShip() gyro.CFrame = CFrame.new(ship_model.PrimaryPart.Position, player:GetMouse().Hit.p) local Direction = ship:GetPrimaryPartCFrame().lookVector bodyvelocity.Velocity = Direction * 65 -- can be any value for speed end game:GetService("RunService").RenderStepped:Connect(flyShip)
It’s very simple code and does not contain any needed camera code to attach the camera to the ship. If you’d like to simply attach the camera behind the ship to follow it, add this to the
ship.CamPart.CFrame = ship:GetPrimaryPartCFrame()*CFrame.new(0,6,-20) -- Note: the `CamPart` needs to be a part of the ship's model. Change the `CFrame.new()` values to increase/decrease the distance from the ship. workspace.CurrentCamera.CFrame = ship.CamPart.CFrame
If you need help with anything, let me know!
Everyone here is giving you great responses but just a tip: Don’t forget to set network ownership of the ship to the player driving it to make it super smooth and prevent lag! Here is a link to help you with it! NetworkOwnership