Effective Aim Assist in 2D Shooters


Fast-action twin-stick shooters can be made much more enjoyable with aim assist. Aim assist is used to detect a target in a narrow search area, and then adjust the aim of the gun to point directly at the found target.  Admittedly this feature isn't for everybody, but for most controller players it's a welcome addition. In the past couple weeks we've examined a few different approaches to implementing this in Unity.

Searching for a target

Circle Cast 2D Approach

The Unity documentation states: A CircleCast is conceptually like dragging a circle through the Scene in a particular direction. Any collider making contact with the circle can be detected and reported.

At first glance this might seem like an ideal solution for finding targets. Though we've found this has some shortcomings.

First, even though the circle cast has a variable circle radius it will still only detect one collision (unless you use CircleCastAll() but then you have to manage distance sorting logic). Imagine an enemy right next to a wall. If the circle cast is wide enough, it might hit the wall before hitting the enemy and hence not provide the expected target.

Secondly, the circle cast has a constant radius throughout its cast. Which means you cannot make cone-shaped casts. Cone shapes are very useful for aim assist since they effectively make the search area wider at further distances and narrower at closer ranges. Conceptually this is better as it leads to a less jerky experience at close range, and better target finding at a distance where it is most needed.

Angled Multi-Raycast Approach (Cone)

A better solution is to use many raycasts set off from the player. This way we can evaluate each ray individually and set them off in a cone shape. The code that demonstrates how this is achieved can be found here.

This function takes the visibility thickness as a parameter. It's useful to tie this to a persistent setting. In Rogue Star Rescue we're including a slider in settings which adjusts this from 0 to 100%, which is effectively the strength of the aim assist. This has the added bonus of letting users disable it completely, many mouse/keyboard players prefer that.

This function is called every frame in FixedUpdate from our main Player Controller. You can see the number of raycasts can be adjusted, although we found 5 to be the optimal amount. More raycasts will have a better search precision, but will cost more in terms of performance. Since this is called every frame, it's better to keep it fast.

We also want to make sure aim assist isn't active when an enemy is too close to the player (this creates a jerky effect). We use the minDistanceAway to ignore any targets that are too close. Also when evaluating distance we use the square of the distance between objects. Without getting too technical, it's much more efficient to do it this way since it avoids the costly square root calculations of Pythagorean's theorem.

Using the multi-raycast approach produces smooth and effective aim assist. This is a big part of the 'feel' of a game so it's important to fine tune the parameters while still leaving an adjustable strength in the game settings.

Found a target

Get Rogue Star Rescue

Buy Now$14.99 USD or more

Leave a comment

Log in with itch.io to leave a comment.