Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate UI element at mouse position but element shouldn't keep following mouse

Discussion in 'UGUI & TextMesh Pro' started by JordanKeiTaylor, Mar 24, 2017.

  1. JordanKeiTaylor

    JordanKeiTaylor

    Joined:
    Mar 17, 2017
    Posts:
    1
    Hi,

    As stated above, I'm trying to initialize a UI element at the current mouse position. I'm using the code below to position a panel at mouse position. The problem is, after initialization, the panel keeps following the mouse, making it impossible to click on. How do I initialize it at clicked position but then have it stay in place?

    Thanks a lot

    Example code:
    float x = Input.mousePosition.x;
    float y = Input.mousePosition.y;
    Vector3 mousePosition = new Vector3(x + 20, y - 40,0);
    _inspectionPanel.transform.position = mousePosition;
     
  2. Equ1nox

    Equ1nox

    Joined:
    Sep 7, 2016
    Posts:
    3
    Hi,

    Perhaps a Stupid response but you shouldn't do that in update() method.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I am thinking the same thing; that that code might be inside update & that explains the behaviour.
    Instantiate the panel and set its position on your click, that's it. It's done :)