Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

TextLabel rotation Pointing to part by the angle of the player.pos-part.pos?

what im trying to make is a hit damage indicator like COD or any FPS Shooter

Here is a image you can look at https://prnt.sc/i6mgrc

2 answers

✓ Accepted answer

I'm not amazing at math, so there may be a better way to do this, but this is what I came up with.

-- The distance from the center of the screen the hit icon will be
local fromCenter = 60

local gui = -- Set this variable to your ScreenGui

local function CalculateShotDirection(shooterPosition)
	local character = game.Players.LocalPlayer.Character
	if character then
		local rootPart = character:FindFirstChild("HumanoidRootPart")
		if rootPart then
			local screenCenter = Vector2.new(gui.AbsoluteSize.X / 2, gui.AbsoluteSize.Y / 2)

			-- Get the world space direction of the shot
			local shotVector = (shooterPosition - rootPart.Position)
			local shotDirection = Vector2.new(shotVector.X, shotVector.Z).Unit

			-- Project camera look vector onto the XZ plane and work out it's rotation
			local projectedCamLook = Vector2.new(
camera.CFrame.lookVector.X, camera.CFrame.lookVector.Z).Unit
			local camAngle = math.atan2(projectedCamLook.X, projectedCamLook.Y)

			-- Rotate shot direction by the angle of the camera
			local rotatedShotDirection = -Vector2.new(shotDirection.X * 	math.cos(camAngle) - shotDirection.Y * math.sin(camAngle),
				shotDirection.Y * math.cos(camAngle) + shotDirection.X * math.sin(camAngle))

			local angleRadians = math.atan2(rotatedShotDirection.X, rotatedShotDirection.Y)

			return screenCenter + (rotatedShotDirection * fromCenter), -math.deg(angleRadians)
		end
	end
end

You need to pass this function the Vector3 position of the shooter. It will return two values. The first value is the position of the hit icon, the second is the rotation in degrees that the hit icon should be rotated to. An example of calling it to set the position of a TextLabel or ImageLabel would be like follows: (I'm omitting any code to show or fade out the label for brevity)

local position, angle = CalculateShotDirection(Your shooter position here)
label.Position = UDim2.new(0, position.X - label.Size.X.Offset / 2, 0, 
position.Y - label.Size.Y.Offset / 2)
label.Rotation = angle

Hope that helps.

1 comment

0

Thank you again, i wonder how you do this. someday you should enlighten me

No clue if it would work but you could try something like this:

local look = (playerWhoShot.Position-localplayer.Character.HumanoidRootPart.Position).unit
look = look * Vector3.new(1,0,1)
local cf = CFrame.new(Vector3.new(0,0,0),localplayer.Character.HumanoidRootPart.Position + look)
local rot = {cf:toEulerAnglesXYZ()}
gui.Rotation = math.deg(rot[2])

There are probably better ways to do it if it does work though.

This discussion is preserved as it was. New answers, comments, and votes are closed.