Limiting how close something can be to an object

How would I go about adding a limit on how close my crosshairs can get to the target and vise versa? I don’t want my crosshairs to be able to overlap my player.

I’m not able to copy and paste the code so a picture will have to do I apologize. My PC has no internet access currently.

Just pointing me in the right direction is also fine.

1 Answer

1

Note: this is without any lerping.
You can check the distance, then force the crosshair to be equal to the distance amount if it is too close, whilst using a direction vector.
(targetPos would be your player position)

        var targetPos = target.transform.position;
	    var crossHairPos = crosshair.transform.position;
		var position = crossHairPos;

		var dist = Vector3.Distance (crossHairPos, targetPos);
		var closenessThreshold = 10.0f;	

		if (dist < closenessThreshold) {
			var dir = (crossHairPos - targetPos).normalized;
			var clampedPos = targetPos + (dir * closenessThreshold);
			position = clampedPos;
		}		
		crosshair.transform.position = position;