How to assign raycastParams after ray is defined

I want to add a system that detects whether a zombie is in the middle of the screen or not.

But how do you add raycastparams after ray is created? i tried ray.RaycastParams = params but it errored. I used

cam:viewportPointToRay(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2)

to get the ray, but how would i assign raycastparams.

I didnt find anything saying about this but i might just be extremely dumb.

It’s not a matter of being “extremely dumb,” programming can be tricky sometimes! I’ll do my best to help you out.

To assign RaycastParams to a Ray, you can create a new RaycastParams object and then assign it to the RaycastHitParams property of the Ray. Here’s an example:

local cam = game.Workspace.CurrentCamera
local ray = cam:ViewportPointToRay(Vector2.new(0.5, 0.5))
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {game.Workspace.Zombie}

local hit = game.Workspace:Raycast(ray.Origin, ray.Direction, params)

if hit then
  print("Zombie detected in the middle of the screen!")
end

In this example, I created a new RaycastParams object and set its FilterType to Blacklist and its FilterDescendantsInstances to an array containing the Zombie instance in the Workspace. This means that the ray will ignore the Zombie instance when checking for collisions.

Then, I used the Raycast method on the Workspace to check if the ray hit anything. If it did hit something, the hit variable will contain a RaycastResult object, which you can use to get information about the hit object and the location of the hit.

I hope this helps! Let me know if you have any more questions.

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