# Raycasting on Z in 2D

I'm making an isometric game in Unity. So far I have managed to render a diamond shaped board, with tiles that are aligned on the x and y axis (and the camera looks down the Z axis).

I am spawning game objects for each tile with their own polygon collider and sprite renderer. I hope this is the correct way. They appear in front of each other naturally as you would expect them to in an isometric view.

I want to make a raycast from the player and forward on the z axis, to hit the sprite right under him. As you can imagine, I run in to problems when I do that, I imagine it's because they are all on the same 0.0f on the Z axis.

So my question is, does the raycast care about when a sprite is rendered in front of another, when it calculates what to hit, or is it just the physics engine handling this? And if so, how can I get around this? I guess I can translate the player coordinates to the board, but I just want to know how this works anyway.

• So I assign z in the world building process, and now my topmost tile is closest. In 3d space. This affects the 2D collider obviously, so that it works.. I am still interrested in learning why. Commented Jan 3, 2015 at 5:00
• To clarify, I am wondering why I am working with 3D raycasts to assert what tile the player is over. I'm sure there's another way? Commented Jan 12, 2015 at 21:26
• So, when I want to cast rays in 2D, I.E check if an enemy is somewhere in front of where the player is facing, with no obstacle between him, how would I do that in unity without having to write my own code for it? There must be a simpler way. Thanks! Commented Jan 13, 2015 at 13:40
• I'm not sure I understand, is the Z-axis out of the screen? In which case in 2D physics, surely a ray cast along the z-axis is just checking a point? Is this what you are asking or have I misunderstood? Commented Feb 3, 2015 at 13:28
• Yes, I just want to check a point Commented Feb 6, 2015 at 9:04

The 3D physics and the 2D physics are handled differently in Unity (for example 2D colliders do not collide with 3D colliders). As a result, 3D ray cast will not collide with 2D colliders.

However, you can mix and match so you could use a 3D raycast along the z-axis but you will need to attach 3D colliders to your objects.

Alternatively, if you want to check the collision of a point you can use the 2D ray cast of length 0 at the point you wish to check

Physics2D.Raycast(YourWorldPosition, Vector2.zero);

• Yes, I attached 3D colliders because I want to check what is directly below my player. If I have multiple layers rendered on top of each other, it does not sort them, it does not chose to collide with the topmost (the one rendered in front) tile, unless I use 3D colliders and Z space. This feels like an anti-pattern but I want to know how to do it. 2D collision will also return any tile below the player, not the topmost tile. Do I need to keep track of my layers and do the calculation myself? Thank you for this, I will vote on you when I get my rep :) Commented Feb 6, 2015 at 10:40
• I believe yes, you will need to manually sort by z (or else use 3D colliders) as I don't believe 2D has any concept of stacking in the 3rd dimension. It shouldn't be hard to sort them by z when you get your results and so is probably preferable to complicating the rest of the physics by using 3D colliders. Commented Feb 9, 2015 at 11:12
• Thanks, that will do :) I can easily sort the tiles when i place them, I just wondered if unity sorted them for us when we draw them or whatever. Thanks! Commented Feb 10, 2015 at 9:10
• I know this is not the answer to my question, but a moderator has deleted my own answer so I guess if someone has the same question you can PM me for the answer. Commented Nov 28, 2016 at 9:10

I dont know how raycast is implemented in Unity but it could very well be as you suspected, the raycast will not hit objects with same x, y or z. Imagine if you are implementing an overlap function that checks if (a,b) overlaps with (c,d), whether (1,2) overlaps with (2,3) is dependent on how you implements it.

• Aye, I could write such a method, but I'm not really sure that the raycasting is easily overridable in Unity. This whole thing making a 2D game in a 3D space is not really helpful :) Commented Jan 12, 2015 at 21:24