- Home /
Vertical Auto Aiming - whats the best way to do it?
Hi guys,
I'm working on an easy shoot 'em up game for mobiles. According to the limitations to two thumps controlling i need a vertically aiming help. Like in Doom1&2 for example.
My idea right now is to use a collider that scans by tag for enemies and set the next to the player (if any) as the target. I'm using raycasts to shoot...
I'm wondering if this would be to expensive for an android game and if there is a better way. Like shooting more rays up and down and take the one that hits something - with fallback if no enemy was hit.
what do you thing would be the fastes solution?
You could check if the enemy gets rendered and if yes, then check the distance. The lowest distance would be like magnet. And you should make an custom cursor, that would normally go towards mouse but when an enemy is rendered and its distance from player is the lowest one drag the vertical position of the cursor towards it. Long story short. If you wanna let the player control both mouse axis, you have to disable the vertical one when you wanna drag, when not, it is much easier. You just let the player move the horizontal axis and when an enemy is rendered and the distance is the lowest, drag the vertical stuff...
sadly, there is no mouse - it's a mobile game on Android and iOS devices - you just have one virtual stick to rotate and walk forward + backward and a fire button. so you only hit the enemy if its on the same level +/- it's height... if you played doom you may noticed, that enemies in front of you but on a higher level are shootable. this is what i'm trying to archive the fastes way (performance side). sry if i'd not ponited out that clearly ;)
I will post how i did it when it works maybe there is a better way than what i'm dooing right now. or anybody can profit from it...
Answer by aldonaletto · Nov 02, 2011 at 10:18 PM
That's not an easy task with such limitations. Your idea is good, and I think it can be less expensive if you place all enemies in some user layer, than use Physics.CapsuleCast in this particular layer to find only enemies: CapsuleCast projects an imaginary capsule in the direction and distance specified, and returns the first collider hit, thus all you have to do is shoot the collider returned, if any.
Ideally, the volume cast should be pyramidal; to get something slightly more similar to this you could do a short CapsuleCast with a smaller capsule, and if nothing found do another CapsuleCast, this time with a larger capsule and starting where the smaller one finished.
This is great! I'd just know about Physics.SphereCast...
I was about to use a pyramidal mesh collider using trigger, but this would be two steps more including the distance check...
Using a capsuleCast i can shot up ... maybe 3 times - each time a bit taller. and the thing with the separate layer is great too. Thanks a lot - i'm on it :)
Life would be much easier if Unity had a kind of Physics.PyramidCast!
Answer by gfr · Nov 02, 2011 at 11:40 PM
Instead of casting, i think your approach could be modified (and should be inexpensive depending on the enemy-count/-density):
Keeping a list of enemies inside the collider attached to the player and then filter that one (e.g.: for those being within a certain horizontal angle from the reticule, shoot the center- & front-most of those).
With few enemies that should be rather inexpensive, but then again i haven't done measurements on those... Building a few test-cases (casts vs. book-keeping/filtering) and measuring how much time they take over a few hundred iterations should be relatively easy though.
That's an good idea as well - i will check this out tomorrow cause it's in the middle of the night here. I actually don't have many enemies on screen at once, but i have enemies spawning. This could be difficult to be combined. In addition i think there is a need of casting anyway because of cover/obstacles... best approach would probably be an mix to reduce the expensive stuff - thanks
The best way to keep an enemy list is to let Unity do it for you: have an empty object called "Enemies", and any time you spawn a new enemy, child it to Enemies. You can iterate through all Enemies children to check if the enemy is visible (what means it's in the viewing angle), then check its position. This list is cheap because Unity keeps it up-to-date for you, deleting every destroyed enemy automatically.
Anyway, I suspect the CapsuleCast method is less expensive, because all calculations are done by internal routines written in C++ or other compiled language, which is much faster than .NET
Hm, i would have thought casting to be more expensive, but than again i don't know how the physics engine implements it. I guess i will have to test it myself when i have the time.
Good point on the enemy list.
The enemy list approach isn't this cheap because you have to iterate through all elements, and if an enemy is inside the frustum (isVisible == true) and shooting angle, do a Raycast in order to make sure it's not hidden behind walls or other obstacles, then finally select the nearest enemy calculating the distances. $$anonymous$$aybe it's cheaper than the CapsuleCast, but only testing both alternatives we can be sure.
True, but the idea was that the angle-check should be rather cheap. If you're casting in a enemies-only-layer you'd also have to apply a visibility-check afterwards.
Your answer
Follow this Question
Related Questions
Mobile Device Video Camera 1 Answer
testing on an android device 1 Answer
Mobile device textfield keyboard keyset - default to numbers 0 Answers
MOBILE - FPS Camera Rotation HELP 0 Answers
testing on an android device 0 Answers