Is there a way to detect if there's something above player's camera

I want to make a system so when player’s camera is under something some sound plays and if above player’s camera there’s no object different sound plays? how can I make it? And is there even a way to make it?

4 Likes

I think you can do It by getting the Camera.CFrame.Position and comparing the Camera’s Y axis with the part Y axis.

local Camera = workspace.CurrentCameta
local Part = workspace.TestPart

if Camera.CFrame.Position.Y > Part.Position.Y then

elseif Camera.CFrame.PositionY < Part.Position.Y then

--[[
Basically, if your Camera Position.Y is 50, and the Part Position.Y is 30, 
50 > 30, play the sound 1
If the camera Position.Y is 30 and the Part's Y is 40,
30 < 40, play sound 2
--]]
4 Likes

Thank you for the code and explanation :slight_smile:

1 Like

@flamenco_687 and I want to ask one more thing should I place it in starter character scripts?

1 Like

Hmmm… May I see the whole code you made?

1 Like

you posted it
local Camera = workspace.CurrentCameta
local Part = workspace.TestPart

if Camera.CFrame.Position.Y > Part.Position.Y then

elseif Camera.CFrame.PositionY < Part.Position.Y then

–[[
Basically, if your Camera Position.Y is 50, and the Part Position.Y is 30,
50 > 30, play the sound 1
If the camera Position.Y is 30 and the Part’s Y is 40,
30 < 40, play sound 2
–]] this is the code you posted

Oh in that case yeah, put it into StarterCharacter Scripts

1 Like

thank you for your help and explanation

Do you want to know if the Camera or the Avatar is under the object? Since most game setups allow you to move the Camera away from your Avatar this may not work precisely.

If you are dealing with the Player, how about using Raycasting to check for items above the Player?

1 Like

I wanted to check if there’s something above player’s camera but thanks for advice of using raycasting

@flamenco_687 and I want to ask you is there a way of checking if player’s camera is inside a part?

You already have got answers in this post

1 Like

The issue I was asking about is what if you zoom your camera away from your player when you’re in 3rd person view?
Do you need to know if the Part is above the camera, or the player?

the camera. I want to make so when player’s camera is inside a building some sound starts playing and when player’s camera is outside different sound starts playing.

I don’t think you understand my question.
If the player is inside the building but they zoom the camera out through a doorway, do you want the inside sound playing because the player is still inside, or the outside sound playing because the camera is outside?

2nd variant outside sound when camera is outside, doesn’t matter where the player is
like in this video.
robloxapp-20200821-2021145.wmv (1.7 MB)

1 Like