Help with BodyGyros

Before I get started, I know you are all going to say “BodyGryos are deprecated.” This is true, but I am using them in this system, so I need help with it.

I have a piece of code that both turns the spaceship and makes it roll a little, to add a cool turning effect. However, when I use this code, it is very jerky. I can’t use tweens because the ship is not anchored. I also can’t use the same BodyGyro that turns the ship because then it affects the turning of the ship. I have experimented with using several BodyGyros, but that doesn’t work either. All help appreciated!

Snippet in question:

if spaceship.Steer > 0 then
	spaceship.BodyGyro.CFrame = CFrame.fromEulerAnglesXYZ(0, 0, -10)
	spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, -0.1, 0)
elseif spaceship.Steer < 0 then
	spaceship.BodyGyro.CFrame = CFrame.fromEulerAnglesXYZ(0, 0, 10)
	spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, 0.1, 0)
elseif spaceship.Steer == 0 then
	spaceship.BodyGyro.CFrame = CFrame.fromEulerAnglesXYZ(0, 0, 0)
	spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame
end

Hey there! Im not a scripted, but this could help.

Try using the RenderStepped event to gradually interpolate the rotation of your spaceship. This approach can help you achieve smoother turning without affecting the overall ship rotation.

local spaceship = -- whatever your ship name is called
local BodyGyro = spaceship:FindFirstChild("BodyGyro")

local turnRate = 0.1 -- adjust for turning speed

local function updateRotation()
    if spaceship.Steer > 0 then
        spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, -10), turnRate)
    elseif spaceship.Steer < 0 then
        spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, 10), turnRate)
    else
        spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.new(), turnRate) -- Reset rotation
    end
end

-- connect to the RenderStepped event for smothe update
game:GetService("RunService").RenderStepped:Connect(updateRotation)

^ I used the RenderStepped event to gradually interpolate the rotation using the lerp function. This should make the turning effect smoother. Remember to adjust the turnRate to control the speed of the turning effect as needed. Cheers!

You know, im no scripter but I actually am getting the hang of code.

2 Likes

Is the ship only sitting on a flat plane, or does it go nose up/nose down?

You could Anchor a base part of the ship and do all your movements with tweens, then CFrame the base part around the map.

There are plenty of posts about setting up aircraft style movement in the forums.
I just typed ‘realistic aircraft flight’ and got a couple of hits.

How would I get started on making a realistic flight system

Thanks so much! I will try it!

2 Likes

Yeah, it doesn’t nose up/down, just one plane, and then up/down buttons. I would prefer not rewriting everything and using tweens, they dont react well to the physical environment.

The reason why it’s unstable is because roblox didn’t intend to support the use two BodyGyros so therefore I think the only way to this would be to integrate the roll with the main turning system.

1 Like

This works great for smooth animation, thanks! However, I also need to actually turn the ship. For example, I need to do the roll just for animation, but I also need to change the Y by 0.1 to actually turn the ship, outside of control of the animation. I tried this, but it doesn’t turn, only the anim plays:

			if spaceship.Steer > 0 then
				spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, -10), 1)
				spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, -0.1, 0)
			elseif spaceship.Steer < 0 then
				spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, 10), 1)
				spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, 0.1, 0)
			elseif spaceship.Steer == 0 then
				spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, 0), 1)
				spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame
			end

Maybe this?


local spaceship = workspace:FindFirstChild("YourShipName") -- replace 'YourShipName' with the actual name of your ship in the workspace
local BodyGyro = spaceship:FindFirstChild("BodyGyro")

local turnRate = 0.1 -- adjust this value for turning speed
local turnAmount = 0.1 -- adjust this value for turning the ship

local function updateRotation()
    if spaceship and spaceship.Steer then
        if spaceship.Steer > 0 then
            local rotation = CFrame.Angles(0, 0, -10) * CFrame.Angles(0, turnAmount, 0)
            spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(rotation, turnRate)
        elseif spaceship.Steer < 0 then
            local rotation = CFrame.Angles(0, 0, 10) * CFrame.Angles(0, -turnAmount, 0)
            spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(rotation, turnRate)
        else
            spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.new(), turnRate) -- reset rotation
        end
    end
end

game:GetService("RunService").RenderStepped:Connect(updateRotation)

Hmm, still doesn’t actually turn the ship. It only plays the animation. I am trying to see if I can modify the lerping though…

Try this:

local spaceship = workspace:FindFirstChild("YourShipName") -- replace 'YourShipName' with the actual name of your ship in the workspace
local BodyGyro = spaceship:FindFirstChild("BodyGyro")

local animationRate = 0.1 -- adjust this value for animation smoothness
local turnAmount = 0.1 -- adjust this value for turning the ship

local function updateRotation()
    if spaceship and spaceship.Steer then
        if spaceship.Steer > 0 then
            spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, -10), animationRate)
            
            local rotation = CFrame.Angles(0, math.rad(turnAmount), 0)
            spaceship.CFrame = spaceship.CFrame * rotation
        elseif spaceship.Steer < 0 then
            spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.Angles(0, 0, 10), animationRate)
            
            local rotation = CFrame.Angles(0, math.rad(-turnAmount), 0)
            spaceship.CFrame = spaceship.CFrame * rotation
        else
            spaceship.BodyGyro.CFrame = spaceship.BodyGyro.CFrame:lerp(CFrame.new(), animationRate) -- reset animation
        end
    end
end

game:GetService("RunService").RenderStepped:Connect(updateRotation)
1 Like

Hmm, still doesn’t work. Now I can’t even turn the ship. Would it help you to see the entire code? I can PM it to you.

Yes please! My messages are open.

1 Like

Additionally, put everything you want modified in your code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.