Search Unity

how to use mouse y input

Discussion in 'Scripting' started by Sylon87, Aug 20, 2018.

  1. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    hello!
    sorry for my bad english, i'll try to do my best...
    actually i have a game where my player are going to move with the mouse y axis,
    but what i'm trying to do is to cover a specified value, let me explain it.

    /\ 0
    |
    |
    \/ -1

    where when y axis are on 0 player should stay on the 2.0f of z axis, and when are on -1 he should stay on - 3.7f of the z axis, and i want a smoth movement when it moves from 0 to 1, i really can't figure out how to do it. someone can help me?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your English is fine, no need to apologize.

    Unity's normalized screen coordinates are 0,0 in the bottom left and 1,1 in the top right. We can use Screen.height to map the current map position to a normalized ratio, and then flip it since you feel more comfortable with 0 being the top:
    Code (csharp):
    1.  
    2. float ratio = Mathf.Clamp01(1.0f - (Input.mousePosition.y / (float)Screen.height));
    3.  
    We can feed that ratio to lerp with your two values to get a target Z position:
    Code (csharp):
    1.  
    2. float targetZ = Mathf.Lerp(2.0f, -3.7f, ratio); // if ratio = 0, we get 2.0, if ratio is 1.0, we get -3.7f, and if it's somewhere in-between then we get a proportional value between the two
    3.  
    Finally, we can use MoveTowards to move the current Z position to the target Z position over time:
    Code (csharp):
    1.  
    2. Vector3 currentPosition = transform.position;
    3. currentPosition.z = Mathf.MoveTowards(currentPosition.z, targetZ, 2.0f * Time.deltaTime); // change 2.0f to adjust movement speed
    4.  
    Then just assign it the value, and you're done:
    Code (csharp):
    1.  
    2. transform.position = currentPosition;    
    3.  
    Now if your mouse is near the top of the screen, the object will move towards 2.0 and if it's near the bottom of the screen, it'll move towards -3.7.
     
    Doug_B likes this.
  3. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    Thank’s you for your answer!
    I can’t figure out of what is happening on the first line, i understand everything else except for the first one..
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Unity's screen coordinates are 0,0 in the bottom left and Screen.width, Screen.height in the top right. Input.mousePosition is the same, 0,0 is in the bottom left and maximum size in the top right corner.

    So first we convert that to a ratio 0...1 ratio with division:
    Code (csharp):
    1.  
    2. float mouseToScreenRatio = (Input.mousePosition.y / (float)Screen.height)
    3.  
    Then we re-map it so that 0 is the top and 1 is the bottom by subtracting the result from 1.0:
    Code (csharp):
    1.  
    2. float remappedRatio = 1.0f - mouseToScreenRatio;
    3.  
    (We did that just because you were more confident with 0 in the top and 1 in the bottom, which is more traditional in "classic" 2D screen coordinates).

    Then we clamp it between 0...1 because in the editor, if the mouse is outside of the game screen, you can actually get values that are below 0 or above 1 because the game's screen height isn't the same as Windows' screen height usually.

    Code (csharp):
    1.  
    2. float ratio = Mathf.Clamp01(remappedRatio);
    3.  
    Hope that helps clear it up.
     
    Sylon87 likes this.
  5. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    Great, i got it! Thank’s you!!

    I’ll try to apply this, i’ll let you know the results!

    Actually there is a way to store how much playes has walked with this method?
    With an if statement for example i’ll be able to store the changes on a kind of float, but with this..