im trying to get a tank’s turret crosshair to line up with the player’s mouse position (which is locked to the centre of the screen) like so:
im using camera:WorldToScreenPoint() to get the crosshair to follow the muzzle of the cannon, but the crosshairs will never line up with the mouse’s position (the black circle). When the tank fires, it fires in the direction the mouse is pointing at, but it can cause confusion since the crosshairs show it would fire in a different spot.
heres the script i have for the crosshair:
local RunService = game:GetService("RunService")
local guiObject = script.Parent.ImageLabel
local part = workspace.Tank.Gun.Gun.Barrel.CameraFocus
local mouse = game.Players.LocalPlayer:GetMouse()
local camera = workspace.CurrentCamera
guiObject.AnchorPoint = Vector2.new(.5, .5)
guiObject.Visible = true
RunService.RenderStepped:Connect(function()
local worldPos = part.Position
local x, y, z = mouse.Hit:ToOrientation()
local pos, isOnScreen = camera:WorldToScreenPoint(part.Position)
local screenpoint = Vector2.new(pos.X,pos.Y)
if isOnScreen then
guiObject.Visible = true
guiObject.Position = UDim2.new(0,pos.X,0,pos.Y)
else guiObject.Visible = false
end
end)
any help would be appreciated!