Tank Turret not pointing in the direction of the mouse

Hey guys,

I have a tank cannon that wont line up with the position of the mouse, as seen below (the red beam should line up with the center of the circle mouse icon)

Im using Hinge constraints to move the cannon, and using mouse.Hit:ToOrientation() to get the angle in which the player is pointing, and applying that angle to the cannon constraint.

im thinking that it has to do with the offest from the barrel and Camera focus part that causes it to shoot too low

heres the code below that controls the turret and cannon movement:

local difference = workspace.Tank.Base.OrientationPart.CFrame:vectorToWorldSpace(workspace.Tank.Base.OrientationPart.Orientation).Y --this is just to adjust the turrets Y rotation according to the tanks position
Turret.HingeConstraint.TargetAngle = math.deg(y)  - difference --this will move the turret side - side on the Y axis
gun.HingeConstraint.TargetAngle = math.deg(x) --this moves the cannon up and down

does anyone know how i can fix this, or know of any resources i can read up on to get a better understanding?

thanks for reading!

mouse.Hit has the exact same orientation as the camera. Thus, the cannon will face the same way as the camera, instead of facing toward the hit position.

Instead, get an orientation that points from the cannon to the mouse.

-- CFrame that points toward the mouse relative to the tank.
-- If the mouse is directly in front of the tank, then it will point forward, i.e. y (from the last line in this codebox) will be near 0.
-- If the tank is on a slope, then that will be accounted for.
local goalObject = CFrame.lookAt(
	Vector3.new(),
	workspace.Tank.Base.OrientationPart.CFrame:PointToObjectSpace(mouse.Hit.Position)
)

-- For completeness's sake, the same thing, but in world space; if the mouse is in front of the tank, and the tank rotates, then this direction stays the same, it's still pointing from the tank to the mouse.
-- This is what you'd put in e.g. a BodyGyro to make A point toward B.
local goalWorld = CFrame.lookAt(
	workspace.Tank.Base.OrientationPart.Position,
	mouse.Hit.Position
)

---

local y, x, z = goalObject:ToOrientation() -- not 100 % sure on the order of the components
3 Likes

You’re right, it’s the difference in height between the camera and the mouse position. If you draw a line on your second image from the blue to the red you’ll see the issue right away.
I don’t think it’ll be really accurate, but if you make the last line:
gun.HingeConstraint.TargetAngle = math.deg(x+5) --this moves the cannon up and down
Or whatever value works best for most of your angles.

1 Like

This would work with the default mouse and camera settings, but in my case the mouse is locked to first person, so the mouse can’t move freely from the cameras focus.

I tried this method before, and it would just return a fixed value

This would work if The tanks orientation is exactly 0,0,0, but any change in the tanks orientation would cause the angle needed to compensate the offset to be changed

Could you ‘calibrate’ it to aim at the spot where you’d most likely be aiming the shell? If you figured it would need to be most accurate about 200 studs away then you could modify the added angle for that distance and see if the shorter and longer shots were affected too much.

It was just the simplest fix I could come up with but I knew there’d be aiming issues.

i did some tweaking mostly to the positions and orientations to the camera part and barrel, and this seems to work. The only issue is once the mouse left button is clicked, the X value changes (example, it would be reading 10 degrees, then once the mouse is clicked, the value drops to 3 degrees and drops the cannon with it)