Colliders vs RayCast
I have to implements automatic crosshair aiming in my FPS game. What is the best (performing on iPhone) solution for that?
I can see two options here:
1: Using Triggers:
Ex: Adding an additional collider to the player object in shape of a long stick, potentially long sphere) and a rigid body marked as kinematic Detecting OnTriggerEnter/Exit between player and enemy colliders. After that use a linecast and if there are no objects on the way moving crosshair based on enemy position.
2: Using RayCasting:
Ex. Use couple (at least three) raycasts in a direction coming from camera, detect object on their way using Physics.Raycast Once enemy has been detected move crosshair based on its position.
So, what is the most expensive operation? Using rigidbody with OnTriggerEnter/Leave handling with one LineCast or 3 raycasts (with additional layer)?
asked Dec 11 '09 at 05:06 PM
If your goal is to aim your crosshair at the closest enemy, if that guy is close enough, you could solve it much simpler, by using Camera.WorldSpaceToScreenSpace() and getting the screencoordinates of this guy. If it's close enough to the middle of your screen, you can rotate the camera/crosshair more towards the guy.
Doing triggers and raycasts seems like very performance heavy operations that don't actually get you want you want.
answered Dec 11 '09 at 07:52 PM
Lucas Meijer 1 ♦♦