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

Question How do I drag UI only on the Y-axis?

Discussion in 'Getting Started' started by Gummi_Ghost, Dec 7, 2021.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    Every guide I look up just shows me how to drag and drop in an inventory system. If I could have some help or direction on how to drag a UI element on the Y-axis in Unity 2D it would be greatly appreciated.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You might have to be a bit more specific about what you're looking for here. UI elements can be positioned in quite a few ways, and how you'd do it depends greatly on how the element is anchored and where its pivots are.

    If the element is anchored vertically, you can adjust its position using the Pos Y attribute.
    upload_2021-12-7_8-39-29.png

    If it has a relative anchor for its vertical axis, you'd position its edges using the Top and Bottom fields.
    upload_2021-12-7_8-40-43.png

    How and why you'd use either setup depends greatly on what you want to do with the element, and how you want it to scale or move with a change of the canvas size. If you haven't already, I strongly recommend reading the manual on the subject.
     
  3. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I'm trying to get player input with the event system to drag the UI, but nothing works.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Well what part isn't working? Are you receiving the user's mouse coordinates and drag state? Is the UI element moving at all? Are you getting any error messages?

    It'd be a lot easier to help if you shared your code and some screenshots of how things are setup.
     
  5. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I have this
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6.  
    7. public class MenuUIMovement : MonoBehaviour, IPointerDownHandler, IPointerClickHandler,
    8.     IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler,
    9.     IBeginDragHandler, IDragHandler, IEndDragHandler
    10.  
    11. {
    12.  
    13.     public void OnBeginDrag(PointerEventData eventData)
    14.     {
    15.         Debug.Log("Drag Begin");
    16.     }
    17.  
    18.     public void OnDrag(PointerEventData eventData)
    19.     {
    20.         Debug.Log("Dragging");
    21.  
    22.     }
    23.  
    24.     public void OnEndDrag(PointerEventData eventData)
    25.     {
    26.         Debug.Log("Drag Ended");
    27.     }
    28.  
    29.     public void OnPointerClick(PointerEventData eventData)
    30.     {
    31.         Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
    32.     }
    33.  
    34.     public void OnPointerDown(PointerEventData eventData)
    35.     {
    36.         Debug.Log("Mouse Down: " + eventData.pointerCurrentRaycast.gameObject.name);
    37.     }
    38.  
    39.     public void OnPointerEnter(PointerEventData eventData)
    40.     {
    41.         Debug.Log("Mouse Enter");
    42.     }
    43.  
    44.     public void OnPointerExit(PointerEventData eventData)
    45.     {
    46.         Debug.Log("Mouse Exit");
    47.     }
    48.  
    49.     public void OnPointerUp(PointerEventData eventData)
    50.     {
    51.         Debug.Log("Mouse Up");
    52.     }
    53.  
    54. }
    The code above is my starting point, and it tells me what's happening with the player's mouse input in the console. No matter what code I add to this base code, it does not work. I can't find any reference on how to use player input to drag anything. I will reread the Unity manual on this subject as you suggested today.
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Do you have an EventSystem in your scene? Does your canvas have a GraphicsRaycaster component? Do you have any invisible UI elements overlapping your element that may be intercepting the mouse events?

    Again, without being able to see what you're actually doing, it's hard to give you more specific help.
     
  7. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    Yes I have an event system, and yes my canvas has a graphicsRaycaster component. No, I don't have any overlapping or invisible UI. I have everything, I just can't find any code to reference that will move a UI element in a script with player input and the event system. I can take pictures later today. Thank you for your help.
     
  8. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I can take a picture later, but for now, it is a ui image on a canvas. I want to learn how to move it down with the mouse.
     
  9. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    Thank you for your help. I did learn a lot I was able to find a tutorial here: