- Home /
Check if there are any object "behind" main object?
So I'm working on a new game, but I'm pretty new in unity (especially in c#), so I'm trying to figure out a way to solve this issue I have, and I'd like some help.
I have a 2D sprite (the player) that always faces the camera, and a 3D environment which is also made out of 2D sprites in different angels. You, as a player, can move the player, and also rotate the camera, using a controller. In this game, you lose if you get out of the shapes. the thing is, Im not sure how to check for that scenario. how do I check if the player got out of the shapes? how can I check if there are no other objects behind it?
EXAMPLE (sry for the link, the file upload server seems to be down) - so in that case, the player supposed to lose when he gets out of the red shape.
I thought about raycasting, but I'm not sure how does it work exactly, so If that's a good idea let me know how to do that exactly.
Thank you in advance!
Answer by ShaiDayan · Apr 03 at 10:26 PM
OK, I finally solved it using raycasting, and adding thin 3d colliders to the planes
private void Update()
{
if(Physics.Raycast(player.transform.position, player.transform.forward, out hit, Mathf.Infinity, ~playerLayer) || Physics.Raycast(player.transform.position, -player.transform.forward, out hit, Mathf.Infinity, ~playerLayer))
{
//Still in the game
}
else
{
//Lose
}
}
Your answer
Follow this Question
Related Questions
Need help to convert this raycasting 3d Script into a 2D x&y version 0 Answers
Bullet collision help 1 Answer
How do I use Raycasting to return gameobject 2 Answers
Ray Detecting without colliders 1 Answer
Raycast hits trigger 3 Answers