How to track player camera movement

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? I have a bar in my game and want to increase/decrease the size of the bar depending if the player is looking up or down
    image

  3. What solutions have you tried so far? I’ve searched endlessly but could not find anything

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

5 Likes

1. Take the dot product of up vector and camera look vector.
2. Clamp it in range [0; 1].
3. Lastly apply this number to the absolute Y size.

local background_bar -- black one
local main_bar -- blue one

local d : number = Vector3.new(0, 1, 0):Dot(Camera.LookVector);
local a : number = (d + 1) * .5;

local y_size : number = (a * background_bar.AbsoluteSize.Y) / background_bar.AbsoluteSize.Y;
main_bar.Size = UDim2.fromScale(1, 0, y_size, 0);
2 Likes

I get an error saying LookVector is not a property of “Workspace.CurrentCamera” also what is “Camera” meant to be exactly?

1 Like

Update:
I’ve managed to do it heres the code:

local player = game.Players.LocalPlayer
local char = player:FindFirstChild("Character")
local hrp = char:WaitForChild("HumanoidRootPart")
local runService = game:GetService("RunService")
local cam = workspace.CurrentCamera

runService.RenderStepped:Connect(function()
	local camDirection = hrp.CFrame:ToObjectSpace(cam.CFrame).LookVector
	print(camDirection)
end)

Thanks for the help anyways!

Camera is the workspace.CurrentCamera.

Oops, it should be Camera.CFrame.LookVector.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.