Getting my head around Dot Product

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.

Great work solving your problem @Yee!
I also find drawing out quick diagrams and doing some back-of-the-envelope calculations are a great way to quickly debug what should be going on in your code.

1 Like

Can I do this stuff using dot in unity2d/3d? @garypettie

the enemy only attacks in 45* angle and range is 3f, if the player position in this range or angle enemy hit them.

Question sketch :

image

and how it’s looks in unity by debugging.

image

Privacy & Terms