Raycasting for Days

Jared Amlin
2 min readDec 16, 2020

I have easily spent the past two days trying to make sense of Raycasting2D, and I think I finally have it all figured out….maybe. If anyone else needs gets stuck on Raycasting2D, I hope this blog entry can help save you some time and frustration. I will go over the walls I ran into, and some of the ways to get around them. My last blog entry went over some other problems I had understanding what to expect with Raycasting. I was using Raycast2D, and expecting the results of RaycastAll. With standard Raycasting, you need to be more careful about what you are targeting, if you want to hit it and get some returned value on the hit.

I have been trying to get an enemy ship to detect my player with a Raycast, unsuccessfully. The ship keeps detecting itself, even though the Unity API states that the Raycast won’t ping the object doing the Raycasting. I got some help yesterday to look for the target player by it’s tag, “Player”. While this does work great, it was not working for me. Nothing I tried was working. I tried layermasking to tell my Raycast to only cast on the Player layer, where only the Player is located, to no avail. The problem and solution was this. Unity has a layering system similar to Photoshop called sorting layers. At some point in time for the best visual effect, I moved my player to the foregound layer and set it to sorting layer #2. Apparently, and this is important, Raycasting will not detect anything on a different sorting layer! As soon as I moved the player back to sorting layer 1, the same as my enemy, the detection worked!

On to another issue. While my ship is now detecting the player, it is also still detecting itself. As it turns out, having children components on my ship was making the Raycast hit the children components. Now that the sorting layer had been figured out, I went back to trying the layermasking and this worked perfectly. I had to declare a variable up top for my layer mask, [Serialize Field] private LayerMask playerLayerMask. Then I went into my instpector to find the new area where I can assign a layer to target. I chose the Player layer, and since my player was already on a new layer named “Player” the Raycast now only detects the player. Perfect!

Also to note, I did have to add the LayerMask into my Raycast to get it to work.

RaycastHit2D hitRightInfo = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.right), 25f, playerLayerMask);

Now that I am detecting items correctly, I can finally get to finishing these checklist items where Raycasting is paramount. If anyone is having trouble Raycasting, ensure that you sorting layers are the same between Raycaster and target, as well as make sure you don’t have any children components on the Caster taking the hit unintended.

I am excited to get back in there and push forward again. This has been such an amazing learning opportunity so I very much hope that the program continues uninterrupted.

--

--

Jared Amlin

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.