Issue with close proximity mouse tracking via normalize

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:

1497839--84117--$unity issue.png

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.

1497839--84118--$unity working.png

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.

Solution:

Don’t use hand as a reference point for normalize with mouse location. Instead, use player/body loc. This works since the player’s body is what the mouse pointer is moving ever-closer to. If I were to use the hand as a reference, I would get the hand jitters as I did as the program struggled to find the vector between the hand and the mouse, move the hand, and then try to find it again next update. Since the player/body loc is my zero point, or my point of reference, it makes so much more sense to use it as the reference point which the hand aligns between it and the mouse. I can then multiply my normalized vector by whatever distance I wish to specify the maximum reach of the body.

This solution still presents an issue when the mouse cursor gets closer then 2 units of distance to the body. However, you’ll see in my initial else condition (and the one I’ll show you) I allow direct location mimicking once the cursor is closer to the body than 2 units. So the hand is just set to the mouse location verbatim once the condition is met (or isn’t met, I guess).

Working code (cleaned up unnecessary longhand from earlier post):

        if(Vector2.Distance(mousePos, player.transform.position) > 2)
            hand.position = player.transform.position + (mousePos - player.transform.position).normalized * 2;
        else
            hand.position = mousePos;

For giggles, here’s the orientation code that makes the hand lookat() the mouse cursor at all times, too.

Vector3 mousePos = camera.ScreenToWorldPoint(Input.mousePosition);
        mousePos.z = 0;
        this.transform.LookAt(mousePos, new Vector3(0, 0, 1));

Hopefully this will help others struggling to set an objects orientation between the mouse and another object.