How to make cursor point go through an object?

Hi, I’m trying to make a top down game where in a scenario where you enter the house, the roof would be transparent (client side). However, a problem I faced was that if I were to shoot in the house, it would shoot as if the cursor was pointing at the top of the transparent brick hence causing aiming issues.

The only solution I can see is to delete the roof and recreate the roof after exiting the building, but is there a way for me that doesn’t involve deleting the roof?

You could use raycasting and ignore the roof instance.

Can you elaborate more on this?

Raycasting Parameters allows you to blacklist some parts

This is the docs for raycasting

Here is the raycast Params datatype.

You can do

Enum.RaycastFilterType.Blacklist
and
RaycastParams.FilterDescendantsInstances = {model to be ignored}
1 Like

Well, I inserted this code

local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.FilterDescendantsInstances = {workspace.House.Roof}

And the results are unchanged… or am I doing something wrong?

Did you pass the raycastParameters through the workspace:raycast?

It sounds like you’re calculating the cursor’s position in the world incorrectly. I’m guessing you cast a ray from the camera forward until you hit something (such as the roof) and then use that hit point as the cursor’s world position.

Luckily you don’t actually need a raycast to figure out the correct world position of the cursor since your game is top-down. You can use a little bit of trigonometry to calculate it given the camera’s field of view and the depth (distance) the character is from their camera. Even better, there’s already some built-in methods to make this easier.

ScreenPointToRay creates a Ray using the screen coordinates of the cursor and a depth (which you already know). With this you can create a ray with an origin on the same 2D plane as your character.

local function getWorldMousePosition(x, y, d)
    return workspace.CurrentCamera:ScreenPointToRay(x, y, d).Origin
end

This function takes the coordinates of the cursor (x and y) and the depth d between the camera and the character, and returns the position of the cursor in world-space. This will be the correct aim position for your weapon.

GetPartsObscuringTarget may also be useful as, given a list of positions, it returns a list of parts that need to be made invisible for the positions you gave it to become unobscured. Specifically, you could use this to determine what parts you should hide when the player is in a building.

4 Likes

I am guessing that you are firing the ray towards the position of the mouse, so you should try setting Mouse.TargetFilter.

If you want a simpler solution, just move the roof to a folder instanced when a player joins the game in server storage, and move it back when the player exits.

1 Like

ReplicatedStorage* Being client side.