- Home /
Raycast goes through
I don't know where is the problem, ray just goes through the wall. Layermask is checked correctly and origin is behind the wall. But it still goes through the cube. I want enemy AI not to shoot if there is a wall. Code:
public virtual bool SeeingAlligned()
{
if (seeingOrigin != null)
{
Ray ray = new Ray(seeingOrigin.position, seeingOrigin.forward*attackDistance);
Debug.DrawRay(seeingOrigin.position, seeingOrigin.forward*attackDistance, Color.blue, 2f);
return Physics.Raycast(ray, out hit, attackDistance, playerLayermask);
}
else
{
Debug.Log("No ray origin for line of sight check");
return false;
}
}
Answer by NPnpNPnpNP · Jan 27, 2021 at 04:46 PM
The problem is in this line :
Physics.Raycast(ray, out hit, attackDistance, playerLayermask);
.
In the second screenshot, we can see that you selected the "Default" layer for the wall.
You may want to change playerLayerMask
to Physics.AllLayers
or if you want to ignore the playerLayerMask, change it to ~playerLayerMask
.
Check this link for more details.
Yup it worked. But 1 question though. Why should I change that thing? Shouldn't the ray detect only the specific encoded layer?
Yes, but if you select the "Player" layer, it will only detect objects of this layer, even if there is a wall in front of it.
Basically, it ignore all objects that are not in the specified layer.
In this example, the ray is going through the wall as if it dont exists, so it will detect the player even if there is a wall between the enemy and the player.
Your answer
Follow this Question
Related Questions
Raycast bullet collision problem 1 Answer
How do I check for collisions when I move objects based on Time.deltaTime? 3 Answers
how detect "OnRaycastOut" 1 Answer
Fall collision force with help of raycast 0 Answers
Raycast not visible 1 Answer