How to make a vehicle turn with the position of the mouse?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so that the player’s mouse controls the turning of a submarine by turning in the direction that the mouse is. Like the Seamoth in Subnautica. I tried using a local script that received user input service in StarterPlayerScripts that sent what button they pressed to the script that controlled the sub but it only turned once each time a button was pressed and was really hard to control. So I want to make the sub controlled by a mouse so that it becomes both more intuitive and easier to control.
3 Likes

If anybody knows, please reply. Thanks

1 Like

I made this script which is for my plane game, which is for my plane taxi.

This script should help you know how to turn the submarine so it requires a BodyGyro in order for it to work. This script modifies the BodyGyro’s CFrame to the look CFrame depending on the position of the mouse.

local Main = ... -- part mover

while wait() do -- must be added in a loop for it to work
   Main.BodyGyro.maxTorque = Vector3.new(0,math.huge,0)
   Main.BodyGyro.CFrame = CFrame.lookAt(Main.Position,mouse.Hit.p)
end
3 Likes

Ok, I’ll try this. I thought that mouse.hit wouldn’t work. :smile:

1 Like

For some reason, it has the error Workspace.Seamoth.VehicleSeat.Script:11: attempt to index nil with ‘Hit’

Since I had to modify this script, here:

local Seat = script.Parent
local Gyro = Seat.BodyGyro
while wait() do
Seat.ChildAdded:Wait()
local Occupant = Seat.Occupant
local Player = game.Players:GetPlayerFromCharacter(Occupant.Parent)
print(Player) -- check if it's working
local Mouse = Player:GetMouse()
Gyro.MaxTorque = Vector3.new(0,math.huge,0)
Gyro.CFrame = CFrame.lookAt(Seat.Position,Mouse.Hit.p)
end
1 Like

There’s an error that caused the Player variable to be always nil, which is i’m assuming because of the childadded function.

local Seat = script.Parent
local Gyro = Seat.BodyGyro

while Seat.Occupant ~= nil do
  
    local Occupant = Seat.Occupant
    local Player =  game.Players:GetPlayerFromCharacter(Occupant.Parent)
    if not Player then return end

    local Mouse = Player:GetMouse()
    Gyro.MaxTorque = Vector3.new(0,math.huge,0)
    Gyro.CFrame = 
    CFrame.lookAt(Seat.Position,Mouse.Hit.p)
end
2 Likes

Thank you, although this wasn’t the answer that worked, it put me on the right track to making my control system work.

1 Like