I am making an ability where the user has the aim their mouse at a player to use it, but I would like it to not have to be exactly on them. Making it easier to aim.
I want to avoid creating extra hitboxes when a character gets added if possible. I was looking into mouse.Hit and seeing if it’s near a character but that can be an infinite distance away.
I would like this to be very efficient and not too taxing on the client or server.
All sorts of ways you could do this, but an example could be:
On each frame, collect the players that are relevant to the player, i.e. ensure that each of the targetable players are:
In front of the client’s camera and/or is within some desired FieldOfView; can be determined using screenspace coords as described below and/or checking the angle between the target and the client e.g.math.acos(cameraCf.LookVector:Dot((targetPos - cameraPos).Unit))
Is within some desired distance to the client’s camera, i.e. the magnitude between the target and the client is within either (a) the current weapon’s range, or (b) some other desired distance
Whose screenspace position is within some desired range to the centre of the camera, computed via WorldToScreenPoint
Sort the targetable players by one of the values computed above, e.g. the closest player ± least screenspace distance/angle; then iterate through each of the targets until you find an appropriate target, i.e. one that:
Is able to be hit, can be checked using a Raycastetc
Some other checks that you’d like to perform
Once the best target has been chosen you would interpolate the Camera’s CFrame towards the target based on the computed values, e.g. if using screenspace aim assist:
you could move the centre of the camera towards the screenspace coordinate
and could attenuate that movement by the distance and smoothed by x smoothing factor etc