Detecting When Player Raises and Lowers Camera

I want to be able to detect when the client looks up/down/straight.

So far I tried monitoring the player’s Head’s CFrame, the Head’s CFrame’s LookVector, the camera’s CFrame, and the camera’s CFrame’s LookVector by printing them and seeing which values changed as I moved the camera up and down. Couldn’t find anything that resulted with what I needed. Closest thing I got was monitoring the camera.CFrame.YVector, but it gives the same data if I look straight up, or straight down.
Looking straight up:
image
Looking straight down:
image

Question: How do I monitor when the player looks up or down? (First Person Game) Just need a generalized method.

2 Likes

I think I’ve seen something that does this, wait just a minute, I’ll go find it.

Edit: Okay, I’ve found it, try manipulating this code that detects when your’e looking certain places and creates a ray from the sun.

Code

wait(1)
— Made by Zued
local p = game.Players.LocalPlayer
local mouse = p:GetMouse()
local blur = Instance.new(‘BlurEffect’) blur.Parent = game.Lighting

while wait() do
local ray = Ray.new(p.Character.Head.Position, (p.Character.Head.Position - mouse.Hit.p))
local ignore = game.Players.LocalPlayer.Character
local hit, position, normal = workspace:FindPartOnRay(ray, ignore)

if (position - p.Character.Head.Position).Magnitude >= 30 then
	blur.Size = 5
else
		blur.Size = (position - p.Character.Head.Position).Magnitude/3
	end
	
end
1 Like

Hmm, I tried it but it has the same problem. Looking straight up and straight down yields the same value, since after it passes the midpoint the numbers start decreasing.
Looking straight up:
image
Looking straight down:
image

It does have a bit more functionality than what I was doing before, so thanks.
I just have to somehow differentiate when the player is looking up and down, since they give the same values.
If anyone has any other solutions, that would be great, otherwise I’ll just try working with this, or maybe disable looking up.

Camera.CFrame is the way I use for my FPS game and it works fine, Can I see your code?

2 Likes

I’m not too sure about this, but you can use the dot product.

--quicc mafs
function UpOrDown(LookVector)
	local LookVector = LookVector.Unit
	local UpVector = Vector3.new(0, 1, 0)
	
	--Should return a value within range [-1, 1]
	return LookVector:Dot(UpVector)
end

If value is positive then you are looking up. If value is negative then you are looking down. See if this helps

5 Likes

Sorry for late response, but the code isn’t too helpful lol, its just me printing a bunch of different CFrames to see which one correlates with the value I want to track.

This prints exactly what I wanted, thanks a bunch for the help.
Never knew about the Dot product.

Going off of what space dice said, if you have a part directly in front of the player and they look up, you can use the Dot product to get what direction they are looking at.

1 Like

If anyone is looking at this in the future, I ended up using

function UpOrDown(LookVector)
local LookVector = LookVector.Unit
local UpVector = Vector3.new(0, 1, 0)
return LookVector:Dot(UpVector)
end

while wait() do
print(UpOrDown(camera.CFrame.LookVector))
end

And then of course you can manipulate the code into whatever you want.

1 Like

In general, vector:Dot(UpVector) is the same as just vector.Y.

In other words,

print(workspace.CurrentCamera.CFrame.LookVector.Y)

and

print(workspace.CurrentCamera.CFrame.LookVector:Dot(Vector3.new(0, 1, 0))

are the same.

i.e.

local function UpOrDown(LookVector)
    return LookVector.Y
end

(note that the dot product is more useful when you’re trying to determine how close a vector is to pointing in a specific direction – but when that direction is “up”, you can simplify things like this)

4 Likes