Turret/Mount Rotates to where player clicks

I am trying to achieve a water gun (Fire Truck) to rotate where a player has clicked,

I have 2 motors (Rotates up and rotates around) and I have no clue on how to make it face where a player has clicked.

So far from what I’ve gathered is this: but I am extremely idiotic and can’t do anything with it

local RotationCFrame = CFrame.new(Vector3.new(playerMouse.hit.p.X, playerMouse.hit.p.Z))

You can use CFrame.new(pos, lookAt) and CFrame:ToObjectSpace() to help with rotating around, and math.atan2 to help with up/down. origin is the part that is each motor’s Part0. (In the “rotate around” portion, origin is aroundMotor’s Part0, and in the “rotate up and down” portion, it is upMotor’s Part0):

-- rotate around
local dir = (target.Position - origin.Position).Unit * Vector3.new(1, 0, 1)
aroundMotor.C0 = origin.CFrame:ToObjectSpace(CFrame.new(origin.Position, origin.Position + dir)

-- rotate up and down
local dir = (target.Position - origin.Position)
local angle = math.atan2(dir.y, dir.z)
upMotor.CFrame = CFrame.new(origin.Position) * CFrame.Angles(angle, 0, 0)

It would probably work

2 Likes

Instead of trying to mess with welds, I always just attach the turret to the body of the vehicle with a HingeConstraint, then use BodyGyro to make it rotate. The BodyGyro’s CFrame would be set using CFrame.new(pos, lookAt). Same concept for making it move up and down. If you want, you can do some math to implement limits too, so that the turret won’t rotate beyond a certain point.

What is “target.position” meant to represent? As in thats where the player has clicked?

uh the motor dissapears and everything go crazy