Aiming Issues
Making a turret that aims at where the players cursor is; however, I just realized that it doesnt aim properly when not facing directly in the +/- z direction.
The turret runs on hinges (servo). Rotate is for side to side rotations (y axis) while pitch is to look up and down. The pitch hinge part is attached to the rotate hinge constraint.
Could figure this out eventually, but I tend to overthink these things and waste alot of time, so help is appreciated.
-- Runs in loop
function aimTurret()
if not activeSentry then return end
local RootPart = activeSentry.TurretBase
local pitch = activeSentry.Base.HingePitch.HingeConstraint
local rotate = activeSentry.Base.HingeRotate.HingeConstraint
local Pos = Mouse.Hit.Position -- Will need to include console and mobile support aswell
local origin = Vector2.new(RootPart.Position.X,RootPart.Position.Z)
local target = Vector2.new(Pos.X, Pos.Z)
local beta = math.deg(math.atan((target.X - origin.X) / (target.Y - origin.Y)))
rotate.TargetAngle = beta
local mag = (Pos - RootPart.Position).Magnitude
local y = Pos.Y - RootPart.Position.Y
beta = math.deg(math.asin(y/mag))
pitch.TargetAngle = beta
end
I have checked out other “solutions” but they all only seem to work in a single dimension.
Camera Issues
Also if this gets solved I got two more questions regarding the Turret Systems Camera. In short the camera is controlled by a part (I did not setup this method), and so I rotate the the part around based on where the mouse is aiming. I don’t want to attach the part to the hinge though, as the hinge lags behind and is also updated slower intentionally.
The Base is the center point of the turret and the camerafocus is the part that the camera is looking from. Currently the code sets the camerafocus part behind the turret at the right x/z angle however, Im a bit confused as to how I should handle the y axis. I don’t want the camera to just be stuck at a specific height (how its set up atm). The camera should be slightly above the turret no matter where its aiming. Technically getting the orthogonal angle of the unit vector I create If I use Vector3 instead of Vector2 should get me in the right direction but im just not managing to set it up properly. Honestly reading this confuses me more so hopefully this picture somewhat helps.
--Vector 2 method (No Change in Y Axis)
game:GetService("RunService"):BindToRenderStep("RotateCamera", Enum.RenderPriority.Character.Value, function()
if activeSentry then
local Pos = Mouse.Hit.Position
local origin = Vector2.new(Base.Position.X,Base.Position.Z)
local target = Vector2.new(Pos.X, Pos.Z)
local unit = (target-origin).Unit
local CamPos = Vector3.new(Base.Position.X - (unit.X * 12), Base.Position.Y, (Base.Position.Z - (unit.Y * 12)))
CameraFocus.Position = CamPos
end
end)
Lastly I need to clamp the cameraFocus parts position. If the turret was only facing directly down a specific axis then it would not be hard but im struggling to find the rights points given the turret starting at an angled position. Turret should be able to aim ± 60 Deg left to right and ± 45 up and down from its original orientation.