0
\$\begingroup\$

I want to know if a gameobject (a plane) is facing another one to prevent collisions. Right now I have an array that contains all planes in the scene and, for each one, compare position in order to detect if is facing it or not. The number of planes is small, but I want to increase it and this method is a little... Mean.

I was thinking to implement a raycast to detect if the gameobject has another one in front of it. Is it worth? Will it be less expensive? I think that raycast it's very expensive. In addition, I think that I will have to run three raycasts for every plane.

Obviously this is not executed every frame, but every two seconds.

\$\endgroup\$
  • 1
    \$\begingroup\$ If your objects are only planes, we can do simple vector math checks which may be cheaper than arbitrary raycasts. An image, diagram, or video of the kind of situation you want to detect would help in figuring out the most appropriate method. \$\endgroup\$ – DMGregory Nov 6 '16 at 2:37
  • \$\begingroup\$ Yeah, I'm doing vector math by now, but I have to do it with every gameobject in the scene, and I was thinking that maybe raycasting is less expensive \$\endgroup\$ – STK Oscar Nov 6 '16 at 9:19
  • \$\begingroup\$ Unity does have built-in acceleration structures for this, so it may be faster in some circumstances — hard to know without profiling. Have you tried some tests each way to compare their real costs? \$\endgroup\$ – DMGregory Nov 6 '16 at 13:51
0
\$\begingroup\$

Both situations that you are describing will be expensive. Having multiple objects casting 3 raycasts each every 2 seconds can have very negative effects on your game optimization (depending on the number of objects you are doing this for). An idea I tried a while back for something like this is I used box colliders on objects that I didn't want to collide. Making the box collider a certain size bigger than the object itself can basically let you use an OnTriggerEnter() call to determine if they are getting too close since the method will be called before any actual collision with the objects would have happened.

\$\endgroup\$
  • \$\begingroup\$ Mmm.. I get the idea but I think unity also has to check for every gameobject in the scene the collider position and size to determine if the gameobjects are colliding, Unity has to have an array with all gameobjects like I'm doing (or something like this), so I think that this will be more expensive (and maybe more accurate) that my method, isn't? But your answer is enough, raycasting is to expensive. Thank you \$\endgroup\$ – STK Oscar Nov 6 '16 at 9:31
  • \$\begingroup\$ @STKOscar It isn't necessarily that raycasting is expensive, its just going to get expensive when it is constantly done by multiple objects. If you find this to be a fitting answer please accept it. Thanks. \$\endgroup\$ – saboehnke Nov 7 '16 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.