Spaceship system with alignorientation only turning on Y axis

Helo. I’m making a spaceship system that steers with the mouse. I use an AlignOrientation (the replacement for the bodygyro), to turn. It works well, however it only turns on the Y axis (Left to right).

The AlignOrientation is on OneAttachment mode. It has a maxtorque of 50000, and a responsiveness of 5. Its MaxAngularVelocity is infinite. All other important settings (PrimaryAxisOnly, ReactionTorqueEnabled, and RigidityEnabled) are set to false.

My code for calculating the AlignOrientation’s CFrame is shown below. (MousePos being Mouse.Hit.Position)

bg.CFrame = CFrame.lookAt(Ship.PrimaryPart.Position, MousePos)

The video below shows how the ship only turns from left to right. I would also like it to turn up and down, so the ship can move properly. If there is any info I am missing, please let me know.

I made a plane a little while ago.

local gyro = Instance.new("AlignOrientation")
gyro.Mode = Enum.OrientationAlignmentMode.OneAttachment
gyro.RigidityEnabled = false
gyro.Attachment0 = attachment
gyro.MaxTorque = math.huge
gyro.MaxAngularVelocity = plane.Statistics.TurnRate
gyro.Enabled = false
gyro.Responsiveness = 100
gyro.Parent = model.PrimaryPart

And I set the CFrame with this:

gyro.CFrame = CFrame.new(currentPos, currentPos + direction)
------- Definition of direction:
local direction = (vehicle.PrimaryPart.Position - mouse.Hit.Position).Unit

AlignOrientation only “reads” the orientation component of a CFrame anyways, so you can just create an ordinary CFrame.

Hey. I tried this, however it made the same effect. Also, it goes in the opposite direction of the mouse now.

local PrimaryPos = Ship.PrimaryPart.Position
local direction = (PrimaryPos - MousePos).Unit

local bg : AlignOrientation = Ship.PrimaryPart.Turn

bg.CFrame = CFrame.new(PrimaryPos, PrimaryPos + direction)

Seems like it’s trying to move up, but the baseplate is stopping it from rotating upwards - try posting a video of you attempting to rotate it upwards without something blocking the way (perhaps with gravity disabled?)

Gravity is already off, and I’ve tried removing the baseplate before. Same result.

Unable to repro your problem - Tried your script on a part in studio and it seems to work just fine with the X and Y axes. If gravity is off, maybe try moving the ship a bit further up? (If the ship is spawning on the SpawnLocation, move it upwards, anchor, and turn collision off). If that doesn’t work, it must be a problem with your ship.

Here’s what I wrote to get your code working in studio:

game.Players.LocalPlayer:GetMouse().Move:Connect(function()
    local MousePos = game.Players.LocalPlayer:GetMouse().Hit.Position;
    local PrimaryPos = workspace.Part.Position;
    local direction = (PrimaryPos - MousePos).Unit;
    local bg : AlignOrientation = workspace.Part;
    bg.CFrame = CFrame.new(PrimaryPos, PrimaryPos + direction);
end)

Hi. Thank you. I had been doing the code on a server script via remote function. I didn’t know that it would be replicated. I guess something with the remote function was messing it up. This fixed it.

1 Like

Actually, there is one more thing. It turns up and down fine, however it doesn’t turn to a good enough angle. It tilts up/down, but not very far or quickly. Any idea? I basically did that code you did there.

In the video shown, you can see that it does turn, however I would like the turns to be a bit stronger.

Send the code you’re using for the turn - the code that I provided instantly turns the part towards the mouse

Mouse.Move:Connect(function()
          local MousePos = Mouse.Hit.Position
          local PrimaryPos = Ship.PrimaryPart.Position
          local direction = (PrimaryPos - MousePos).Unit
          
          local bg : AlignOrientation = Ship.PrimaryPart.Turn

          bg.CFrame = CFrame.new(PrimaryPos, PrimaryPos - direction)
end)

One thing I forgot to mention is that in the final setting of the CFrame, I did change HRPPos + direction to HRPPos - direction, because that made it so the turns weren’t inverted anymore.

Try this - should make your moves instant

Mouse.Move:Connect(function()
          local MousePos = Mouse.Hit.Position;
          local PrimaryPos = Ship.PrimaryPart.Position;
          local direction = (PrimaryPos - MousePos).Unit;

          local AlignOrientation = Instance.new("AlignOrientation");
          AlignOrientation.RigidityEnabled = true;
          local bg : AlignOrientation = Ship.PrimaryPart.Turn;

          bg.CFrame = CFrame.new(PrimaryPos, PrimaryPos - direction);
end)

I do like that, however I want to only enable it for turning up and down. I don’t want sharp horizontal turns.

On second thought, I’ve decided on a solution. I’m just going to make it so turns are sharper if the ship isn’t accelerating, and have turns be less sharp if moving. Thank you for all the help.

1 Like

With RigidityEnabled set to true, turns are instant - if you want to make it a more gradual change, look into changing the AlignOrientation’s MaxTorque, MaxAngularVelocity, and Responsiveness.

Yep. That’s what I’m going to do.

1 Like