• Advertisement
Sign in to follow this  

Unity Targetting Cues

This topic is 1881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So my game's a third person space shooter. This causes a wierd problem with targeting as the player ship may not be pointing at the center of the screen at all times, which means crosshairs function kind of strangely and basically make it difficult to determine where exactly you're shooting (see attached screenshot for an earlier incarnation of this game that used a crosshair).

So instead I came up with a new system where there is no crosshair (see second screenshot), but each enemy ship has a targeting pattern that changes colors from red to green if they're in your sights. This targeting is fairly sophisticated in that it's not necessarily green if you're pointing directly at the enemy, but actually factors in the amount that you need to lead your shots to hit the enemy if they maintain their current velocity. However, when I set a few friends in front of this version and they seemed confused as to what the coloring meant. Once I explained it they seemed to get a little better at managing it.

While I could easily explain it away in some kind of tutorial part at the beginning of the game I wanted to get an opinion from the community about third person targetting and whether it's even desirable or if I should just do away with all targeting aids and err to the side of realism.

Since some weapons are fairly inaccurate or have tracking features, this complicates the situation even further.

Share this post

Link to post
Share on other sites
It seems like you've pretty much solved the math for this and you're more or less curious about the design. What I've used in the past is 2 cross hairs along with auto aim. The first cross hair represents where the player is aiming, the second represents where an enemy closest to that crosshair is, and when the player moves the cross hair close enough the first one jumps onto the second to show a lock on, and all rounds fired go towards where the player's cross hair is. This started out as a solution for the z point of where the player was aiming in our 3rd person shooter and eventually became an aim assist that the player could use. It took some of the challenge of shooting out of the game, but it meant that the challenge of the game was getting into cover, and then selectively taking out the targets by the danger priority each target presented. Quite impressive that you've solved the mathematics of this given you have player velocity and direction and enemy velocity and direction to account for. For accuracy we scaled the cross hairs to represent the accuracy of the selected weapon and for some weapons that accuracy decreased as they fired so thee player would end up firing short bursts to keep the shots focused in the reticule. We also used different textures for the cross hairs of different weapons. Another thing to keep in mind with a third person shooter is, that you need to solve that z-axis based on what target you think the player is aiming at for the shots from the player's avatar to hit the target instead of go around it. Which is important when the player needs to fire deflection shots (aiming ahead of the enemy) to score a hit. It's not an issue in first person shooters however. We had a bit in the game where the player fired a turret in first person against fast moving flying enemies, it didn't have a lock on, but it was a bit of a pain to get the deflection shots working intuitively since we needed to use the projected Z position of the target the player was aiming at to solve the z in the equation of what direction the bullet should go.

Share this post

Link to post
Share on other sites
About 10% of men are red-green colorblind to some degree, so those colors are pretty much the worst possible ones to indicate correct aim.
Change of color in general is slower and harder to see than a change in sight geometry. I'd suggest you make the sight change shape, add a box around it or something as the indicator.

Share this post

Link to post
Share on other sites
Stroppy - good call... Yellow/red or blue/red seem to be good choices from that standpoint.

Interesting post Kyall. Please post a screenshot if you have one, kind of curious how you handle the auto-adjustment of the Z-axis. When targeting enemies at different distances would the targeting "pop" around adjusting for different projection lengths?

The mathematics were a little fun. It's just a pretty simple system of vector equations to derive the formula you need, but solving it requires a few tricks. I already use the same formula so that the enemies can lead their shots too (otherwise they're hopelessly inaccurate).

Basically you start with:
finalPosition = initialPosition + velocity * time

What you want is to solve for any point at which a fired bullet will cross into the enemy's radius.

||(bulletInitial + bulletVelocity * time) - (enemyInitial + enemyVelocity * time)|| = enemyRadius

I'll let you figure out the rest of the proof for yourself, but basically you break each vector into it's component parts and solve for time with the quadratic formula. There should either be two real solutions or two complex solutions for time when you solve it. If there are two complex solutions it means the bullet will miss, if the solutions are real the bullet will hit.

The equation of course becomes trickier when you need to account for non-linear bullets like rockets (these have an accelleration component in my game).

