1
\$\begingroup\$

Anyone remember how the aim assist works in GoldenEye on n64? Where the gun model would point at an enemy if he's within a certain area in your field of view. I'm trying to do something like that but have hit a wall. I'm pretty inexperienced in Unity 3D and using this project as a way to learn the ropes.

I've currently got it so that if you're in range of an enemy (facing him or not), your gun model will rotate towards that enemy. But I need to do it so that only happens if that enemy is in front of your facing direction and near the centre of the screen.

Any tips or pointers how to do this? Do I need do a raycast in direction of camera and do some math to figure out which enemy is nearest? If so what Math functions would I need to look into?

\$\endgroup\$
  • \$\begingroup\$ Aim assist is actually used by almost every first person shooter on consoles (due to the difficulty of aiming precisely with a gamepad) and is also available on most first person shooters on PC as an option. \$\endgroup\$ – Philipp Aug 21 '16 at 12:52
  • \$\begingroup\$ Doing many different implementations is possible and it depends on your camera code etc. \$\endgroup\$ – Ave Aug 21 '16 at 13:23
2
\$\begingroup\$
  1. Find out which enemies actually have a clear line of sight. You can do that with a linecast from player to each enemy. When the line-cast hits a collider which is not part of the enemy (level geometry or another game-object), that enemy is not eligible as a target this frame. Make sure that the layerMask of your linecast is set in a way that only those colliders block the linecast which are actually supposed to block auto-aiming. You certainly want to exclude the player-collider and any invisible trigger-areas.
  2. Calculate the difference between the direction of the enemy and the direction the player is facing.
    1. Use Quaternion targetDirection = Quaternion.LookRotation(target.transform.position - player.transform.position) to calculate the direction of all eligible targets.
    2. Calculate how far the aiming is off for each target: float aimAngleWrongness = Quaternion.Angle(player.transform.rotation, targetDirection)
  3. Pick the target with the lowest aimAngleWrongness. Optionally you can also take the distance to the enemy into account to prefer closer targets to those further away. The distance can be calculate with (enemy.transform.position - player.transform.position).magnitude.
  4. You can now implement the actual aiming behavior in many different ways. You can simply move the crosshair without moving the sight direction, you can move the sight direction immediately (very simple to do but might feel disorienting for the player) or you can do it smoothly with Quaternion.Slerp between targetDirection and player.transform.rotation.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.