Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 6 years ago
Edited 6 years ago

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.

0
Please enclose your code in blocks so we can better understand it. antonio6643 426 — 6y
0
I think its mouse.hit.lookVector kristibezatlliu1111 33 — 6y
0
Try CFrame.Angles(0, 0, mouse.Origin.z). (Angles is the same as fromEulerAnglesXYZ) SimplyRekt 413 — 6y
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. Jesse1240 59 — 6y

1 answer

Log in to vote
0
Answered by 5 years ago

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.

Ad

Answer this question