I had trouble understanding Dot Product in code mainly due to these reasons:
- Vector zero.
- Understanding what vectors are.
I tried making a simple script that detects when an object is behind another but I ended up making two big silly mistakes:
- One of the objects was located at the origin of the world (0, 0, 0), this will return 0 when using dot product.
- I used positions, which lead to some crazy results.
After all my experimentation I looked at Unity’s documentation, there I found a great example of how to use dot product correctly, yet I wasn’t able to get my head around it, I kept looking at vectors as positions instead of lines with directions, so I draw a graph to see what was going on, everything became clear after that, I suggest you try drawing if you are having issues with vectors and how they work.