I want to check if an object is facing towards another object. I'm wondering if there is a method for this or if you can covert something like turnTowards() to boolean.
I want to check if an object is facing towards another object. I'm wondering if there is a method for this or if you can covert something like turnTowards() to boolean.
Two alternatives without too much math involved here:
A:
– save current rotation
– turn towards the other object
~ compare saved rotation with new rotation. If it changed by less than x degrees, you could say that were facing each other. (Make sure to not miss that 0°=360°)
– set rotation back to saved rotation
B:
– save current location
– keep moving forward and check if the object touches the other object. It they do, they were facing each other
– set location back to saved location
If the distance between the objects matters in your case, you can include that with both options
turnTowards(int, int) cannot return something, because what should it? There are multiple things imaginable here. For example, which is what I think you thought of, return weather the object was looking at that location before anyways. But it could also return the angle in degrees it has rotated to face that object, or the new rotation after turned towards that location, or weather the locaiton given was actually valid to look at and not the objects location itself, or...
You see, it really did not make any sence if you could convert void methods so that they return a value. If you want that information, you will have to use a different method, possibly created on your own.
In this case I would also reccomend the system A from Super_Hippo. Option B will be quite intense on the cpu and also be limited to a certain distance, because you don't want the object to keep moving forever if it does not hit anything. Another way would be to use my raycasting system (check my scenarios of you're interested) which can calculate this stuff pretty efficiently, but that's also way more complicated than A.