Help needed with making a Turret face towards Mouse

Hello, Im kind of new to Cframe and I’ve been trying to make my turret face towards the mouse for the past few days.

I have made a code but it doesn’t work
Here is my current setup:

The code:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local TurretUnion = game.Workspace.Turret.TurretS

while wait() do

TurretUnion.CFrame = TurretUnion.CFrame * CFrame.Angles(math.asin((mouse.Hit.Position - mouse.Origin.Position).Unit.Y), 0, 0)

end

I tried to make a loop that detects the mouse position and faces the turret towards it, however something doesn’t work.

-Hope you can help

1 Like

To get something with angles pointing towards another object the equation is CFrame.new(origin, pointyouwanttolookat)

1 Like

do this instead:

TurretUnion.CFrame = CFrame.new(TurretUnion.Position, mouse.Hit.Position)
3 Likes

Make the CFrame of the turret:

CFrame.new(Turret.Position, mouse.Hit.Position)

The first Vector3 is the position, the second Vector3 is the LookAtPosition;
I suggest doing this client-sided and server-sided (client-sided for the player who is doing it, server-sided for other players)

2 Likes

Also you would have to move the script into the player’s playerscripts for it to run, i would clone the script and remove the clone upon sitting down and sitting up since it seems you want to do that as you put it in the seat.

1 Like

It worked, Thanks
Just gotta fix some stuff, but it worked

1 Like

Quick question… I’ve been trying to make it rotate only in one axis
Is there a way to lock some axis orientation or making it just rotate in one direction?

-Hope you can help

Yes, try this, this would make the turrret rotate in one axis

turret.CFrame = CFrame.new(turret.Position, Vector3.new(mouse.Hit.Position.X,turret.Position.Y,mouse.Hit.Position.Z))

You can also try this

local unit = (mouse.Hit.Position-turret.Position).Unit
local cross = unit:Cross(Vector3.new(0,1,0))
turret.CFrame = CFrame.fromMatrix(turret.Position,cross, Vector3.new(0,1,0))

sorry for my bad english :smile:

2 Likes

Thx for your help, it works now I can start with more server sided stuff :smiley:

Not sure if I should ask my own questions on here… But…

I’m making a mech, and using this, I made it rotate towards the mouse! The problem is… It rotates, and MOVES towards the mouse at the same time. Do you have a fix on this? (The first one, by the way)

1 Like

Wich code did you use and what dou you mean by it goes towards the mouse?

I used the first block of code, and by it went to the mouse… It literally was moving in the direction of the mouse.

Look:

In this video, I’m not moving. Whenever I point my mouse somewhere, the mech just moves over there.

3 Likes

Use this code:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local turret = game.Workspace.TurretUnion

while wait() do

	turret.CFrame = CFrame.new(turret.Position, Vector3.new(mouse.Hit.Position.X,turret.Position.Y,mouse.Hit.Position.Z))

end
2 Likes

It’s still happening… The turret is still moving towards the mouse.

is your script the same as the one I sent you?

Yes, with a few adjustments with the variables
(Just changed what the turret model was)

Weird, dont you have any Body mover or vector force acting on the thing, or another script?

Nope! Not at all! Very strange…

1 Like

I have almost not even coded in a while so i’ve forgoten a lot of things but I tested the code and it didnt make it go forward so it might be something in your game.

Well, was your object anchored at all? This might be the problem, because mine was not…

1 Like