Hi,
I’m making an inventory system with a hotbar and main inventory.
I’m attempting to get the hotbar to originally be at the bottom, but as the player moves down to the bottom quarter of the screen the hotbar moves to the top. This all works fine, the only issue I’m having is when the inventory is opened, the hot bar then becomes disabled (as planned) but if I am in the bottom quarter of the screen and open the inventory and then close it, it jumps to the bottom and instantly to the top, this happens randomly. See below:
This is in my UIInventoryBar script to handle the inventory bar UI
public class UIInventoryBar : MonoBehaviour
{
private RectTransform rectTransform;
private bool _isInventoryBarPositionBottom = true;
public bool IsInventoryBarPositionBottom { get => _isInventoryBarPositionBottom; set => _isInventoryBarPositionBottom = value; }
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
}
private void Update()
{
SwitchHotBarPosition();
}
private void SwitchHotBarPosition()
{
Vector3 playerViewportPosition = Player.Instance.GetPlayerViewportPosition();
if (playerViewportPosition.y <= 0.25f)
{
rectTransform.pivot = new Vector2(0.5f, 0f);
rectTransform.anchorMin = new Vector2(0.5f, 0f);
rectTransform.anchorMax = new Vector2(0.5f, 0f);
rectTransform.anchoredPosition = new Vector2(0f, 318f);
IsInventoryBarPositionBottom = false;
}
if (playerViewportPosition.y > 0.25f && IsInventoryBarPositionBottom == false)
{
rectTransform.pivot = new Vector2(0.5f, 0f);
rectTransform.anchorMin = new Vector2(0.5f, 0f);
rectTransform.anchorMax = new Vector2(0.5f, 0f);
rectTransform.anchoredPosition = new Vector2(0f, 3f);
IsInventoryBarPositionBottom = true;
}
}
}
This is in my player script to handle the input of toggling the inventory open and closed
private void Update()
{
for (int i = 0; i < toggleInventoryKeys.Length; i++)
{
if (Input.GetKeyDown(toggleInventoryKeys[i]))
{
inventoryGameobject.SetActive(!inventoryGameobject.activeSelf);
if (inventoryGameobject.activeInHierarchy)
{
hotBarGameobject.SetActive(false);
}
else
{
hotBarGameobject.SetActive(true);
}
break;
}
}
}