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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Building an XY pad, having various problems with mouse position and sorting layers

Discussion in '2D' started by fivegrandpint, Feb 20, 2020.

  1. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    Hi so Im making an XY pad - basically a rectangle the player can click and drag a "point" (this is represented by a circle) around on, to effect two parameters at once (one controlled by X axis and one controlled by Y axis). I'm having various issues with this.

    The first is this:
    I am basically checking the mouse position when dragging on the rectangle. Then in a seperate script on the circle, I am getting that position and setting the position of the circle equal to it. But even though I have them all as public variables and they all appear to be the same - there seems to be some crazy offset that's making the circle not follow the mouse closely at all. I have no idea what it is

    Second:
    Sorting Layers dont seem to be working. As I'm not working with sprites, Im using a script to access the renderer and set the sorting layer manually, but it doesn't seem to be working at all. I have created both sorting layers and set up "Shapes" to be further down the list than "background" .. there are also order numbers set by the script and I have given the circle a higher number for the order as well as a later sorting layer and it still places it behind the rectangle.

    Please find attached at this link:
    a video of my project and below I have also attached pictures of all my scripts. Any help would be much appreciated. Thanks!
     

    Attached Files:

  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    show script
     
  3. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    all the scripts are attached right there in the images
     
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    paste in edit box so we can copy paste and test
     
    fivegrandpint likes this.
  5. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16


    This is the for checking mouse position when dragging inside the rectangle:

    Code (CSharp):
    1.  
    2.  
    3. public class XyScript : MonoBehaviour
    4. {
    5.    
    6.  
    7.     public Vector3 pos;
    8.     public Vector3 bottomEdge;
    9.     public Vector3 topEdge;
    10.     public Vector3 leftEdge;
    11.     public Vector3 rightEdge;
    12.     public Vector3 boundsExtents;
    13.     public Vector3 currentXyPosition;
    14.     public bool isDragging = false;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         boundsExtents = GetComponent<Renderer>().bounds.extents;
    20.         pos = GetComponent<Renderer>().transform.localPosition;
    21.  
    22.         leftEdge = pos + boundsExtents.x * Vector3.left;
    23.         rightEdge = pos + boundsExtents.x * Vector3.right;
    24.         bottomEdge = pos + boundsExtents.y * Vector3.down;
    25.         topEdge = pos + boundsExtents.y * Vector3.up;
    26.  
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.  
    32.     private void OnMouseDrag()
    33.     {
    34.  
    35.         isDragging = true;
    36.  
    37.         if (isDragging == true)
    38.         {
    39.             currentXyPosition = Input.mousePosition;
    40.             currentXyPosition = Camera.main.ScreenToWorldPoint(currentXyPosition);
    41.  
    42.             currentXyPosition.z = 5f;
    43.  
    44.         }
    45.  
    46.  
    47.  
    48.     }
    49.  
    50.  
    51.     private void OnMouseUp()
    52.     {
    53.         isDragging = false;
    54.     }
    55.  
    56. }
    57.  

    This is the next one for making the position of circle equal to mouse position:


    Code (CSharp):
    1. public class XyPointer : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     public Vector3 pointerCirclePos;
    5.  
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         pointerCirclePos = GetComponentInParent<XyScript>().currentXyPosition;
    11.  
    12.  
    13.         if (GetComponentInParent<XyScript>().isDragging == true)
    14.         {
    15.  
    16.             this.transform.localPosition = pointerCirclePos;
    17.         }
    18.    
    19.  
    20.     }
    21.  
    22. }
    23.  

    and this is the one for setting the sorting layers:


    Code (CSharp):
    1. public class SortingLayerAssignment : MonoBehaviour
    2. {
    3.     public string nameOfSortingLayer;
    4.     public int orderOfSortingLayer;
    5.  
    6.  
    7.     // Start is called before the first frame update
    8.     void Awake()
    9.     {
    10.  
    11.         this.GetComponent<Renderer>().sortingOrder = orderOfSortingLayer;
    12.         this.GetComponent<Renderer>().sortingLayerName = nameOfSortingLayer;
    13.     }
    14.  
    15. }
    16.  
     
  6. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    i have posted but my post is waiting for moderation
     
  7. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    ok sir
     
    fivegrandpint likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Normally this kind of thing is when you're not using Perspective rather than Orthographic projection on the camera and/or not taking the Z position into account. If it's 2D and you're using Ortho then Z=0 is fine. You also seem to be using Transform.localPosition for some reason to get world-space but those are local-space.
     
    fivegrandpint likes this.
  9. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16

    Ah ok, this could be one of the issues.. so I should just use transform.position ?

    Also, any ideas on the sorting layers?

    Thanks a lot for your help, really appreciate it. Total noob to C# and unity
     
  10. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    yeah just tested there and replacing localposition with position has made it work. Just need to figure out the sorting layers thing now! :D
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Sorting layers are for sprites. If you're using 3D stuff the Z position determines that.

    It's hard to tell why you're not using 2D sprites/physics but instead using 3D stuff but I'm sure you have your reasons so render order is controlled by it's depth.
     
    fivegrandpint likes this.
  12. fivegrandpint

    fivegrandpint

    Joined:
    Jan 16, 2020
    Posts:
    16
    Ah okay thanks! I had followed this post: https://forum.unity.com/threads/using-non-sprites-with-the-new-sorting-layers.211822/ thinking that the script from there would sort out the render order.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Well I'm probably mistaken then by the look of that thread as I've never used it in 3D. Certainly the Z order is used as the primary sort mechanism though.