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.
public static CanvasGroup canvasGroup;
private bool fadingOut;
private bool fadingIn;
private float hoverYOffset;
private float inventoryWidth;
private float inventoryHeight;
private static GameObject hoverObject;
private static int emptySlots;
private List<GameObject> allSlots;
private RectTransform inventoryRect;
private Slot from, to;
public Canvas canvas;
public float fadeTime;
public float paddingLeft;
public float paddingTop;
public float slotSize;
public GameObject iconPrefab;
public GameObject player;
public GameObject slotPrefab;
public int slots;
public int rows;
public static int EmptySlots { get => emptySlots; set => emptySlots = value; }
public static CanvasGroup CanvasGroup { get => canvasGroup; }
void Update()
{
if(hoverObject != null)
{
Vector2 position;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
//This is supposed to use the offset for the position
position.Set(position.x, position.y - hoverYOffset);
hoverObject.transform.position = canvas.transform.TransformPoint(position);
}
if (Input.GetKeyDown(KeyCode.E))
{
if (CanvasGroup.alpha > 0)
{
StartCoroutine("FadeOut");
} else { StartCoroutine("FadeIn"); }
}
if(CanvasGroup.alpha == 1)
{
player.GetComponent<PlayerController>().enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
} else
{
player.GetComponent<PlayerController>().enabled = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Start()
{
canvasGroup = transform.root.GetComponent<CanvasGroup>();
CreateLayout();
}
private void CreateLayout()
{
allSlots = new List<GameObject>();
//This is what sets the offset
hoverYOffset = slotSize * 0.01f;
EmptySlots = slots;
inventoryRect = GetComponent<RectTransform>();
int columns = slots / rows;
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < columns; x++)
{
GameObject newSlot = (GameObject)Instantiate(slotPrefab);
RectTransform slotRect = newSlot.GetComponent<RectTransform>();
newSlot.name = "Slot";
newSlot.transform.SetParent(this.transform);
slotRect.localPosition = inventoryRect.localPosition + new Vector3(paddingLeft * (x + 1) + (slotSize * x), -paddingTop * (y + 1) - (slotSize * y));
slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize);
slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
allSlots.Add(newSlot);
}
}
}
public bool AddItem(Item item)
{
if (item.maxSize == 1)
{
PlaceEmpty(item);
return true;
} else
{
foreach (GameObject slot in allSlots)
{
Slot tmp = slot.GetComponent<Slot>();
if (!tmp.isEmpty)
{
if (tmp.CurrentItem.type == item.type && tmp.isStackable)
{
tmp.AddItem(item);
return true;
}
}
}
if (EmptySlots > 0)
{
PlaceEmpty(item);
}
}
return false;
}
private bool PlaceEmpty(Item item)
{
if (EmptySlots > 0)
{
foreach (GameObject slot in allSlots)
{
Slot tmp = slot.GetComponent<Slot>();
if (tmp.isEmpty)
{
tmp.AddItem(item);
EmptySlots--;
return true;
}
}
}
return false;
}
public void MoveItems(GameObject clicked)
{
if (from == null && CanvasGroup.alpha == 1)
{
if (!clicked.GetComponent<Slot>().isEmpty)
{
from = clicked.GetComponent<Slot>();
from.GetComponent<Image>().color = Color.gray;
hoverObject = (GameObject)Instantiate(iconPrefab);
Debug.Log(hoverObject.GetComponent<Image>().sprite = clicked.GetComponent<Image>().sprite);
hoverObject.name = "Hover";
RectTransform hoverTransform = hoverObject.GetComponent<RectTransform>();
RectTransform clickedTransform = clicked.GetComponent<RectTransform>();
hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);
hoverObject.transform.SetParent(GameObject.Find("PlayerHUD").transform, true);
hoverObject.transform.localScale = from.gameObject.transform.localScale;
}
} else if (to == null)
{
to = clicked.GetComponent<Slot>();
}
if (to != null && from != null)
{
Stack<Item> tmpTo = new Stack<Item>(to.Items);
to.AddItems(from.Items);
if (tmpTo.Count == 0)
{
from.ClearSlot();
} else { from.AddItems(tmpTo); }
from.GetComponent<Image>().color = Color.white;
to = null;
from = null;
hoverObject = null;
}
}
private IEnumerator FadeOut()
{
if (!fadingOut)
{
fadingOut = true;
fadingIn = false;
StopCoroutine("FadeIn");
float startAlpha = CanvasGroup.alpha;
float rate = 0.1f / fadeTime;
float progress = 0.0f;
while (progress < 1.0f)
{
CanvasGroup.alpha = Mathf.Lerp(startAlpha, 0, progress);
progress += rate * Time.deltaTime;
yield return null;
}
CanvasGroup.alpha = 0;
fadingOut = false;
}
}
private IEnumerator FadeIn()
{
if (!fadingIn)
{
fadingOut = false;
fadingIn = true;
StopCoroutine("FadeOut");
float startAlpha = CanvasGroup.alpha;
float rate = 0.1f / fadeTime;
float progress = 0.0f;
while (progress < 1.0f)
{
CanvasGroup.alpha = Mathf.Lerp(startAlpha, 1, progress);
progress += rate * Time.deltaTime;
yield return null;
}
CanvasGroup.alpha = 1;
fadingIn = false;
}
}