How to script helicopter?

Hi! I was wondering how to script a helicopter that would fly similiar to jailbreak’s one. I just took a guess and went with BodyForce, but can’t find the center of mass so the helicopter would be actually stable. Also because I counter gravity, the helicopter bounces like blob rather than crashing into the ground.
robloxapp-20210302-1127396.wmv (1.5 MB) (first part is supposed to show the unbalance, when I apply more force it raises into the air and crashesinto the ground like blob)
Is there any other alternative to BodyForce that would more likely to suit this task?

I suggest having one part that has body force that is at the centre, this will serve as a kind of an anchor block since it anchors everything else to it. Make everything else massless and unanchored.
Use BodyPosition rather than body force, and calculate the position infront of the anchor block.
You can change this position when player presses buttons like wasd.

Hope this helps!

I suggest you in fact having two body forces.

One for anti-gravity, make sure its Force property is equal to the helicopter’s total mass * world gravity (property under workspace).

The second for directional movement. You can repeatedly fire a Remote event from the client so the server can tell the helicopter which direction to fly in using the client’s camera look vector. Simply update this BodyForce accordingly.
With this mechanic it means you don’t have to use BodyAngularVelocity or BodyGyro explicitly for rotating the helicopter.

hmmm, I already have constant antigravity force and when I want to move I just add a force like u said…

networkRemote:FireServer()
local bodyForce = Instance.new("BodyForce", helicopter:FindFirstChild("Base", true))

local totalMass = 0
for i, v in pairs(game.Workspace.Hip:GetDescendants()) do
	if v:IsA("BasePart") then
		totalMass = totalMass + v:GetMass()
	end
end

local function setForce(power, axis)
	if axis == "X" then
		bodyForce.Force = bodyForce.Force + Vector3.new(totalMass * power, 0, 0)
	elseif axis == "Y" then
		bodyForce.Force = bodyForce.Force + Vector3.new(0, totalMass * power * game.Workspace.Gravity, 0)
	else
		bodyForce.Force = bodyForce.Force + Vector3.new(0, 0, totalMass * power)
	end
end
setForce(1, "Y")

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		setForce(0.1, "Y")
	end
	if input.KeyCode == Enum.KeyCode.E then
		setForce(-0.1, "Y")
	end
	if input.KeyCode == Enum.KeyCode.W then
		setForce(-0.1, "Z")
	end
	if input.KeyCode == Enum.KeyCode.S then
		setForce(0.1, "Z")
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		setForce(-0.1, "Y")
	end
	if input.KeyCode == Enum.KeyCode.E then
		setForce(0.1, "Y")
	end
	if input.KeyCode == Enum.KeyCode.W then
		setForce(0.1, "Z")
	end
	if input.KeyCode == Enum.KeyCode.S then
		setForce(-0.1, "Z")
	end
end)
2 Likes

I see your point, but how would I update the targeted position? Since holding “W” is an action that doesnt fire but is ment to be executed in longer span of time

In that case, increase/decrease the anti-gravity when the helicopter flies up/down. This means the helicopter will “fall” gently, creating an impression of a graceful landing.

Use different body force(s) for left/right and forward/back.

The main feature of helicopter is levitation, as long as I cant get center of mass I cant influence the posture of helicopter

I’d guess a helicopter’s centre of mass is the propeller, so parent all body movers to the main/central part of the propeller.

When put to flight, the aviation vehicle would swing from top, enhancing the realism of your game.

Use Runservice.heartbeat to check the values IsKeyDown
Do this every frame to constantly update the body position.

u can do

  • Anti-gravity (massless the parts of the char and helicopter)
  • Velocity + Camera Vectors (for the movement)
  • UIS (for going up/down/on/off)

better will be not to use RunService since it can affect the performance
use

repeat
      wait()
      --stuff
until UIS:IsKeyDown("any key") == false

here is what we got by now:
robloxapp-20210302-1939284.wmv (653.9 KB)
There are some still some flows, like rotation and BodyPosition being applied in the axis acording to world space. What would help my helicopter stay balanced and in correct form all the time? How to make vector3 value relative to object?

Gyro can help it be balanced.
Use * rather than + when dealing with vector matrices to get relative translations rather than world positions.

E.g.

CFrame + Vector3.new(1,0,0)

Returns a cframe that has been moved one stud to the X direction.

But

CFrame * Vector3.new(1,0,0)

Returns a cframe that has been moved one stud forwards in relation to the object.

since I am using BodyPosition I am not dealing with CFrames…

posForce.Position = posForce.Position + Vector3.new(0, speed * step, 0)