Search Unity

Recording mouse position is acting weird

Discussion in 'Scripting' started by matias-e, Feb 5, 2015.

  1. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hi! I created a small script that is supposed to move a GameObject to the position of the mouse's X and Y value whenever the left mouse button is held down. However, when I hold my mouse down in the game view in Unity the X and Y values go crazy, shooting to hundreds.

    The GameObject's original values are the following: X: -3.89, Y: 3.18, Z: 0

    When I click on the screen the values always go somewhere between 100-700, which makes the game unplayable as it shoots the GameObject way out of bounds of the screen. The script is linked to the GameObject I want to move.

    Here's my simple script:

    Code (CSharp):
    1. void Update () {
    2.      
    3.         float mouseX = Input.mousePosition.x;
    4.         float mouseY = Input.mousePosition.y;
    5.  
    6.         if (Input.GetMouseButton(0)) {
    7.             transform.position = new Vector2(mouseX, mouseY);
    8.         }
    9.     }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,447
  3. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  4. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Thanks for the quick replies guys! I'm a total newbie so I'm not sure what I'm doing, but I ran into a problem with the script.

    Code (CSharp):
    1. void Update () {
    2.         Vector3 springPlace = camera.ScreenToWorldPoint
    3.             (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
    4.  
    5.         if(Input.GetMouseButton(0)) {
    6.                 transform.position = new Vector3(springPlace.x, springPlace.y, 0);
    7.             }
    8.         }
    9.     }
    I get an error report where Unity tells me that there is no camera attached to the GameObject I'm trying to control, but adding a camera to it wouldn't be a feasible option in terms of the game. Putting the script on the camera is not working very well.

    What should I do? Should I refer to the GameObject in the code after putting the script on the main camera?
     
  5. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  6. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    I was a bit too quick to post the first reply as I noticed some pretty big logical mistakes in the script after sending it out! Anyway, I have now attached the script to the camera and ordered the script to find the GameObject I want to move. Now the position of the GameObject updates once, not to the position of my mouse and then it stops there, not responding to any further mouse clicks/movements.

    Code (CSharp):
    1. void Update () {
    2.  
    3.         springer = GameObject.Find("springControl");
    4.  
    5.         Vector3 springPlace = camera.ScreenToWorldPoint
    6.             (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
    7.  
    8.         if(Input.GetMouseButton(0)) {
    9.                 springer.transform.position = new Vector3(springPlace.x, springPlace.y, 0);
    10.  
    11.             }
    12.  
    13.         }
     
  7. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    From the docs: "The z position is in world units from the camera."

    So, it returns the world point that is z distance from the camera.