EDIT: Solution below in next post
Hello all,
I’m writing a game similar to Madness Interactive (2003 Flash game). I am trying to accomplish a main character having a hand that will always be the normalized magnitude of the mouse pointer and player body. Meaning, the hand will always be between the player’s body and the mouse pointer, though will only ever be 2 units away from the body (normalize * 2). This results in the hand being able to aim a weapon wherever the mouse cursor is pointing.
I have already figured out how to get said orientation to work, though when I bring the mouse cursor closer than 2 units, the hand no longer lines up between the body and mouse pointer, because it’s still trying to keep a normalized magnitude of 2 units (again, normalize * 2). See attached image for an example of the sporadic hand placement when the pointer gets too close:
If you were to see the issue in progress, you would see that the distance between the hand and mouse dipped below 2, but as update continued to run the difference inched back up to 2 as it corrected itself.
So, the hand is not staying between the cursor and body because it’s still trying to keep a magnitude of 2 between the cursor and hand. Here’s my code:
if (Vector2.Distance(new Vector2(mousePos.x, mousePos.y), new Vector2(player.transform.position.x, player.transform.position.y)) > 2)
{
hand.position = player.transform.position + (mousePos - hand.position).normalized * 2;
}
else
{
hand.position = new Vector2(mousePos.x, mousePos.y);
}
Any thoughts on which process I can use so when the pointer gets too close, the hand is still between the mouse and body? I figured I would have to multiply the normalized value within the if by the difference of some value, but I can’t figure out what.
Thanks.
edit:
for further understanding, here’s a picture of when the tracking is working properly.
It will track with a distance more than 2 units between the hand and mouse just fine. Once it dips below that is where I get the sporadic placement as the program tries to keep a 2 unit magnitude.