Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Mouse Position for Screen Space - Camera?

Discussion in 'UGUI & TextMesh Pro' started by shivansps, Jan 30, 2015.

  1. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Lets say i want a UI.Image to follow the mouse, in a "Screen Space - Overlay" canvas i can do it as easy as

    Image.transform=Input.mousePosition;

    in a LateUpdate.

    But how to do the exact same thing for a Screen Space - Camera canvas?
     
    Filip8429 and Helkar333 like this.
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,687
    Exactly the same, both of the Screen Space canvases cover the entire screen. You might however have to transform the mouse coordinates from world space (through the camera of the Screen Space - Camera canvas) to get the 2D position.
    e.g.

    Code (CSharp):
    1. var screenPoint = Vector3(Input.mousePosition);
    2. screenPoint.z = 10.0f; //distance of the plane from the camera
    3. transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
     
  3. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Thank you.
     
    SimonDarksideJ likes this.
  4. fmathews34

    fmathews34

    Joined:
    Apr 6, 2016
    Posts:
    1
    this really helped me out, thanks.
     
  5. Vinnie420

    Vinnie420

    Joined:
    Mar 16, 2014
    Posts:
    2
    thanks, this really should be in the documentation
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,687
    Hiep_Cao likes this.
  7. adalynnskidmore1

    adalynnskidmore1

    Joined:
    May 31, 2019
    Posts:
    3
    I know this post is several years old now, but I want to thank @SimonDarksideJ for his answer, I have spent several days wondering why my mouse position seemed to be offset from where it should be I have looked through tons of forums and finally I have found a useful answer. This has given me a much better understanding of how Unity's UI systems work, I cannot thank you enough!
     
  8. naagi

    naagi

    Joined:
    Nov 27, 2014
    Posts:
    13
    this post may be old, but i really thank you for the post, you really save me, thank you so much
     
  9. maxlukonin

    maxlukonin

    Joined:
    Jul 20, 2019
    Posts:
    1
    Dude seven years later you still helped me out on that, really appreciate it :)
     
  10. gamedevbud

    gamedevbud

    Joined:
    Jul 13, 2019
    Posts:
    1
    This is great! Thank you!
     
  11. invisage

    invisage

    Joined:
    Jun 27, 2019
    Posts:
    23
  12. Vanille987

    Vanille987

    Joined:
    Nov 12, 2020
    Posts:
    1
    Agreed :)
     
  13. Kenthria11

    Kenthria11

    Joined:
    May 6, 2017
    Posts:
    20
    Unity 2020 update - 'error CS1955: Non-invocable member 'Vector3' cannot be used like a method.'
    Fixed with: (C#)
    Vector3 screenPoint = Input.mousePosition;
    screenPoint.z = 10.0f; //distance of the plane from the camera
    transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
     
  14. Waruna_Nirmal

    Waruna_Nirmal

    Joined:
    Apr 15, 2020
    Posts:
    1
    I was searching this for hours... Thanks !!!!
     
  15. no00ob

    no00ob

    Joined:
    Dec 13, 2017
    Posts:
    65
    Or just change:
    var screenPoint = Vector3(Input.mousePosition);
    to
    var screenPoint = (Vector3)Input.mousePosition;
     
  16. adammak

    adammak

    Joined:
    Dec 7, 2019
    Posts:
    2
    Sorry for necroing but this page is in top search for this particular problem.
    For anyone encountering this in future with Canvas Scaler:

    Code (CSharp):
    1. public void OnDrag(PointerEventData eventData)
    2. {
    3.     myRectTransform.anchoredPosition = eventData.position / myCanvas.scaleFactor;
    4. }
    works if myRectTransform has minMax Anchors set to (0,0) and its parent is in Canvas (0,0) point
     
    Hiep_Cao and MilkMax like this.