Search Unity

How to get character to accurately follow mouse?

Discussion in '2D' started by ShichiNoBushi, Apr 19, 2016.

  1. ShichiNoBushi

    ShichiNoBushi

    Joined:
    Apr 19, 2016
    Posts:
    5
    I'm trying to make a 2D game, and an important mechanic of it is to have a character follow the mouse's position on screen. I have found a method to do it, but I'm having difficulty having it work accurately and have mostly accomplished it by applying variables to the calculation.

    I am first giving the character a reference to the camera of the scene as myCamera as well as some serialized references I've called cameraFactorX, cameraOffsetX, cameraFactorY, and cameraOffsetY so I may easily alter them in Unity. Then I added the following lines into the Update method in the character's script:

    Vector2 mouseNow = Input.mousePosition;

    transform.position = new Vector3 ((mouseNow.x+myCamera.transform.position.x)*cameraFactorX+cameraOffsetX,(mouseNow.y+myCamera.transform.position.y)*cameraFactorY+cameraOffsetY,transform.position.z);

    The camera's position is currently at (0, 0) and set at Size = 3. It has taken me setting the values of cameraFactorX and CameraFactorY to 0.0112, cameraOffsetX to -6.8, and cameraOffsetY to -3.03 before the character seemed close enough to the mouse cursor. But I have noticed that this only works as long as the camera stays at position (0, 0) and doesn't move. Changing the camera's position also has negligible effect on where the character is in the game's space but is then displaced from the mouse.

    Is there a more reliable and easier process to use to get the results I need so a character will follow the mouse regardless of where the camera is looking?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,695
    Code (CSharp):
    1. private void Update()
    2. {
    3.     // convert a point on screen (pixel units) into a world space position (unity units)
    4.     Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    5.  
    6.     // preserve original Z value
    7.     newPosition.z = transform.position.z;
    8.  
    9.     // apply position
    10.     transform.position = newPosition;
    11. }
     
  3. ShichiNoBushi

    ShichiNoBushi

    Joined:
    Apr 19, 2016
    Posts:
    5
    Thanks. I had actually just discovered "Camera.main.ScreenToWorldPoint" just before I checked up on this thread. I had tested it, and it works perfectly. I just came here to check up on it and mention it myself. The code I used for it was this:

    Vector3 cameraPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

    transform.position = new Vector3(cameraPos.x, cameraPos.y, transform.position.z);
     
    LiterallyJeff likes this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,695
    It is a completely negligible difference so don't feel the need to change your code, but just FYI, if I'm not mistaken my solution is slightly more optimal because you don't have to create another new Vector3 every frame.

    But again don't feel the need to change your code, I think yours is perfectly readable and elegant.
     
unityunity