Make player get damaged when looking at an object

I am trying to make a slenderman game and I want it so that when a player has his body or like his arm or something inside there Camera View then I want it to damage them. Is there any way I could do this?

3 Likes

You’ll have to check the orientation of the camera, then look at the LookVector and check if Slenderman is anywhere in that (straight) direction. Also, this isn’t a place to ask for free code so please refrain from doing that :slight_smile:

2 Likes

Yup, I learnt that the hard way… But is there any tips on how to do this?

1 Like

Not really, but CFrames have a .LookVector property like I said. If you don’t really understand CFrames, I recommend looking at the tutorial by AxisAngles.

3 Likes

you can use WorldToScreenPoint: Camera | Documentation - Roblox Creator Hub

local camera = workspace.CurrentCamera -- the current camera being used by the player
local screenPosition, isOnScreen = camera:WorldToScreenPoint(desiredPosition)

isOnScreen will tell you if the vector3 desiredPosition is on screen or not, you could then use remoteevents to tell the server if the player is looking at slenderman or not, and go from there.

Although, this doesnt account for if slenderman is behind a wall or something, so you would then use raycasting from the camera to slenderman to see if there are any objects in the way. If there are none, then he is in view

Heres a tutorial on raycasting if you dont understand that yet: Raycasting | Documentation - Roblox Creator Hub

6 Likes

Ok this is getting really complicated now.

1 Like

This would be terribly easy to exploit.

2 Likes

I think this Orientation property can do the job

1 Like

This math is more complicated. I recommend 3D Math Primer for Graphics and Game Development, second edition by Fletcher Dunn et al (if you are okay with reading about math).

Otherwise, the post by @asbuff seems like an easy solution.

1 Like

???

What does that mean :joy:?

1 Like

This reply is highly unnecessary and just looks like an attempt to start an argument.

WorldToScreenPoint is not replicated, therefore this information is fully controllable by the client thus making such a system rely on this function highly exploitable. Nor is there any way to make this method expoit-proof either. Due to this, your method is not contributive to the OP’s question and was something I found necessary to bring to their attention.

The best way to check this would probably not be by looking at the camera’s CFrame but by using the ScreenPointToRay or WorldToScreenPoint functions under Camera.

You should probably check those out and see if you can use them.

ah i see, true that it is fully controllable by the client. You could put some sanity checks on the server but you do have a point here.
sorry if i came off a bit aggressive in my reply, wasnt supposed to be in any way.

I’m not sure if anyone proposed this, but, you can find the dot product of the two LookVectors: Camera LookVector and the Slender’s LookVector (preferably of the root part) and verify if it’s 1 (which means they’re facing each other). Here’s some sample code of what I mean.

local SlenderModel = SlenderModel:WaitForChild("HumanoidRootPart")
local HRT = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")

local function isFacingSlender()
     return SlenderModel.CFrame.LookVector:Dot(HRT.CFrame.LookVector) == 1
end

RunService.RenderStepped:Connect(function()
    if isFacingSlender() then
       game.Players.LocalPlayer.Character.Humanoid:TakeDamage(.1) --arbitrary
    end
end)

Fully client side :smiley:
Who doesn’t love math right

1 Like

What if you back it up with some math like dot products on the server to combat the WorldToScreenPoint being unreplicated? Though it’s probably just as safe as using math on the server anyways.

1 Like

Doesn’t work.

It doesn’t give off an error either.

Try a ray that is given off from the front of a player, if this hits slender man then that user takes damage

How would I do that :joy:

I really don’t know how to do it.

Also, I edited to script to this:

local SlenderModel = workspace.Slenderman.Torso
local HRT = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")

local function isFacingSlender()
     return SlenderModel.CFrame.LookVector:Dot(HRT.CFrame.LookVector) == 1
end

RunService.RenderStepped:Connect(function()
    if isFacingSlender() then
       game.Players.LocalPlayer.Character.Humanoid:TakeDamage(.1)
    end
end)

HE DOES NOT HAVE A HUMANOID ROOT PART BY THE WAY!

I could insert slenderina right?

The one I inserted has a HRT