[Solved] Constraining a position within a radius (2D crosshair)

Okay, so I’ve been going through forums and stuff, and trying many solutions to this problem yet they all fail and I can’t for the life of me see why right now. Hopefully I’m overlooking something simple because this code SHOULD work as intended and it isn’t.

I have two cameras in my scene, the second one controls the majority of my HUD and other UI elements, since it’s a bit cleaner that way. The game is 2D and I am aligning a crosshair between the player and mouse. I can set it to the mouse’s position no problem, and can have it be at a point between the two as well. The problem is I want it constrained within a radius around the player. This code SHOULD be doing that, but instead of clamping it within the radius and allowing it to move normally otherwise, it just produces a ‘scaled’ result. It is hard to explain. The farther my mouse is from the player, the further the cursor moves, but it is relative to the radius that I want to clamp to. I have tried many variations of this code, but to no avail. Hopefully someone can help me because I’m not sure what the problem is at this point, I’ve been working on it too long.

Update: I’ve been working on it some more but still no progress. It has nothing to do with using multiple cameras, since I used similar code with only the main camera to actually fire. The bullet velocity scales according to the distance of the mouse from the player as well. Attempting to get an angle between the direction vector calculated here and something like vector3.right or up always gives 180 as well.

				Vector3 mousePoint = camera.ScreenToWorldPoint(Input.mousePosition);
				Vector3 temp = Camera.main.WorldToScreenPoint(player.transform.position);
				Vector3 playerPoint = camera.ScreenToWorldPoint(temp);

				Vector3 target = mousePoint;
				Vector3 diff = mousePoint - playerPoint;
				float mouseDist = diff.magnitude;

				if (mouseDist > crossDist)
					target = playerPoint + (diff.normalized * crossDist);

				target.z = crosshair.position.z;
				crosshair.position = target;

Can’t really spot a mistake in the actual logic, unless it’s something strange with the first 3 lines.

Only thing that comes to mind is, you could try to zero the z-components in mousePoint/target, playerpoint and diff.

Edit: according to API docs the ScreenToWorldPoint and WorldToScreenPoint set the z-component to “distance in world units from the camera”. So the first 3 lines make your source vectors’ z values become non-zero, which in turn causes the normalizing of diff produce vectors that have x an y values that scale in relation to that z value.