Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this turret point towards a player's mouse?

I'm trying to make a tank script that allows you to turn the turret & barrel with your mouse. The turret is a cylinder so it has to move left and right on the y axis. Yes, the y axis. Since the tank is unanchored I am using a weld to CFrame the turret.

I have an example of my tank script at my place. It can be driven. You can press f to fire the machine gun (which is my old script that I need to rewrite.) With all of the crazy things I have tried you'd see that I can't get the barrel to point at the mouse.

This is probably very inaccurate but it would be something similar to this I'm guessing? Just kind of an example of what I have tried.


wait(1)
player = game.Players.LocalPlayer
mouse = player:GetMouse()
weld = findCar.VehicleSeat.Connection -- this is the weld that I need to CFrame

while true do
	wait()
	weld.C0 = CFrame.new(0,0,0) * CFrame.fromEulerAnglesXYZ(mouse.Origin.z,0,0)
end

I really don't know how to ask this. And I don't really have too much information to give.

Summary of my question: Since the turret rotates left and right on the y axis. How do I make it turn left and right with your mouse by CFraming the weld.

4 comments

0

Please enclose your code in blocks so we can better understand it.

0

I think its mouse.hit.lookVector

0

Try CFrame.Angles(0, 0, mouse.Origin.z). (Angles is the same as fromEulerAnglesXYZ)

0

I tried lookVector just now, I seemed to be closer but apparently lookVector is a forward position. At least it would follow the mouse. So apparently if I used CFrame.Angles(mouse.Origin.z,0,0) or CFrame.Angles(0,0,mouse.Origin.z) then it spins around and doesn't point towards the mouse, if you move your camera then it spins wildly. Kind of hard to explain. I still haven't found a solution though.

1 answer

The best place you can look, is the Roblox Developer documentation. If you look at this page you'll notice a nifty attribute of the player's mouse. The hit attribute. According to the docs:

The CFrame of the mouse’s position in 3D space.

So, that means it will give you the CFrame of where the player's mouse is pointing. Now, you simply set your current to rotate, to face that CFrame.

I hope I answered your question.

This discussion is preserved as it was. New answers, comments, and votes are closed.