Hi guys im working in a click to move game Rpg type, right now all work fine i just wanna know one thing.
I have and inventory system that if we place an item out of inventory it delete the item and work 100% but when i click out of inventory to drop it the payer move to the position since it works the click to move. wt i wanan know is how i can stop character to move if im deleting the item??
Scripts:
using UnityEngine;
using System.Collections;
public class ClickToMove : MonoBehaviour
{
NavMeshAgent navAgent;
public Inventory inventory;
// Use this for initialization
void Start ()
{
navAgent = GetComponent<NavMeshAgent> ();
}
Appreciate. Cheers
// Update is called once per frame
void Update ()
{
if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI> ().IsmouseOverGUI)
{
}
else
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonUp (0)) {
if (Physics.Raycast (ray, out hit, 1000)) {
navAgent.SetDestination (hit.point);
}
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Item")
{
inventory.AddItem(other.GetComponent<Item>());
Destroy (other.gameObject);
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class Inventory : MonoBehaviour
{
private RectTransform inventoryRect;
private float inventoryWidth, inventoryHeight;
public int slots;
public int rows;
public float slotPaddingLeft;
public float slotPaddingTop;
public float slotSize;
public GameObject slotPrefab;
private static Slot from, to;
private List<GameObject> allSlots;
public GameObject iconPrefab;
private static GameObject hoverObject;
public Canvas canvas;
private float hoverYOffset;
public EventSystem eventSystem;
private static CanvasGroup canvasGroup;
public static CanvasGroup CanvasGroup
{
get{ return canvasGroup; }
}
private bool fadingIn;
private bool fadingOut;
public float fadeTime;
//need to have has much prototypes has the number of diferente items, right now only mana and health potions
public GameObject mana;
public GameObject health;
public GameObject weapon;
private static int emptySlots;
public static int EmptySlots
{
get{ return emptySlots; }
set{ emptySlots = value; }
}
public GameObject tooltipObject;
private static GameObject tooltip;
public Text sizeTextObject;
private static Text sizeText;
public Text visualTextObject;
private static Text visualText;
void Start ()
{
tooltip = tooltipObject;
sizeText = sizeTextObject;
visualText = visualTextObject;
canvasGroup = transform.parent.GetComponent<CanvasGroup>();
CreateLayout();
}
void Update ()
{
if (Input.GetMouseButtonUp (0))
{
//Removes the selected item from the inventory
if(!eventSystem.IsPointerOverGameObject(-1) && from != null)
{
from.GetComponent<Image>().color = Color.white;
from.ClearSlot();
Destroy (GameObject.Find ("Hover"));
to = null;
from = null;
emptySlots++;
}
}
if (hoverObject != null)
{
Vector2 position;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
position.Set(position.x, position.y - hoverYOffset);
hoverObject.transform.position = canvas.transform.TransformPoint(position);
}
if (Input.GetKeyDown (KeyCode.I))
{
if(canvasGroup.alpha > 0)
{
StartCoroutine("FadeOut");
PutItemBack();
}
else
{
StartCoroutine("FadeIn");
}
}
if (Input.GetMouseButton (2))
{
if(eventSystem.IsPointerOverGameObject(-1))
{
MoveInventory();
}
}
}
public void ShowToolTip(GameObject slot)
{
Slot tmpslot = slot.GetComponent<Slot>();
if (!tmpslot.IsEmpty && hoverObject == null)
{
visualText.text = tmpslot.CurrentItem.GetToolTip();
sizeText.text = visualText.text;
tooltip.SetActive(true);
float xPos = slot.transform.position.x + slotPaddingLeft;
float yPos = slot.transform.position.y - slot.GetComponent<RectTransform>().sizeDelta.y - slotPaddingTop;
tooltip.transform.position = new Vector2(xPos, yPos);
}
}
public void HideToolTip()
{
tooltip.SetActive(false);
}
public void SaveInventory()
{
string content = string.Empty;
for (int i = 0; i < allSlots.Count; i++)
{
Slot tmp = allSlots[i].GetComponent<Slot>();
if(!tmp.IsEmpty)
{
content += i + "-" + tmp.CurrentItem.type.ToString() + "-" + tmp.Items.Count.ToString() + ";";
}
}
PlayerPrefs.SetString ("content", content);
PlayerPrefs.SetInt ("slots", slots);
PlayerPrefs.SetInt ("rows", rows);
PlayerPrefs.SetFloat ("slotPaddingLeft", slotPaddingLeft);
PlayerPrefs.SetFloat ("slotPaddingTop", slotPaddingTop);
PlayerPrefs.SetFloat ("slotSize", slotSize);
PlayerPrefs.SetFloat ("xPos", inventoryRect.position.x);
PlayerPrefs.SetFloat ("yPos", inventoryRect.position.y);
PlayerPrefs.Save();
}
public void LoadInventory()
{
string content = PlayerPrefs.GetString ("content");
slots = PlayerPrefs.GetInt ("slots");
rows = PlayerPrefs.GetInt ("rows");
slotPaddingLeft = PlayerPrefs.GetFloat ("slotPaddingLeft");
slotPaddingTop = PlayerPrefs.GetFloat ("slotPaddingTop");
slotSize = PlayerPrefs.GetFloat ("slotSize");
inventoryRect.position = new Vector3 (PlayerPrefs.GetFloat ("xPos"), PlayerPrefs.GetFloat ("yPos"), inventoryRect.position.z);
CreateLayout();
string[] splitContent = content.Split (';'); //0-MANA-3
for (int x = 0; x < splitContent.Length-1; x++)
{
string[] splitValues = splitContent[x].Split('-');
int index = Int32.Parse(splitValues[0]); //"0"
ItemType type = (ItemType)Enum.Parse(typeof(ItemType), splitValues[1]); //"MANA"
int amount = Int32.Parse(splitValues[2]); //"3"
//replace the item in inventory has many times has we have before
for(int i = 0; i < amount; i++)
{
switch (type)
{
case ItemType.MANA:
allSlots[index].GetComponent<Slot>().AddItem(mana.GetComponent<Item>());
break;
case ItemType.HEALTH:
allSlots[index].GetComponent<Slot>().AddItem(health.GetComponent<Item>());
break;
case ItemType.WEAPON:
allSlots[index].GetComponent<Slot>().AddItem(weapon.GetComponent<Item>());
break;
}
}
}
}
private void CreateLayout()
{
if(allSlots != null)
{
foreach(GameObject go in allSlots)
{
Destroy(go);
}
}
allSlots = new List<GameObject>();
hoverYOffset = slotSize * 0.01f;
emptySlots = slots;
inventoryWidth = (slots/rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
inventoryHeight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
inventoryRect = GetComponent<RectTransform>();
inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHeight);
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";
//set slot as parent of inventory, who is parent from canvas that draw sprites
newSlot.transform.SetParent(this.transform.parent);
slotRect.localPosition = inventoryRect.localPosition + new Vector3(slotPaddingLeft * (x + 1) + (slotSize * x), -slotPaddingTop * (y + 1) - (slotSize * y));
slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize * canvas.scaleFactor);
slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize * canvas.scaleFactor);
newSlot.transform.SetParent(this.transform);
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.IsAvailable)
{
tmp.AddItem(item);
return true;
}
}
}
if(emptySlots > 0)
{
PlaceEmpty(item);
}
}
return false;
}
private void MoveInventory()
{
Vector2 mousePos;
RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas.transform as RectTransform, new Vector3 (Input.mousePosition.x - (inventoryRect.sizeDelta.x / 2 * canvas.scaleFactor), Input.mousePosition.y + (inventoryRect.sizeDelta.y / 2 * canvas.scaleFactor)), canvas.worldCamera, out mousePos);
transform.position = canvas.transform.TransformPoint (mousePos);
}
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 MoveItem(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);
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 ("Canvas").transform, true);
hoverObject.transform.localScale = from.gameObject.transform.localScale;
}
}
else if (to == null)
{
to = clicked.GetComponent<Slot>();
Destroy(GameObject.Find ("Hover"));
}
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;
//reset "to" and "from"
to = null;
from = null;
Destroy(GameObject.Find ("Hover"));
}
}
private void PutItemBack()
{
if (from != null)
{
Destroy(GameObject.Find("Hover"));
from.GetComponent<Image>().color = Color.white;
from = null;
}
}
private IEnumerator FadeOut()
{
if (!fadingOut)
{
fadingOut = true;
fadingIn = false;
StopCoroutine("FadeIn");
float startAlpha = canvasGroup.alpha;
float rate = 1.0f / fadeTime;
float progress = 0.0f;
while(progress < 1.0)
{
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 = 1.0f / fadeTime;
float progress = 0.0f;
while(progress < 1.0)
{
canvasGroup.alpha = Mathf.Lerp(startAlpha, 1, progress);
progress += rate * Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 1;
fadingIn = false;
}
}
}