0
\$\begingroup\$

Here I have a 2D top down game. Currently the camera is set on the player position. The player constantly points to mouse position.

I want to modify this setup having the camera to be set slightly forward in the player.transform.up direction but when I try it as the mouse gets closer to the character it starts looping because as the mouse approache, the camera turns but also change the mouse position so the camera needs to turn again, and so on.

this is the camera update

transform.position = new Vector3 (player.transform.position.x, player.transform.position.y, -10) + player.transform.up * camDistance;

and this is how the mouse position gets updated

mousePos = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, -5)) - transform.position;

any clue?

\$\endgroup\$
4
  • \$\begingroup\$ the camera turns but also change the mouse position how does the camera turn, affect your mouse location ? i didn't get what is happening exactly \$\endgroup\$
    – dimitris93
    Commented Jan 21, 2015 at 10:02
  • \$\begingroup\$ the camera turns because it follows player transform and it includes player rotation which is always done according to mouse position \$\endgroup\$
    – Leggy7
    Commented Jan 21, 2015 at 10:05
  • \$\begingroup\$ where does your mouse has to be for the player to stand still, and where does it have to be for the character to move ? (in theory) \$\endgroup\$
    – dimitris93
    Commented Jan 21, 2015 at 10:17
  • \$\begingroup\$ @Leggy7 You don't need the absolute mouse position, you need the offset, which means; when mouse moves you save the old position and the new position, take the different and put this displacement in a rotation matrix. \$\endgroup\$
    – concept3d
    Commented Jan 21, 2015 at 12:18

1 Answer 1

0
\$\begingroup\$

It's going to be a tiny bit of work. To fix this you will need to manage your own cursor in world-space using relative movements of the mouse to move it rather than a screen space cursor.

Using Screen.lockCursor (http://docs.unity3d.com/ScriptReference/Screen-lockCursor.html)

And Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") (http://docs.unity3d.com/ScriptReference/Input.GetAxis.html) to get relative mouse movements.

You keep a world-coordinate mouse position and add the mouse delta (relative) movements to it. This way when the camera moves the cursor doesn't move relative to the player/world.

The cursor will be in the world rather than on the screen.

\$\endgroup\$
2
  • \$\begingroup\$ this sound's like the solution I'm looking for. Could you please help me understanding Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")? they return floats and I'm not sure what the range returned represent and how to treat it to get absolute mouse position \$\endgroup\$
    – Leggy7
    Commented Jan 22, 2015 at 14:14
  • \$\begingroup\$ You keep a 2d vector and add those values to it. That is the "cursor" coordinates in world space, not screen space. Map those to (X, Z) if you're looking down. \$\endgroup\$ Commented Jan 22, 2015 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .