- Home /
First-Person Shooter Aim Assist by "Snapping" the Crosshair On Object
I have an issue figuring out how to implement a certain method of assisting aim into my game. The goal is to "snap" the crosshair to an enemy if the enemy is close enough to the cursor. A close enough example of what I mean is with GTA's assisted/auto aim, where if you even somewhat-aim your firearm at someone, the cursor attracts itself exactly to that person. The method that got me the closest to what I wanted was spherecasting, but I am open to other methods and explanations as well. Below is my current method from a firearm script:
Vector3 lookingAt;
if (Physics.SphereCast(ray, 0.25f, out RaycastHit hit, accuracyRange, ~LayerMask.GetMask("Boundary"),
QueryTriggerInteraction.Ignore) && hit.collider.GetComponentInParent<PlayerMovement>() == null)
//ray is the cursor's screen position raycasted forward into 3d space
{
Vector3 center = ray.origin + (ray.direction * hit.distance); //acts as cursor but translated hit.distance units forward
Enemy enemy = (Enemy)hit.collider.GetComponentInParent(typeof(Enemy));
if (enemy != null)
{
player.camera.transform.rotation = Quaternion.Lerp(
player.camera.transform.rotation,
Quaternion.LookRotation(enemy.transform.position, center),
1f * Time.deltaTime
);
}
lookingAt = hit.point;
}
This method has a few issues: 1) You can't really feel like you're snapping to anything because you have to aim almost exactly at a target for it to even have a chance at working. 2) When I increase the radius to resolve issue 1 so that a target can be hit more easily, I end up with the exact opposite result, meaning that hits seem to be detected less by the SphereCast. 3) The bigger the radius, the more frequent it is for shots to go in completely different and random directions as if they were deflected.
Your answer
Follow this Question
Related Questions
How do I make a raycast for a particle 1 Answer
How to change 3rd shooter directions? 0 Answers
animations or code? 1 Answer
Unity Iphone Build keeps crashin 1 Answer