Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Moving camera with the mouse

Discussion in 'Scripting' started by Wadoren, Oct 27, 2016.

  1. Wadoren

    Wadoren

    Joined:
    Mar 13, 2016
    Posts:
    9
    Hi!
    I am trying to build a camera controller for my puzzle plataformer game.

    The way it should work is like this:

    1. The camera is directed by the cursor, which is always in the center of it.

    2. The camera has to always show the character, so if the character touches one of the screen borders, the camera cannot move anymore. And if the character starts running towards that border, he will push the camera with it.

    However, just when I was starting, I found something weird.

    My idea to do the first part was to get the world point of the mouse and put it as the x and y of the camera transform.

    The problem is that when i try to print this:

    Code (CSharp):
    1.  
    2.     public float distance = 1.0f;
    3.  
    4.       void Update ()
    5.     {
    6.         Vector3 mousePosition = Input.mousePosition;
    7.         mousePosition.z = distance;
    8.         print(Camera.main.ScreenToWorldPoint(mousePosition));
    9.     }
    I get numbers that are very close to the transform of the camera, but when, for example i go up with the mouse some decimals are added and when i go down some decimals are subtracted, but it doesn't really show the position that the mouse is pointing at.

    Does anybody know why does this happen?
     
  2. Wadoren

    Wadoren

    Joined:
    Mar 13, 2016
    Posts:
    9
    Here are some imatges trying to describe what i mean.
    1-
    Camera1...png Camera2...png
    2-
    Camera3...png Camera4....png
     
unityunity