Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AutoAim: Automatic Turret Aiming system with aim-ahead for moving targets

Discussion in 'Assets and Asset Store' started by PlanetTimmy, May 8, 2012.

  1. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    You're right about the bullet speed - I should change it so that it doesn't just update it on start.

    However, I'm pretty stumped on the firing pause; the actual code to do the firing is really simple:

    Code (csharp):
    1.  
    2. if ((Time.time - m_lastShot) > m_firingPause)
    3. {
    4.     m_lastShot = Time.time;
    5.     Fire();
    6. }
    7.  
    Looking at it now, I should change it so that it won't run into floating point rounding errors after a few days continuous play, but I can't see any reason why the pause would vary with distance. Have you changed the code to use object pooling? If you only have a limited number of objects in a pool, you wouldn't be able to spawn a new projectile until the last one had been destroyed (by for instance, hitting the target).
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I dont know how to use object pooling. If I enabled it, it was coincidental. I was changing the code so it would work with sound effects. I did not change that bit of code to make it fire though.
     
  3. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    Hi Tim,
    Been a while since I last contacted you. I had been working on other projects as I couldn't get the error to be resolved. I found I had a similar error with A* pathfinding project where any game object that used more than 1 thread for multithreading was crashing the game in build mode when it was destroyed (editor mode was flawless). I don't think the AutoAim code had a multithreading issue however it was an error that only occurred on destruction of a game object that contained AutoAim code during the built game.

    Since the update to Unity 4.5.0f6 your brilliant software works flawlessly and if I was rich I'd throw wads of cash at you.
    I can only assume it was some unity bug that was causing the crash as I had changed no code at all and yet since the update it no longer crashes.

    Don't know if reporting about this helps you in anyway however I'm gonna party like it's 1999 because that was a year or so of stress for a problem that was not related to my code OR your code. It was a unity bug.

    I'm just glad we didn't end up having you stress over my code because unless you are a developer
    of the Unity editor it wasn't something you could have fixed.
     
    Last edited: Jun 30, 2014
  4. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hey, I'm glad to hear that! Good luck with your game.
     
  5. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    UPDATE: Turning off "Limit the Turn Speed" fixes it HOWEVER I want turn speed limited so I'll look into this. I assume any speed of rotation results in the bug but I did try down to speed 4 and got the bug and even if 1 worked it would be impractical for my needs. Fast speeds like 90 quickly result in the bug.

    Hi Tim,
    I might have found an unintended result in your code. If the target leads the turret so that the barrel's vertical angle is equal to or greater than 88 degrees then the turret locks into place at 90 degrees and won't budge. 87 degrees or less and the error does not occur but if you go to 88 degrees or more then the barrel locks and will no longer move. It will shoot if you go within cone angle but the barrel is locked for good in that angle - even if you use "return to center"

    Repeatable on any Auto-aim turret..OH I should include some info:

    Aimer.cs
    Target Type = Circular
    Projectile Type = Linear
    Max iterations = 10
    Max Inaccuracy = 0.1

    PrefabShooter.cs
    CheckForObstacles = true

    Limiter.cs
    max Horizontal = -180, 180
    mirror horizontal = true
    min vertical angle = 0
    max vertical angle = 90 (error occurs at >= 88)
    limit the range = true
    min range = 0
    max range = 40
    Limit the turn speed = true (turn this off to disable the bug but lose a great function)
    Maximum turn speed = 90 (even super slow speed 4 doesn't escape the bug).
    Return to Centre = true (error occurs with false too)

    Targeter.cs
    switch to closest = true
    switch if invalid = true



    PS: if you know which part of the code to tweak, let me know ASAP thx.
     
    Last edited: Jul 6, 2014
  6. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Thanks Shinugami, I'll investigate.

    Cheers,
    Tim
     
    Shinugami likes this.
  7. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    Thanks Tim. My game that I'm working on is coming along awesome. Because I have Aspergers I'm a one man band so far as game creating skills. I'll put you in the special thanks credits for always being good with support.

    Let me know when you work out what is causing the problem otherwise I might have to ditch using AutoAim. I can't imagine a MOBA where everyone's turrets are locked and don't aim anymore.
     
    Last edited: Jul 18, 2014
  8. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Sorry it's taken a little while to get back!
    I've just had a look at this - I've found the problem - it only happens when you have speed limiting turned on with a two-part turret. I have to have a little think about how I'm going to solve it.
     
    Shinugami likes this.
  9. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Okay, I have a fix. I'll release a new version to the asset store and my website once I get home.
     
    Shinugami likes this.
  10. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    I've submitted the 1.05 update to the asset store - it'll show up once the Unity team have approved it. 1.05 is also now up on my website.
     
    Shinugami likes this.
  11. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    Nice ! Well done. I'll copy the fixed part over because I've heavily altered the original code to have more features.

    I compared the old code and new code and found the function that you updated. I copied that updated function into my heavily modified code and it works perfectly. You legend !
     
    Last edited: Jul 29, 2014
  12. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    I have just submitted version 1.06 to the asset store. It should be up once it passes submission. It is already available at my website.

    Version 1.06:
    -------------
    * Fix for bug where first shot could be off when using linear / linear aiming.
    * Added ability to filter layers tested against when checking for obstacles.
    * Fixed warnings that resulted from methods that were deprecated in Unity version 4.0.

    I know I'm a bit late with getting the deprecated functions out - it's just that I don't have any active projects that use the code any more and I've only just been notified by a user that they were getting warnings!
     
    John-G likes this.
  13. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    If I put AutoAim on a moving vehicle, can it properly aim-ahead when firing at other moving vehicles? Or is the math too complicated haha. Thank you
     
  14. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hi eridani,
    I assume you mean if the projectile inherits the speed of the firing vehicle? The system currently doesn't take that into account. The maths wouldn't actually be too difficult; you'd just need to take away the velocity of the firer from the predicted velocity of the target.
     
  15. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Hmm you mean subtract the vector of the firer from the predicted vector of the target? Could you provide a code sample that would accomplish that? I'm horrible at 3D math!
     
  16. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Also, is there a setting for turrets to stop shooting at occluded targets (targets it can no longer see, such as a target behind a wall). Thank you
     
  17. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    There is an option for that in the prefab shooter called 'check for obstacles'.

    It is demonstrated in the 'ObstacleTracker' sample scene.
     
  18. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hi folks, due the new EU VAT MOSS rules that came into effect today I've had to disable direct selling of AutoAim via my website. You can still buy it on the asset store, though.

    Those who have already bought AutoAim through my website will be unaffected - they will still be able to download from my website and any further updates to AutoAim will still continue to be pushed to both the Asset Store and the website.
     
  19. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Is there a setting to make the double gun barrels always remain parallel to the ground? Right now the gun body rotates around its long axis (the axis pointing at the target) while it tracks the target. Thanks!

    autoaim.jpg
     
  20. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Did you make a two or three part turret?
     
  21. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Umm it is just the example scene that comes with the asset. All the scenes exhibit this behavior except for the turret with the limiter on it. I'm running 4.6.1p2 so maybe it's a recent Unity issue?
     
  22. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Yeah, I've replicated the issue. I'll try and fix it when I get home from work this evening.

    In the mean time, you can work around the problem by adding a limiter component to the turret. If you set the limit angles to maximum it won't have any effect on the aiming, but it will prevent the gun barrel from getting rotated.
     
  23. eridani

    eridani

    Joined:
    Aug 30, 2012
    Posts:
    655
    Ok thanks a lot! I need a limiter for the turrets in my current scene anyway so I'll go with that for now.
     
  24. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Version 1.07 submitted!
    • Fix for the twisting barrel bug.
    • Unity 5.0 support.
    Version 1.07 should be up on the asset store soon (once it gets approved), and it's already on thegamemechanics.co.uk for those who bought it there.
     
    VIC20 likes this.
  25. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Hi Tim, would it be possible to add support for air resistance and even wind influence to projectiles.
     
  26. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hi John,
    Is it just that you would like the default projectile type to react to wind / air resistance, or do you want the aimer to take that in to account as well?

    Cheers, Tim
     
  27. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
  28. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Wow - that paper might have been quite useful to me when I originally wrote AutoAim! I'll have a look when I get the opportunity, but it may not be for a few days as I'm very busy at the moment.
     
    John-G likes this.
  29. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Hello again! I am currently building an air combat game and I'm running into performance issues when using massive amounts of turrets, I'm seeing if you might be able to offer some help.

    Each ship in the game can have up to 20-50 turrets. There can be 20-30 airships in the battle at once which can add up to several hundred turrets in the scene. The limiter script is taking alot of CPU as well as the aimer and prefab shooter (mostly due to instantiation of the bullet). I was able to improve the performance by only running the update function every 0.1 seconds on the limiter, aimer, and prefab shooter scripts. I made the targeter script only run 4.5 - 5 seconds however its still producing big lag spikes.
     
  30. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hi Roger, I only really ever meant the prefab shooter to be an example script for launching projectiles; ideally it would use a pool system so that it didn't have to do an instantiation every time something fires a shot. It should be pretty straightforward to mod it to use a pooling system (there are several on the asset store and on the wiki) - have a look around line 125 on PrefabShooter.cs.

    Also, make sure that the prefab shooter doesn't have Check For Obstacles checked, as that does a raycast every time you fire.

    To be honest though these are all guesses - the only real way to see what's using up the CPU is to profile it.

    I am curious about what aiming and tracking modes you're using, though.

    Cheers,

    Tim
     
  31. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I've implemented a pooling system although this actually makes the performance worse. The garbage collection goes through the roof when using hundreds of turrets. I think its because targeter.cs is using gameObject.FindGameObjectsWithTag, and it has to check every pre spawned bullet in the scene.

    I am using the linear aiming method.
     
  32. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Why are you tagging bullets as targets? I would have thought you would only be tagging enemy airships?
     
  33. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I thought gameObject.FindGameObjectsWithTag checks every object in the scene to see if it has the correct tag, so it still needs to check everything even if it does not have the tag causing the overhead. That was my theory anyway.
     
  34. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    The docs don't mention anything about performance, whereas the docs for things like GameObject.Find do, so I assume that FindGameObjectsWithTag has a dictionary of tags that map to lists of object GUIDs.

    Anyway, back to your game - have you profiled it to see which methods the time is being spent on?
     
  35. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Finally got around to using this again. Noticed a few small errors.
    1. In the Turret Wizard - The window is sized that you can't see the next or back buttons till you re-size the window.
    2. Also in Turret Wizard - If you select the Swivel type, it has Select Gun Barrel text instead of Select Turret text.
    3. The Gizmos take on the scale of the Parent so if your turret is using a large scale the Gizmos can be huge.
    4. Limiter Script - Return to Center does not take into account rotation of parent, so if parent rotates the gun offsets its center by the same amount.
    By the way did you ever get a chance to implement Air Drag into the system?
     
    Last edited: Apr 21, 2016
  36. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Hello. Thanks again for such a great turret system!

    Could you explain to me how exit points work? I have a weapon that has duel guns and I am just trying to add two exit points for the projectiles. However exit points can only be animations, not transforms.

    How do I mearly add two exit points for the bullets without using animations?

    Also, I am wondering how you can play a muzzle/particle effect when shooting. I see a script called PlayParticleSystem.cs however I don't know how it manages to work because I cant find any references to the script in PrefabShooter.

    Also, I am wondering if its possible for you to grant me AutoAim in the asset store. I bought the package awhile ago off of your main site. However I can no longer download it and I have to redownload the package because I need a fresh install of the autoAim package. The one I have has all of my custom code put into it and I did not make a backup of the basic package.
     
  37. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hi Roger,
    Unfortunately there is no way for asset creators to gift people things in the asset store, otherwise I'd be happy to do that for you. However, I'm concerned that you're unable to get your downloads from my website - I'll have a look in to it.

    Cheers,

    Tim
     
  38. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    The login part of my site had gotten broken - it should work now. DM me if you have any trouble.

    Cheers,

    Tim
     
  39. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Unity have given Dev's the means to do that with so many license keys that can be given out per year.
    You should have the means to do that now.
    I've been given license from devs for assets that I've purchased previously from their sites.
     
  40. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Man! I'm behind the times. Thanks John.

    However, looking at the terms and conditions for asset vouchers, I'm not allowed to take any money for them - and if I gave them to people who had bought it from my site I would be breaking that agreement.
     
  41. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    No worries Tim, any chance you fixed the issue I posted above especially the return to centre on limiter script.
    Currently turrets only work for fixed turrets, if placed on a patent that rotates (eg tank Turret) if tank rotates the turret centre is offset by parent.

    I have edited my script to account for this, but would be nice if it worked out of the box for anyone else doing similar turret systems.
     
  42. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Hello again from 2018. Your website is down so I cant look at the documentation. Also, the support email that's registered to the asset store account is returning blank. Have you remove it?

    You might want to get yourself up to speed again and update a few things. It says on the product page that it has not been updated for over 3 years. I'm surprised it has not yet been deprecated and removed yet. At least update your email and include the documentation with the product. It would be better than relying on a private website year after year just so people can access the documentation.

    Thanks again
     
    VIC20 likes this.