Overlapping colliders with Raycasting in 2D
This is the first time I've ever resorted to posting on the forum because I really wanted to figure this out for myself, but I'm stumped and its been over a week.
I'll try to keep it simple;
I'm building a 2d isometric tile based game with walls that equate to 3 blocks high. relative to my character who is maybe slightly taller than 1 block. The game is also not a traditional dungeon crawler or adventure map, it's a tight quarters map taking place in a school. Because of this, walls are often overlapping other walls and objects as well as the player. I've been trying to create a see-through-walls system using a sprite sprite mask, and a script that basically says: - Fire Raycast at Player - If hitting Player set ViewBubble to inactive - If not hitting Player (hitting wall) set it to active
When its active, the sprite mask makes whatever is in front of it invisible, and disregards stuff behind it. (At least that's the plan, right now its a little messy)
The issue I'm having is that while the Raycast is confirming that it's hitting my Player, and separately also confirming that it's hitting the Wall, it only hits the Wall collider if I deactivate or move the Players collider leading me to believe it has something to do with sorting layers perhaps?
Anyone who could give me insight on this would be a godsend because I know it shouldn't be as difficult as I think it is. All I want is for the the Raycast to know which collider is in front of the other, or if that's not possible, maybe a different solution?
Code Below:
public Camera camera;
public GameObject target;
void Update() {
RaycastHit2D hit = Physics2D.Raycast(camera.transform.position, target.transform.position, Mathf.Infinity, LayerMask.GetMask("Obstructions"));
if (hit.collider != null)
{
Debug.Log("Just smacking up on some " + hit.collider.name);
if(hit.collider.gameObject.tag == ("ViewBubble")){
Debug.Log("Hitting View Bubble");
}
else if (hit.collider.gameObject.tag != ("ViewBubble")){
Debug.Log("Not hitting View Bubble");
}
}
}
}
Your answer
Follow this Question
Related Questions
hit normal vector from raycast is always [-1,0], which makes slope angle 90 1 Answer
Why doesn't my box collider work? (Unity2D) 2019.2.13.f1 0 Answers
How do you make one game object follow the shape of another game object? 0 Answers
How can i store all colliders of colliders which collide with a prefab? 1 Answer