I spent quite a while experimenting with a simple line of sight function using Raycast2D and finally got something working as needed (I hope to post it somewhere because I couldn't find any tutorial that did what I needed).
Basically I have a top down RPG where I hide enemies that aren't visible to the active PC by firing a ray between them, possibly colliding with tilemap tiles in between. All good.
Then I needed something simple that I thought would take me 10 minutes and has eluded me all afternoon. So, the player can click on the tile they want to move to. I only allow them to move there if the tile is 'visible' (no wall tiles in the way). I thought I'd use the above method by creating on-the-fly a fake enemy, put it on that tile then use the already working line of sight function to see if it's visible.
It's just not working. The ray does not collide with the newly placed enemy. I suspect a timing issue, but I can't nail it down. I do use ForceRaycastUpdate (I'm using C# BTW). I also turn on collision shapes in Debug, and can see the ray is visually intersecting with the collision shape of the enemy..
I can post some code if need be. Any thoughts appreciated.
PS I'm happy to replace my method with something better if need be.
PPS Also posted in Q&A.
edit: Added a link to sample project
https://dropbox.com/s/rhp6s5mal18hhik/TestRaycast.zip?dl=0
The project has 3 pink skulls (enemies), a blue skull (player) and a 'wall'. There is a timer that when it fires, it casts a ray from the player to an enemy. If it collides, the enemy remains visible. If it doesn't collide with the enemy (hits the wall instead), it becomes invisible. Every timer event, the next enemy is checked. The timer is to slow things down.
What's not working is that when you click the mouse, it should decide whether that position is visible to the player or not. If it is it moves the player there. If not, it doesn't. To check this I instance an enemy node, use it for the visibility check, then get rid of it. It doesn't ever collide with the node in this sample.