- Home /
[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;
Have you tried using Debug.Log
to print out values of vectors and other variables? That might help you narrow the problem down.
I have printed all of the values during testing, and it shows that the vector was normalized properly, but I can also see the values s$$anonymous$$dily scaling as I move the mouse. A normalized vector should always have a magnitude of 1, so it doesn't make sense that a directional vector is still carrying a scaled weight.
HOWEVER.
I just Zeroed out the z values and that seems to have fixed it. I'm not sure why, seeing as the game it two dimensional. I guess the mouse pointer is considered on a far enough z position in relation to the others that it messed everything up. Go ahead and post an answer so I can mark it solved Nose$$anonymous$$ills. I knew it was something stupid.
Answer by NoseKills · Jul 10, 2015 at 05:46 PM
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.
Well that explains that then. Clearly I should sleep more before I try to program. Thanks for the help, I knew it would be some stupid detail.