How to raycast from position and check for wall regardless of lookvector

I’m wanting to to always check if there’s a wall to one side of a player

I have this, but it seems to depend what direction the player is facing. I don’t want this however.

local RaycastCheck = workspace:Raycast(
		Character:GetPivot().Position,
		Vector3.new(0, 0, RandomOffset == 1 and -20 or 20),
		Params
)

I want to know if there’s a wall next to a player, regardless of what way the player is looking, as i want to check if a player is behind a wall when another part comes through

1 Like

You probably need to change from Raycast to Region3

Raycasting always cast in a straight line no matter what. If you want to achieve that using raycasting technique, you may need tons of rays. instead please do consider using Region3

In order to check which side of the wall is to player, just manipulate Position or CFrame of the player to walls.

1 Like

I need to get if they are behind a specific wall on a specific side tho

If a player is say standing here, red block moves from left to right, the player should take no damage.
image

However if the red block is on the other side, the player should die.


Pretty sure Region3 would return true across both tests, as player is within range of a wall, even tho they aren’t on the right side

1 Like

Couldnt you just have the rayDirection set to Vector3.new(100,0,0) then change the 100 to -100 depending on which side red wall is coming from? Also just change 100 to a smaller number if you want player to be closer to the wall.

This is or more or less what I am already doing. It’s on the Z axis tho as the wall comes from the Z axis and that’s what

RandomOffset == 1 and -20 or 20

RandomOffset determines which side the wall comes from.

This however does not work, as it seems to be basing it on my players look direction as well, which I can’t have

Then use the combination of Region3 and Raycast. You will use Region3 to detect if you are close to the blue wall. Then detect which side the blue wall is at. Then shoot a ray to that direction, to see if you detect a red wall. If it detects, the player lives, otherwise, dies.

Would that be what you want to achieve?

Again, issue with that is what if there’s multiple parts detected on multiple sides of a player?

you’d get the dot product between the direction from blue to red and the direction from blue to player position?

If the dot product is near enough to 1 then they should be damaged since they are both on the same side.
They should not be damaged if the dot product is around -1 though.

That shouldnt be the case considering you are just shooting a ray in one direction instead of giving it players lookvector, are you sure there isnt some other part of the code messing with it?

This is what I have been thinking about it also. Since rayDirection can be from anywhere, then why is it shooting out from where the player is looking at instead?

I’ve tested it by calculating rayDirection like this and it works perfectly:

local wallpos = (wall.Position-redwall.Position).Unit
local rayDirection = Vector3.new(0,0,-20) * wallpos
1 Like