Turret Tracking Issue

	local radX, radY, radZ = absoluteTurretCFrame:toOrientation()
	
	local TrueRadion = Vector3.new(radX,radY,radZ)*180/math.pi
	
	local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
	
	local radHX, radHY, radHZ = HumanoidRootPart.CFrame:toOrientation()
	
	
	local ClampY = math.clamp(radHY,TrueRadion.Y - 50,TrueRadion.Y + 50)

script.Parent.Head:SetPrimaryPartCFrame(CFrame.new(absoluteTurretCFrame.p) * CFrame.fromOrientation(radX,math.rad(ClampY),0))

IT KINDA WORKS? BUT NOT REALLY? I’m a bit lost, but if I don’t clamp it works and aims, elsewise it kinda glitchilly goes to the side. (HumanoidRootPart facing forward it seems it splits from 180 to -180 in the middle. but math.clamp cannot work with the current numbers, its prob something I’m doing.

As @jody7777 said, you need to manipulate the relative vector. In other words, after you calculate the turret CFrame to aim at the character as you originally posted, convert it to the object space of the tank. Then adjust the CFrame based on your desired maximum and minimum angles, and then convert back to world space.

Of course, there is always more than one solution to any problem, so I am not saying others solutions are wrong. Let me know if my solution does not make sense!

Yeah, I kinda get what you mean, but I don’t think I understand enough to put that into practice effectively, it could just be my brain being tired too. I’ve been spending alot on other things trying to push out something before a self imposed deadline :unamused:

Self imposed deadlines can be troublesome haha, I don’t know about you but I usually don’t give myself enough time because I am bad at guessing how long something will take… :joy:

I have to go to bed, but I will explain this in the morning if someone doesn’t beat me to it! :+1:t3:

Alright, thanks I’m prob gonna go to bed soon too, I just need to work on other parts of stuff before I do. :partying_face:

1 Like

I found this identical post that explains two different solutions you can use. I was going to give you a detailed explanation, but they have already done it. If you have any questions, just ask!

You will have to do some basic tweaking of the code (obviously you aren’t aiming at the mouse, or using 90 degrees as your maximum angle). Also note, his first post of the code has a mistake that he fixes later on in a separate reply. You also do not have a “core”, so you won’t need that (you will have to adjust a few lines for that as well).

3 Likes

Alright thank you I’ll take a look at it when I get more time! :face_with_hand_over_mouth:

Worked like a charm, I also fixed the turret falling behind issue while doing this, now it doesn’t fall behind at all even when moving, is a bit problematic though because it sometimes snaps behind itself if you go right on the edges, but I can look into this, prob need to put a clamp or two.

1 Like

Sorry to bug you again, but do you have any idea why this is doing this though?


local X, Y, Z = absoluteTurretCFrame:ToOrientation()
local X2, Y2, Z2 = absoluteTurretCFrame:ToEulerAnglesXYZ()
local ClampedY = math.clamp(Y,Y2 - 2,Y2 + 2)

Elsewise it works great and it correctly stops on angles.
Just this turning backwards things when standing on it may be problematic, oh and it doesn’t matter if your floating above it, as long as you’re standing above this area the clamp stops working correctly.

Can you post a GIF please? It is hard to tell what is going on there.

https://youtu.be/AxNv_os9u9o I’ll do you one better.

That video was kind of hard to tell what was going on too. xD Its good though, I can at least see what you are talking about now!

Can you post your new code now that you have made updates?

local absoluteTurretCFrame = CFrame.new(script.Parent.Body.TurretPos.Position, Players[i].Character.HumanoidRootPart.Position)

	local Test = isInAngleRange(script.Parent.HumanoidRootPart.CFrame.p, script.Parent.HumanoidRootPart.CFrame.LookVector, Players[i].Character.HumanoidRootPart.Position)



if Test then
local X, Y, Z  = absoluteTurretCFrame:ToOrientation()

local X2, Y2, Z2  = absoluteTurretCFrame:ToEulerAnglesXYZ()


local ClampedX = math.clamp(X,-0.2,0.2)



local ClampedY = math.clamp(Y,Y2 - 2,Y2 + 2)

script.Parent.Head:SetPrimaryPartCFrame(CFrame.new(absoluteTurretCFrame.p) * CFrame.fromOrientation(ClampedX,ClampedY,0))

else
	

	script.Parent.Head:SetPrimaryPartCFrame(script.Parent.Body.TurretPos.CFrame)
	
	
	
	end
	
end

(EXCUSE THE MESS ;o; it was a code warzone)



Both above images are from the Developer Hub page for CFrames


I think you may just need to reorganize your points like so;

CFrame.fromOrientation(ClampedY, ClampedX, 0)

Let me know what happens. good luck. :+1:t3:

image

Lmao, that doesn’t look right. :rofl:

trys to make tank turret function properly Tank: NECK SNAP SOUND

but in all seriousness, it works until that very specific spot in the middle is touched, then it goes full disco depending on where you walk. (Which I assume is the clamp stopping for some reason)

I can walk to its side, or its back without any issue.

I’m thinking the problem is with this part (revert that last change for sure lmao).

Yeah, I tested it a bit earlier and it seems the X clamp doesn’t give out, it’s the Y clamp that gives out (It still clamps correctly for X even when Y is crying)

Actually, when you are back there I am pretty sure it is not meeting the condition of the if statement to use the “Test” portion at all.

I think the problem is here:

Edit: I also think I am tired and confused lol, I think I need to go to bed… :sweat_smile:

1 Like