How can I get my lower hinge to track the player properly?
I have a project I've been working on and I tried writing my own script for a turret that tracks the player when it gets near, although at the moment I have it look for me regardless of distance. So far I have the actual gun part rotating properly, but not the thing it's attached to. It's literally the same code except using the Y axis instead of the X axis since it's mounted sideways on a wall.
Here's my code:
01 | local turret = script.Parent |
02 | local gunHinge = turret.GunBase.Base.Base 2 GunHinge |
03 | local gunHingePos = gunHinge.Attachment 0. WorldPosition |
04 | local baseHinge = turret.WallMount.Mount.Base 2 WallHinge |
05 | local baseHingePos = baseHinge.Attachment 0. WorldPosition |
09 | local function trackObject(object) |
10 | local gunHingeX = object.Position.X - gunHingePos.X |
11 | local gunHingeZ = object.Position.Z - gunHingePos.Z |
12 | local baseHingeY = object.Position.Y - baseHingePos.Y |
13 | local baseHingeZ = object.Position.Z - baseHingePos.Z |
14 | local ratio 1 = gunHingeX/gunHingeZ |
15 | local ratio 2 = baseHingeY/baseHingeZ |
17 | gunHinge.TargetAngle = -math.deg(math.atan(ratio 1 )) |
18 | baseHinge.TargetAngle = -math.deg(math.atan(ratio 2 )) |
22 | trackObject(game.Workspace:WaitForChild( "acer1102" ).HumanoidRootPart) |
Thanks in advance! :D