Hello I’m making a simple 3D game where the player runs run and shoots enemies, however I want to build in an aim assist, so when an enemy if in a certain field of view, and range, it will target the closest enemy. So when the player fires, it will shoot at that enemy. I’m not exactly sure the best way to go about this. I currently know how to have one object track another object. If you put a trigger collider around the player, when and enemy comes into that collider, using a void OnTriggerStay(Collider other) function, shoot a raycast ray from the player to the enemy. Physics.Raycast (Player.position,enemy’s position, range, layermask)
within a certain view, but I do not know how to do this for multiply objects or how to figure out which is the closest and to compensate for objects that are being tracked, leaving the required range. I’m not even sure if Physics.Raycast is the way to go. Any help would be greatly appreciated! thanks
EDIT: stupid forum editor deleted all the stuff of my posting… I will make a double post to make sure the post will be displayed correctly…
what you really need is to know the closest distance between a point (enemy) and a ray (the current aim direction).
You have to calculate that for every enemy in a certain area and then find the closest enemy. Since you just have to compare which enemy is closer you can use squared distance instead of distance (it is much faster cause you don’t need the square root).
here is how you can get the squared distance:
public static float DistanceSqToLine(Ray ray, Vector3 point)
{
return Vector3.Cross(ray.direction, point - ray.origin).sqrMagnitude;
}
when you found the closest enemy you have to check if the squared distance of it is closer than the squared threshold.
Hope this helps ![]()
thanks for the help I will look into this.
Interesting.
Posting so I’ll get an alert if there is a reply I have been curious about this in the past myself.
Please check your latest update. Please care in your studies too. There are a number of writing work. If you want enjoy the games, please choose thesis writing service provider from online for better relaxed work.