Decided to post the code of the "IsTargeted" function so those who don't have the mathematics skills can use it, enjoy!

private static bool IsTargeted(Weaponry.Weapon weapon, PhysicsObject target, Player p)
var shootPosition = weapon.GetShootPosition();
var shootSpeed = weapon.GetShootVelocity().Length();

// Quadratic formula to calculate time for leading the shot
Vector3 shootVelocity = shootSpeed * p.Look;
var speedSquared = Square(shootSpeed);
var velocityDelta = shootVelocity - target.Velocity;
var positionDelta = shootPosition - target.Position;
var a = Square(velocityDelta.X) + Square(velocityDelta.Y) + Square(velocityDelta.Z);
var b = 2f * (shootPosition.X * shootVelocity.X - shootPosition.X * target.Velocity.X - target.Position.X * shootVelocity.X + target.Position.X * target.Velocity.X
+ shootPosition.Y * shootVelocity.Y - shootPosition.Y * target.Velocity.Y - target.Position.Y * shootVelocity.Y + target.Position.Y * target.Velocity.Y
+ shootPosition.Z * shootVelocity.Z - shootPosition.Z * target.Velocity.Z - target.Position.Z * shootVelocity.Z + target.Position.Z * target.Velocity.Z);
var c = Square(positionDelta.X) + Square(positionDelta.Y) + Square(positionDelta.Z) - Square(target.Radius) - Square(weapon.BulletRadius);

// Evaluate the quadratic formula, with a little modification
var inner = Square(b) - 4 * a * c;
var latterPart = (float)Math.Sqrt(inner);
return inner >= 0 && ((-b + latterPart) / a > 0 || (-b - latterPart) / a > 0);
[/CODE] Edited by jsuffolk

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By Atwo Studios
      Hey guys,

      Anthony here from Atwo Studios bringing you some new updates for the new year!
      In this video I go over our game ROY, the new games and some general updates to the company!

      If you have not checked out ROY feel free to give it a try! Many people have said they enjoyed the game thus far!
      ROY: https://goo.gl/o6JJ5P
    • By Affgoo
      still a lot of work to do, but its pretty stable  please let me know what you think <3
      Atlas Sentry is a game of destroy everything. Using your turret, simply swivel and shoot your way to victory, upgrading your weapons to unleash destruction on the variety of spaceships. The bigger your combo’s the more score you get! Earn silver as you play and then purchase new weapons and abilities to better deal with your enemy. Different enemies use different tactics and weapons, work out your own priorities in their destruction order. 

      **2 different game modes 
      **A level select mode with 20 difficult levels including a final boss, can you defeat it? **Arcade mode of endless destruction, how long will you last? 
      **High scores to compete against others, see who can take the top spot. 
    • By Chamferbox
      Chamferbox, a mini game asset store has just opened with some nice game assets, 
      Here you can find a free greek statue asset 

      Also check their dragon, zombie dragon and scorpion monster out:

      They're running the Grand Opening Sale, it's 30% off for all items, but for gamedev member, you can use this coupon code:
      to get 50% off prices What are you waiting for, go to
      and get those models now!

      View full story
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!

      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064

      View full story
    • By DevdogUnity

      Ho ho ho
      Tis the season of Christmas surprises, and we have a awesome one for you! 🎅  
      Sponsored by all your favorite Unity Asset Store developers, Nordic Game Jam, Pocket Gamer Connects, and co-hosted by Game Analytics, we (Joris and I – Devdog) are launching the second edition of our yearly Christmas Giveaway Calendar for all Unity game developers!
      You can already now sign up right here.
      So what’s this all about?
      For the past weeks, we’ve been collecting sponsored gifts related to Unity (asset vouchers, product keys, conference tickets etc.), and throughout each day of December leading up to Christmas Day on the 25th, we will be sending out these sponsored gifts as early gamedev Christmas presents via e-mail to hundreds of lucky winners.
      The total prize pool is at $35,000, with over 1200 presents donated by the awesome sponsors!
      Merry Christmas from Devdog, Game Analytics, and every single one of the sponsors.

      View full story
  • Advertisement