Jump to content

Line-Of-Sight targeting in a 2-D platformer


graycontrast
 Share

Recommended Posts

I am looking to implement a line-of-sight targeting in a 2-D platformer where the player presses a 'ready gun' button, moment is disabled, and a list of hitable targets is made and you can cycle through the targets.

 

post-17593-0-81173500-1471661323.jpg

 

 

Is there a way to use ray tracing or the pick command in a 2-D area that will stop at every non-target object?

 

If I can get those results I can make an array of targets to cycle through them.

 

What is a good way to start?

Link to comment
Share on other sites

Actually, volumetric shadows (or lighting) does something like this by deforming meshes using vertex shaders. However, that's way too complicated for this.

 

You'll want to avoid casting rays from the player. Evenly spaced angles will generate too many rays for regions where there's no target. Do the inverse instead. You can make some sort of box around the targets, and then pick from various intervals between the top and the bottom of the box. This will give you a decent estimate. Otherwise, there's an infinite number of rays you would need to check.

 

Pick doesn't stop at the first object. Internally it generates a list of objects since it is calculated out of order. You'll have to use multiple picks no matter what.

Link to comment
Share on other sites

But doesn't Pick just stops at the first object in a cylinder? is there a way to do it in a 2-D area or will I have to use multiple pick commands in evenly-spaced angles?

Check the pick docs. You can set closest to true and it will always get the closest object, which is what you want.

 

I would shoot a ray from gun to a few bone positions in your characters like head, feet, hands, etc. Say 9 or so rays per person. If any one of them hit, you have sight of the character and can shoot him. Even if this is a little on the slow side, since you said time stops while you aim, it should be fine.

 

Edit: You can probably shoot rays to only one side of the person and be fine since this is a side-scroller, saving some rays. No need to do both hands, both feet, etc. In fact, you might be able to skip the arms/hands altogether, depending on what your characters are doing.

Link to comment
Share on other sites

So a good way to start is say have a pick from the top and bot of the character? That should be a cheap way to get LOS, I don't plan on having any narrow holes you can shoot through. Should I save that information as vertices in the bad guy script? and to be more efferent I don't want to have to check every bad guy (like ones behind the character or way way down the map), just the ones in a certain range. Is there a way to collect the objects in a certain range without iterating through all the entities in the world?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...