x


Colliders vs RayCast (automatic crosshair aiming in an FPS game)

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)?

Thanks

more ▼

asked Dec 11 '09 at 05:06 PM

stubbornmoose gravatar image

stubbornmoose
54 3 4 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 11 '09 at 07:52 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

fair enough, but I am wondering the same question but without any screen interaction, so what is actually faster? raycast or trigger?

Dec 27 '10 at 12:24 PM Jean Fabre
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2000
x472

asked: Dec 11 '09 at 05:06 PM

Seen: 2734 times

Last Updated: Dec 11 '09 at 05:06 PM