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

Offset isn't working

Discussion in 'Scripting' started by UndeadMadison, May 31, 2020.

  1. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    I still am watching a tutorial on how to do this but no matter how many times I redo this it still isn't working. I didn't do exactly what he did because I don't want my inventory to change size with the slots. But nothing in the inventory that I know of is messing this up. I am trying to make the hover object to follow the mouse and be able to place it back down. The gameobject is covering up the mouse so I was doing an offset for it like the guy said but even after I do what he said it won't allow me to click the actual slots still and the offset doesn't even look like it changed. It is only two lines of code but none of it is working. I am going to give all the Inventory Code in case I did something else wrong in it that is causing this. I have a few other scripts associated but this one is where the offset stuff is.

    Code (CSharp):
    1.     public static CanvasGroup canvasGroup;
    2.  
    3.     private bool fadingOut;
    4.     private bool fadingIn;
    5.  
    6.     private float hoverYOffset;
    7.     private float inventoryWidth;
    8.     private float inventoryHeight;
    9.  
    10.     private static GameObject hoverObject;
    11.  
    12.     private static int emptySlots;
    13.  
    14.     private List<GameObject> allSlots;
    15.  
    16.     private RectTransform inventoryRect;
    17.  
    18.     private Slot from, to;
    19.  
    20.     public Canvas canvas;
    21.  
    22.     public float fadeTime;
    23.     public float paddingLeft;
    24.     public float paddingTop;
    25.     public float slotSize;
    26.     public GameObject iconPrefab;
    27.     public GameObject player;
    28.     public GameObject slotPrefab;
    29.  
    30.     public int slots;
    31.     public int rows;
    32.  
    33.     public static int EmptySlots { get => emptySlots; set => emptySlots = value; }
    34.     public static CanvasGroup CanvasGroup { get => canvasGroup; }
    35.  
    36.     void Update()
    37.     {
    38.         if(hoverObject != null)
    39.         {
    40.             Vector2 position;
    41.             RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
    42.  
    43.             //This is supposed to use the offset for the position
    44.             position.Set(position.x, position.y - hoverYOffset);
    45.  
    46.             hoverObject.transform.position = canvas.transform.TransformPoint(position);
    47.         }
    48.  
    49.         if (Input.GetKeyDown(KeyCode.E))
    50.         {
    51.             if (CanvasGroup.alpha > 0)
    52.             {
    53.                 StartCoroutine("FadeOut");
    54.             } else { StartCoroutine("FadeIn"); }
    55.         }
    56.  
    57.         if(CanvasGroup.alpha == 1)
    58.         {
    59.             player.GetComponent<PlayerController>().enabled = false;
    60.             Cursor.lockState = CursorLockMode.None;
    61.             Cursor.visible = true;
    62.         } else
    63.         {
    64.             player.GetComponent<PlayerController>().enabled = true;
    65.             Cursor.lockState = CursorLockMode.Locked;
    66.             Cursor.visible = false;
    67.         }
    68.     }
    69.  
    70.     void Start()
    71.     {
    72.         canvasGroup = transform.root.GetComponent<CanvasGroup>();
    73.         CreateLayout();
    74.     }
    75.  
    76.     private void CreateLayout()
    77.     {
    78.         allSlots = new List<GameObject>();
    79.  
    80.         //This is what sets the offset
    81.         hoverYOffset = slotSize * 0.01f;
    82.  
    83.         EmptySlots = slots;
    84.  
    85.         inventoryRect = GetComponent<RectTransform>();
    86.  
    87.         int columns = slots / rows;
    88.  
    89.         for (int y = 0; y < rows; y++)
    90.         {
    91.             for (int x = 0; x < columns; x++)
    92.             {
    93.                 GameObject newSlot = (GameObject)Instantiate(slotPrefab);
    94.  
    95.                 RectTransform slotRect = newSlot.GetComponent<RectTransform>();
    96.  
    97.                 newSlot.name = "Slot";
    98.  
    99.                 newSlot.transform.SetParent(this.transform);
    100.  
    101.                 slotRect.localPosition = inventoryRect.localPosition + new Vector3(paddingLeft * (x + 1) + (slotSize * x), -paddingTop * (y + 1) - (slotSize * y));
    102.  
    103.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize);
    104.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
    105.  
    106.                 allSlots.Add(newSlot);
    107.             }
    108.         }
    109.     }
    110.  
    111.     public bool AddItem(Item item)
    112.     {
    113.         if (item.maxSize == 1)
    114.         {
    115.             PlaceEmpty(item);
    116.             return true;
    117.         } else
    118.         {
    119.             foreach (GameObject slot in allSlots)
    120.             {
    121.                 Slot tmp = slot.GetComponent<Slot>();
    122.                 if (!tmp.isEmpty)
    123.                 {
    124.                     if (tmp.CurrentItem.type == item.type && tmp.isStackable)
    125.                     {
    126.                         tmp.AddItem(item);
    127.                         return true;
    128.                     }
    129.                 }
    130.             }
    131.             if (EmptySlots > 0)
    132.             {
    133.                 PlaceEmpty(item);
    134.             }
    135.         }
    136.         return false;
    137.     }
    138.  
    139.     private bool PlaceEmpty(Item item)
    140.     {
    141.         if (EmptySlots > 0)
    142.         {
    143.             foreach (GameObject slot in allSlots)
    144.             {
    145.                 Slot tmp = slot.GetComponent<Slot>();
    146.                 if (tmp.isEmpty)
    147.                 {
    148.                     tmp.AddItem(item);
    149.                     EmptySlots--;
    150.                     return true;
    151.                 }
    152.             }
    153.         }
    154.  
    155.         return false;
    156.     }
    157.  
    158.     public void MoveItems(GameObject clicked)
    159.     {
    160.         if (from == null && CanvasGroup.alpha == 1)
    161.         {
    162.             if (!clicked.GetComponent<Slot>().isEmpty)
    163.             {
    164.                 from = clicked.GetComponent<Slot>();
    165.                 from.GetComponent<Image>().color = Color.gray;
    166.  
    167.                 hoverObject = (GameObject)Instantiate(iconPrefab);
    168.                 Debug.Log(hoverObject.GetComponent<Image>().sprite = clicked.GetComponent<Image>().sprite);
    169.                 hoverObject.name = "Hover";
    170.  
    171.                 RectTransform hoverTransform = hoverObject.GetComponent<RectTransform>();
    172.                 RectTransform clickedTransform = clicked.GetComponent<RectTransform>();
    173.  
    174.                 hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
    175.                 hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);
    176.  
    177.                 hoverObject.transform.SetParent(GameObject.Find("PlayerHUD").transform, true);
    178.                 hoverObject.transform.localScale = from.gameObject.transform.localScale;
    179.             }
    180.         } else if (to == null)
    181.         {
    182.             to = clicked.GetComponent<Slot>();
    183.         }
    184.  
    185.         if (to != null && from != null)
    186.         {
    187.             Stack<Item> tmpTo = new Stack<Item>(to.Items);
    188.             to.AddItems(from.Items);
    189.  
    190.             if (tmpTo.Count == 0)
    191.             {
    192.                 from.ClearSlot();
    193.             } else { from.AddItems(tmpTo); }
    194.  
    195.             from.GetComponent<Image>().color = Color.white;
    196.             to = null;
    197.             from = null;
    198.             hoverObject = null;
    199.         }
    200.     }
    201.  
    202.     private IEnumerator FadeOut()
    203.     {
    204.         if (!fadingOut)
    205.         {
    206.             fadingOut = true;
    207.             fadingIn = false;
    208.             StopCoroutine("FadeIn");
    209.  
    210.             float startAlpha = CanvasGroup.alpha;
    211.  
    212.             float rate = 0.1f / fadeTime;
    213.  
    214.             float progress = 0.0f;
    215.  
    216.             while (progress < 1.0f)
    217.             {
    218.                 CanvasGroup.alpha = Mathf.Lerp(startAlpha, 0, progress);
    219.  
    220.                 progress += rate * Time.deltaTime;
    221.  
    222.                 yield return null;
    223.             }
    224.  
    225.             CanvasGroup.alpha = 0;
    226.  
    227.             fadingOut = false;
    228.         }
    229.     }
    230.  
    231.     private IEnumerator FadeIn()
    232.     {
    233.         if (!fadingIn)
    234.         {
    235.             fadingOut = false;
    236.             fadingIn = true;
    237.             StopCoroutine("FadeOut");
    238.  
    239.             float startAlpha = CanvasGroup.alpha;
    240.  
    241.             float rate = 0.1f / fadeTime;
    242.  
    243.             float progress = 0.0f;
    244.  
    245.             while (progress < 1.0f)
    246.             {
    247.                 CanvasGroup.alpha = Mathf.Lerp(startAlpha, 1, progress);
    248.  
    249.                 progress += rate * Time.deltaTime;
    250.  
    251.                 yield return null;
    252.             }
    253.  
    254.             CanvasGroup.alpha = 1;
    255.  
    256.             fadingIn = false;
    257.         }
    258.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    It's kinda tough to understand what might be going wrong here because we don't have your entire project set up... here are some tips to try and track down what's going on.

    - Debug.Log() is your friend. Use Debug.Log() to display some of the values involved, such as local position and offsets, and that can give you some idea of where your offsets are not what you want.

    - The other thing going on here that can confuse all of this is that a lot of the UnityEngine.UI layout components will actually "drive" your components as you move them around. This can confound attempts to figure out what is really going on. For instance if you add or remove items from a grid, the layout code detects it and reflows stuff, possibly overwriting values your code has set.

    In general you want to simplify, simplify, simplify. Try making a little scene and just dragging and dropping things that are not part of a larger hierarchy, and combining that with Debug.Log() you can start to understand what is going on without having a lot of possible things going on.
     
  3. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    i
    I did use the Debug.Log() earlier on the offset and it has 0.7 which is what it is supposed to have because I have my x and y for the grid layout group set to 70. I have hardly anything in my hierarchy besides the inventory, test ground and a cube that is used to gather a piece of wood for testing.
     
  4. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    Okay so I have broken up the one bit of code that sets the positions in the update and supposedly the position.y - hoverOffset isn't working. I don't know exactly why it isn't but that is what I found out.