How to check if a 2D object is ahead of the player?
I know there are a lot of questions like this one, but I tried many of the solutions already posted and they never worked for me. I'm trying to doing something similar to raycasting with a 2D cone. If the object is ahead of the player, then do this. Otherwise don't do anything. I know of the Vector Dot method and I really like the idea of it, but there is one issue. My game is top down and let's say there are four animations. Walk Left, Right, Up and Down. Vector Dot works because there is a fixed direction. transform.up, for those people it's always the same. But my direction has those four options. How do I check if an object is ahead of me, if my direction is constantly changing. Is there a better way?
Answer by bracciata · Aug 26, 2015 at 10:53 PM
Have this script on the object void update{ if(transform.position.y < player.transform.position.y{ execute code } }
I appreciate the effort but I'm afraid this won't work for a few reasons. The main issue I am having is that the player changes directions. I could adjust your code for each direction, which is possible is in my opinion sloppy. If I was moving down the player would be facing downwards. This means that if the transform.position.y is greater than objects ahead of it. Same thing with right and left movements and the X axis. This may be semantics but onto my next reason. This would be working with rectangles. I'm trying to make it in a 2D cone.