Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Input.mousePosition always a constant

Discussion in 'Scripting' started by realdka13, Jun 19, 2018.

  1. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    Im trying to make a spinning selector arrow when the player is dragging the mouse around. But the mouse position that the script is getting is always the same and does not change.
    Its updating when dragging the mouse but the value doesnt change. Any ideas on how to fix this?

    My Code

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class MultiMeter : MonoBehaviour {
    8.  
    9.     private GameObject selectorArrow;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         selectorArrow = GameObject.Find("SelectorArrow");
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.     }
    20.  
    21.     public void FollowMouse()
    22.     {
    23.         Vector3 mouseposition = Input.mousePosition; //Find Mouse Position
    24.         mouseposition = Camera.main.ScreenToWorldPoint(mouseposition);//Convert from screen space to world space
    25.         print(mouseposition);
    26.  
    27.         Vector2 direction = new Vector2(mouseposition.x - selectorArrow.transform.position.x, (mouseposition.y -  selectorArrow.transform.position.y));
    28.  
    29.         selectorArrow.transform.up = direction;
    30.     }
    31. }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    I think you have to set the .Z value of the mouse position, as it only will have X and Y set by the Input system.

    That .Z value will tell the .ScreenToWorldPoint() function how far down the view frustum to put the projected world point. Otherwise it's probably either returning Vector3.zero or else the position of the camera.
     
    Doug_B likes this.
  3. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    I just got rid of the vector 3 and made it a vector 2 so the z axis wouldnt be taken into account, and same issue
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Actually you kind of NEED the Z axis. The Z axis is what communicates to the .ScreenToWorldPoint() function where in the world you are interested in. A single 2D mouse point maps to an infinite number of World points from right near you going off to infinity.

    Also, the Vector2 is just being up-coerced to a Vector3 with the Z == 0, so your problem remains. Set the Z to 10 (after taking it from Input.mousePosition) and see what happens to the output numbers.
     
    rakkarage likes this.
  5. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    Im getting different numbers base one where i click, but it wont change as im dragging still. Although i did just notice that the X is 0 for the value it gives me. Could that have something to do with it?

    Also here is the code change
    Code (CSharp):
    1.     public void FollowMouse()
    2.     {
    3.         Vector3 mouseposition = Input.mousePosition; //Find Mouse Position
    4.         mouseposition = Camera.main.ScreenToWorldPoint(mouseposition);//Convert from screen space to world space
    5.         print(mouseposition);
    6.  
    7.         Vector3 direction = new Vector3(mouseposition.x - selectorArrow.transform.position.x, (mouseposition.y -  selectorArrow.transform.position.y),10);
    8.  
    9.         selectorArrow.transform.up = direction;
     

    Attached Files:

  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    As Kurt-Dekker said above, you need to supply a Z value. Otherwise you just get the location of the camera (see the Unity code example). You could try this:
    Code (CSharp):
    1. Vector3 mouseposition = Input.mousePosition;
    2. mouseposition.z = Camera.main.nearClipPlane;
    3. mouseposition = Camera.main.ScreenToWorldPoint(mouseposition);
     
  7. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    Oh i misunderstood how to add it then. X works now but it still not updating while dragging. Or at all after the first drag
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Do you mean the FollowMouse method is not being called? If so, you would need to check the calling code.