Search Unity

Stop click to move when dropping

Discussion in 'Scripting' started by Paykoman, Aug 27, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickToMove : MonoBehaviour
    5. {
    6.     NavMeshAgent navAgent;
    7.  
    8.     public Inventory inventory;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         navAgent = GetComponent<NavMeshAgent> ();
    14.     }
    15.  
    16. Appreciate. Cheers
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.         if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI> ().IsmouseOverGUI)
    22.         {
    23.  
    24.         }
    25.         else
    26.         {
    27.             RaycastHit hit;
    28.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    29.  
    30.             if (Input.GetMouseButtonUp (0)) {
    31.                 if (Physics.Raycast (ray, out hit, 1000)) {
    32.                     navAgent.SetDestination (hit.point);
    33.                 }
    34.             }
    35.         }
    36.     }
    37.  
    38.     private void OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.tag == "Item")
    41.         {
    42.             inventory.AddItem(other.GetComponent<Item>());
    43.             Destroy (other.gameObject);
    44.         }
    45.     }
    46. }
    47.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6. using System;
    7.  
    8. public class Inventory : MonoBehaviour
    9. {
    10.     private RectTransform inventoryRect;
    11.  
    12.     private float inventoryWidth, inventoryHeight;
    13.  
    14.     public int slots;
    15.  
    16.     public int rows;
    17.  
    18.     public float slotPaddingLeft;
    19.  
    20.     public float slotPaddingTop;
    21.  
    22.     public float slotSize;
    23.  
    24.     public GameObject slotPrefab;
    25.  
    26.     private static Slot from, to;
    27.  
    28.     private List<GameObject> allSlots;
    29.  
    30.     public GameObject iconPrefab;
    31.  
    32.     private static GameObject hoverObject;
    33.  
    34.     public Canvas canvas;
    35.  
    36.     private float hoverYOffset;
    37.  
    38.     public EventSystem eventSystem;
    39.  
    40.  
    41.     private static CanvasGroup canvasGroup;
    42.  
    43.     public static CanvasGroup CanvasGroup
    44.     {  
    45.         get{ return canvasGroup; }
    46.     }
    47.  
    48.  
    49.     private bool fadingIn;
    50.  
    51.     private bool fadingOut;
    52.  
    53.     public float fadeTime;
    54.  
    55.  
    56.     //need to have has much prototypes has the number of diferente items, right now only mana and health potions
    57.     public GameObject mana;
    58.  
    59.     public GameObject health;
    60.  
    61.     public GameObject weapon;
    62.  
    63.  
    64.     private static int emptySlots;
    65.  
    66.     public static int EmptySlots
    67.     {
    68.         get{ return emptySlots; }
    69.         set{ emptySlots = value; }
    70.     }
    71.  
    72.  
    73.     public GameObject tooltipObject;
    74.  
    75.     private static GameObject tooltip;
    76.  
    77.  
    78.     public Text sizeTextObject;
    79.  
    80.     private static Text sizeText;
    81.  
    82.  
    83.     public Text visualTextObject;
    84.  
    85.     private static Text visualText;
    86.  
    87.  
    88.  
    89.     void Start ()
    90.     {
    91.         tooltip = tooltipObject;
    92.  
    93.         sizeText = sizeTextObject;
    94.  
    95.         visualText = visualTextObject;
    96.  
    97.         canvasGroup = transform.parent.GetComponent<CanvasGroup>();
    98.  
    99.         CreateLayout();
    100.     }
    101.    
    102.     void Update ()
    103.     {
    104.         if (Input.GetMouseButtonUp (0))
    105.         {
    106.             //Removes the selected item from the inventory
    107.             if(!eventSystem.IsPointerOverGameObject(-1) && from != null)
    108.             {
    109.                 from.GetComponent<Image>().color = Color.white;
    110.  
    111.                 from.ClearSlot();
    112.  
    113.                 Destroy (GameObject.Find ("Hover"));
    114.  
    115.                 to = null;
    116.  
    117.                 from = null;
    118.  
    119.                 emptySlots++;
    120.             }
    121.         }
    122.  
    123.         if (hoverObject != null)
    124.         {
    125.             Vector2 position;
    126.  
    127.             RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
    128.  
    129.             position.Set(position.x, position.y - hoverYOffset);
    130.  
    131.             hoverObject.transform.position = canvas.transform.TransformPoint(position);
    132.         }
    133.  
    134.         if (Input.GetKeyDown (KeyCode.I))
    135.         {
    136.             if(canvasGroup.alpha > 0)
    137.             {
    138.                 StartCoroutine("FadeOut");
    139.  
    140.                 PutItemBack();
    141.             }
    142.             else
    143.             {
    144.                 StartCoroutine("FadeIn");
    145.             }
    146.         }
    147.  
    148.         if (Input.GetMouseButton (2))
    149.         {
    150.             if(eventSystem.IsPointerOverGameObject(-1))
    151.             {
    152.                 MoveInventory();
    153.             }
    154.         }
    155.     }
    156.  
    157.     public void ShowToolTip(GameObject slot)
    158.     {
    159.         Slot tmpslot = slot.GetComponent<Slot>();
    160.  
    161.         if (!tmpslot.IsEmpty && hoverObject == null)
    162.         {
    163.             visualText.text = tmpslot.CurrentItem.GetToolTip();
    164.  
    165.             sizeText.text = visualText.text;
    166.  
    167.  
    168.             tooltip.SetActive(true);
    169.  
    170.  
    171.             float xPos = slot.transform.position.x + slotPaddingLeft;
    172.  
    173.             float yPos = slot.transform.position.y - slot.GetComponent<RectTransform>().sizeDelta.y - slotPaddingTop;
    174.  
    175.             tooltip.transform.position = new Vector2(xPos, yPos);
    176.         }
    177.  
    178.     }
    179.  
    180.     public void HideToolTip()
    181.     {
    182.         tooltip.SetActive(false);
    183.     }
    184.  
    185.     public void SaveInventory()
    186.     {
    187.         string content = string.Empty;
    188.  
    189.         for (int i = 0; i < allSlots.Count; i++)
    190.         {
    191.             Slot tmp = allSlots[i].GetComponent<Slot>();
    192.  
    193.             if(!tmp.IsEmpty)
    194.             {
    195.                 content += i + "-" + tmp.CurrentItem.type.ToString() + "-" + tmp.Items.Count.ToString() + ";";
    196.             }
    197.         }
    198.  
    199.         PlayerPrefs.SetString ("content", content);
    200.  
    201.         PlayerPrefs.SetInt ("slots", slots);
    202.  
    203.         PlayerPrefs.SetInt ("rows", rows);
    204.  
    205.         PlayerPrefs.SetFloat ("slotPaddingLeft", slotPaddingLeft);
    206.  
    207.         PlayerPrefs.SetFloat ("slotPaddingTop", slotPaddingTop);
    208.  
    209.         PlayerPrefs.SetFloat ("slotSize", slotSize);
    210.  
    211.         PlayerPrefs.SetFloat ("xPos", inventoryRect.position.x);
    212.  
    213.         PlayerPrefs.SetFloat ("yPos", inventoryRect.position.y);
    214.  
    215.         PlayerPrefs.Save();
    216.     }
    217.  
    218.     public void LoadInventory()
    219.     {
    220.         string content = PlayerPrefs.GetString ("content");
    221.  
    222.         slots = PlayerPrefs.GetInt ("slots");
    223.  
    224.         rows = PlayerPrefs.GetInt ("rows");
    225.  
    226.         slotPaddingLeft = PlayerPrefs.GetFloat ("slotPaddingLeft");
    227.  
    228.         slotPaddingTop = PlayerPrefs.GetFloat ("slotPaddingTop");
    229.  
    230.         slotSize = PlayerPrefs.GetFloat ("slotSize");
    231.  
    232.         inventoryRect.position = new Vector3 (PlayerPrefs.GetFloat ("xPos"), PlayerPrefs.GetFloat ("yPos"), inventoryRect.position.z);
    233.  
    234.         CreateLayout();
    235.  
    236.         string[] splitContent = content.Split (';');    //0-MANA-3
    237.  
    238.         for (int x = 0; x < splitContent.Length-1; x++)
    239.         {
    240.             string[] splitValues = splitContent[x].Split('-');
    241.  
    242.             int index = Int32.Parse(splitValues[0]);    //"0"
    243.  
    244.             ItemType type = (ItemType)Enum.Parse(typeof(ItemType), splitValues[1]);    //"MANA"
    245.  
    246.             int amount = Int32.Parse(splitValues[2]);    //"3"
    247.  
    248.             //replace the item in inventory has many times has we have before
    249.             for(int i = 0; i < amount; i++)
    250.             {
    251.                 switch (type)
    252.                 {
    253.                     case ItemType.MANA:
    254.                         allSlots[index].GetComponent<Slot>().AddItem(mana.GetComponent<Item>());
    255.                         break;
    256.                     case ItemType.HEALTH:
    257.                         allSlots[index].GetComponent<Slot>().AddItem(health.GetComponent<Item>());
    258.                         break;
    259.                     case ItemType.WEAPON:
    260.                         allSlots[index].GetComponent<Slot>().AddItem(weapon.GetComponent<Item>());
    261.                         break;
    262.                 }
    263.             }
    264.         }
    265.     }
    266.  
    267.     private void CreateLayout()
    268.     {
    269.         if(allSlots != null)
    270.         {
    271.             foreach(GameObject go in allSlots)
    272.             {
    273.                 Destroy(go);
    274.             }
    275.         }
    276.  
    277.         allSlots = new List<GameObject>();
    278.  
    279.         hoverYOffset = slotSize * 0.01f;
    280.  
    281.         emptySlots = slots;
    282.  
    283.         inventoryWidth = (slots/rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    284.         inventoryHeight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
    285.  
    286.         inventoryRect = GetComponent<RectTransform>();
    287.  
    288.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
    289.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHeight);
    290.    
    291.         int columns = slots / rows;
    292.  
    293.         for (int y = 0; y < rows; y++)
    294.         {
    295.             for(int x = 0; x < columns; x++)
    296.             {
    297.                 GameObject newSlot = (GameObject)Instantiate(slotPrefab);
    298.  
    299.                 RectTransform slotRect = newSlot.GetComponent<RectTransform>();
    300.  
    301.                 newSlot.name = "Slot";
    302.  
    303.                 //set slot as parent of inventory, who is parent from canvas that draw sprites
    304.                 newSlot.transform.SetParent(this.transform.parent);
    305.  
    306.                 slotRect.localPosition = inventoryRect.localPosition + new Vector3(slotPaddingLeft * (x + 1) + (slotSize * x), -slotPaddingTop * (y + 1) - (slotSize * y));
    307.  
    308.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize * canvas.scaleFactor);  
    309.  
    310.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize * canvas.scaleFactor);      
    311.  
    312.                 newSlot.transform.SetParent(this.transform);
    313.  
    314.                 allSlots.Add (newSlot);
    315.             }
    316.         }
    317.     }
    318.  
    319.     public bool AddItem(Item item)
    320.     {
    321.         if (item.maxSize == 1)
    322.         {
    323.             PlaceEmpty(item);
    324.             return true;
    325.         }
    326.         else
    327.         {
    328.             foreach (GameObject slot in allSlots)
    329.             {
    330.                 Slot tmp = slot.GetComponent<Slot>();
    331.  
    332.                 if(!tmp.IsEmpty)
    333.                 {
    334.                     if(tmp.CurrentItem.type == item.type && tmp.IsAvailable)
    335.                     {
    336.                         tmp.AddItem(item);
    337.                         return true;
    338.                     }
    339.                 }
    340.             }
    341.             if(emptySlots > 0)
    342.             {
    343.                 PlaceEmpty(item);
    344.             }
    345.         }
    346.  
    347.         return false;
    348.     }
    349.  
    350.     private void MoveInventory()
    351.     {
    352.         Vector2 mousePos;
    353.  
    354.         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);
    355.  
    356.         transform.position = canvas.transform.TransformPoint (mousePos);
    357.     }
    358.  
    359.     private bool PlaceEmpty(Item item)
    360.     {
    361.         if (emptySlots > 0)
    362.         {
    363.             foreach (GameObject slot in allSlots)
    364.             {
    365.                 Slot tmp = slot.GetComponent<Slot>();
    366.  
    367.                 if(tmp.IsEmpty)
    368.                 {
    369.                     tmp.AddItem(item);
    370.                     emptySlots --;
    371.                     return true;
    372.                 }
    373.             }
    374.         }
    375.  
    376.         return false;
    377.     }
    378.  
    379.     public void MoveItem(GameObject clicked)
    380.     {
    381.         if (from == null && canvasGroup.alpha == 1)
    382.         {
    383.             if (!clicked.GetComponent<Slot>().IsEmpty)
    384.             {
    385.                 from = clicked.GetComponent<Slot>();
    386.  
    387.                 from.GetComponent<Image>().color = Color.gray;
    388.  
    389.  
    390.                 hoverObject = (GameObject)Instantiate(iconPrefab);
    391.  
    392.                 hoverObject.GetComponent<Image>().sprite = clicked.GetComponent<Image>().sprite;
    393.  
    394.                 hoverObject.name = "Hover";
    395.  
    396.  
    397.                 RectTransform hoverTransform = hoverObject.GetComponent<RectTransform>();
    398.  
    399.                 RectTransform clickedTransform = clicked.GetComponent<RectTransform>();
    400.  
    401.  
    402.                 hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
    403.  
    404.                 hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);
    405.  
    406.  
    407.                 hoverObject.transform.SetParent(GameObject.Find ("Canvas").transform, true);
    408.  
    409.                 hoverObject.transform.localScale = from.gameObject.transform.localScale;
    410.             }
    411.         }
    412.         else if (to == null)
    413.         {
    414.             to = clicked.GetComponent<Slot>();
    415.  
    416.             Destroy(GameObject.Find ("Hover"));
    417.         }
    418.         if (to != null && from != null)
    419.         {
    420.             Stack<Item> tmpTo = new Stack<Item>(to.Items);
    421.  
    422.             to.AddItems(from.Items);
    423.  
    424.             if(tmpTo.Count == 0)
    425.             {
    426.                 from.ClearSlot();
    427.             }
    428.             else
    429.             {
    430.                 from.AddItems(tmpTo);
    431.             }
    432.  
    433.             from.GetComponent<Image>().color = Color.white;
    434.  
    435.             //reset "to" and "from"
    436.             to = null;
    437.  
    438.             from = null;
    439.  
    440.             Destroy(GameObject.Find ("Hover"));
    441.         }
    442.     }
    443.  
    444.     private void PutItemBack()
    445.     {
    446.         if (from != null)
    447.         {
    448.             Destroy(GameObject.Find("Hover"));
    449.  
    450.             from.GetComponent<Image>().color = Color.white;
    451.  
    452.             from = null;
    453.         }
    454.     }
    455.  
    456.     private IEnumerator FadeOut()
    457.     {
    458.         if (!fadingOut)
    459.         {
    460.             fadingOut = true;
    461.  
    462.             fadingIn = false;
    463.  
    464.             StopCoroutine("FadeIn");
    465.  
    466.  
    467.             float startAlpha = canvasGroup.alpha;
    468.  
    469.             float rate = 1.0f / fadeTime;
    470.  
    471.             float progress = 0.0f;
    472.  
    473.  
    474.             while(progress < 1.0)
    475.             {
    476.                 canvasGroup.alpha = Mathf.Lerp(startAlpha, 0, progress);
    477.  
    478.                 progress += rate * Time.deltaTime;
    479.  
    480.                 yield return null;
    481.             }
    482.  
    483.             canvasGroup.alpha = 0;
    484.  
    485.             fadingOut = false;
    486.         }
    487.     }
    488.  
    489.     private IEnumerator FadeIn()
    490.     {
    491.         if (!fadingIn)
    492.         {
    493.             fadingOut = false;
    494.            
    495.             fadingIn = true;
    496.            
    497.             StopCoroutine("FadeOut");
    498.            
    499.            
    500.             float startAlpha = canvasGroup.alpha;
    501.            
    502.             float rate = 1.0f / fadeTime;
    503.            
    504.             float progress = 0.0f;
    505.            
    506.            
    507.             while(progress < 1.0)
    508.             {
    509.                 canvasGroup.alpha = Mathf.Lerp(startAlpha, 1, progress);
    510.                
    511.                 progress += rate * Time.deltaTime;
    512.                
    513.                 yield return null;
    514.             }
    515.            
    516.             canvasGroup.alpha = 1;
    517.            
    518.             fadingIn = false;
    519.         }
    520.     }
    521. }
    522.  
     
  2. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Might I recommend a clear button so you click the button instead of the ground.

    sorry for the non coding answer.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I already thing on that.. Appreciate ;)
     
  4. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    sorry for being no help then. Have a nice day.
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Np. its an option... anyway i will see if someone has other ideas. have a nice day too
     
  6. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi again guys. So back to this thread i still get this big problem to solve after some attempts, using diferente bools and so on. So right now wts going on is this:

    1 - the functionality of my system is click to move player, open inventory with "I" key and bank with "E" key

    2 - if i click with left mouse button in on inventory icon and click in other slot it change position

    3 - if i click left mouse to take item to the mouse and click out of inventory it destroys it

    4 - if i click left mouse to take item and click in bank inventory it puts it in bank

    Now the problem is this, if i click on item inside inventory and i move mouse out of inventory even without click the player start to move, for example when im moving from inventory to bank i i pass with mouse out of a inventory/bank window player start move alone, and when i try to delete item when i click in floor to delete player move to that same position.

    I try alot of way, so wt i need is when i hv a item "Hover" the icon the place it in other place or delete it i have to stop my player to move.

    This are my inventory / clicktomove scripts and this is a pick of my game. If someone can help me i will appreciate alot :)

    PS: Sorry for the big code -.-



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(NavMeshAgent))]      
    5. public class ClickToMove : MonoBehaviour
    6. {
    7.     private Vector3 targetPosition;              
    8.  
    9.     const int LEFT_MOUSE_BUTTON = 0;          
    10.  
    11.     NavMeshAgent navAgent;
    12.  
    13.     public Inventory inventory;
    14.  
    15.     private Inventory bank;
    16.  
    17.  
    18.  
    19.     // Use this for initialization
    20.     void Awake ()
    21.     {
    22.         navAgent = GetComponent<NavMeshAgent>();
    23.     }
    24.  
    25.  
    26.  
    27.     void Start()
    28.     {
    29.         targetPosition = transform.position;
    30.     }
    31.  
    32.  
    33.  
    34.     // Update is called once per frame
    35.     void Update ()
    36.     {
    37.         if (Input.GetMouseButton (LEFT_MOUSE_BUTTON))
    38.             SetTargetPosition();
    39.  
    40.         MovePlayer();
    41.  
    42.         if (Input.GetKeyDown (KeyCode.I))
    43.         {
    44.             inventory.Open();
    45.         }
    46.         if(Input.GetKeyDown (KeyCode.E))
    47.         {
    48.             if(bank != null)
    49.             {
    50.                 bank.Open();
    51.             }
    52.         }
    53.     }
    54.  
    55.  
    56.  
    57.     void SetTargetPosition()
    58.     {
    59.         Plane plane = new Plane(Vector3.up, transform.position);
    60.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    61.         float point = 0;
    62.  
    63.         if (plane.Raycast (ray, out point))
    64.             targetPosition = ray.GetPoint (point);
    65.     }
    66.  
    67.  
    68.  
    69.     private void MovePlayer()
    70.     {
    71.         if (GameObject.Find ("MouseOverUI").GetComponent<MouseOverUI>().IsmouseOverGUI)
    72.         {
    73.  
    74.         }
    75.         else
    76.         {
    77.             navAgent.SetDestination(targetPosition);
    78.         }
    79.     }
    80.  
    81.  
    82.  
    83.     private void OnTriggerEnter(Collider other)
    84.     {
    85.         if (other.tag == "Item")
    86.         {
    87.             //pick 0 or 1 or 2
    88.             int randomType = UnityEngine.Random.Range(0, 3);
    89.        
    90.             GameObject tmp = Instantiate(InventoryManager.Instance.itemObject);
    91.  
    92.             int randomItem;
    93.  
    94.             switch (randomType)
    95.             {
    96.                 case 0:
    97.                     tmp.AddComponent<ItemScript>();
    98.                     ItemScript newConsumable = tmp.GetComponent<ItemScript>();
    99.                     randomItem = UnityEngine.Random.Range(0, InventoryManager.Instance.ItemCountainer.Consumables.Count);
    100.                     newConsumable.Item = InventoryManager.Instance.ItemCountainer.Consumables[randomItem];
    101.                     inventory.AddItem(newConsumable);
    102.                     Destroy(tmp);
    103.                     break;
    104.                 case 1:
    105.                     tmp.AddComponent<ItemScript>();
    106.                     ItemScript newWeapon = tmp.GetComponent<ItemScript>();
    107.                     randomItem = UnityEngine.Random.Range(0, InventoryManager.Instance.ItemCountainer.Weapons.Count);
    108.                     newWeapon.Item = InventoryManager.Instance.ItemCountainer.Weapons[randomItem];
    109.                     inventory.AddItem(newWeapon);
    110.                     Destroy(tmp);
    111.                     break;
    112.                 case 2:
    113.                     tmp.AddComponent<ItemScript>();
    114.                     ItemScript newEquipment = tmp.GetComponent<ItemScript>();
    115.                     randomItem = UnityEngine.Random.Range(0, InventoryManager.Instance.ItemCountainer.Equipment.Count);
    116.                     newEquipment.Item = InventoryManager.Instance.ItemCountainer.Equipment[randomItem];
    117.                     inventory.AddItem(newEquipment);
    118.                     Destroy(tmp);
    119.                     break;
    120.             }
    121.         }
    122.  
    123.         if (other.tag == "Bank")
    124.         {
    125.             bank = other.GetComponent<BankScript>().bankInventory;
    126.         }
    127.     }
    128.  
    129.  
    130.  
    131.     private void OnTriggerExit(Collider other)
    132.     {
    133.         if (other.gameObject.tag == "Bank")
    134.         {
    135.             if(bank.IsOpen)
    136.             {
    137.                 bank.Open();
    138.             }
    139.             bank = null;
    140.         }
    141.     }
    142. }
    143.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6. using System;
    7.  
    8. public class Inventory : MonoBehaviour
    9. {
    10.     private RectTransform inventoryRect;
    11.  
    12.     private float inventoryWidth, inventoryHeight;
    13.  
    14.     public int slots;
    15.  
    16.     public int rows;
    17.  
    18.     public float slotPaddingLeft;
    19.  
    20.     public float slotPaddingTop;
    21.  
    22.     public float slotSize;
    23.  
    24.     private List<GameObject> allSlots;
    25.  
    26.     private float hoverYOffset;
    27.  
    28.     private CanvasGroup canvasGroup;
    29.  
    30.     private bool fadingIn;
    31.  
    32.     private bool fadingOut;
    33.  
    34.     public float fadeTime;
    35.  
    36.     public static bool mouseInside = false;
    37.  
    38.     private bool isOpen;
    39.  
    40.     public bool IsOpen
    41.     {
    42.         get{ return isOpen; }
    43.         set{ isOpen = value; }
    44.     }
    45.  
    46.  
    47.     private int emptySlots;
    48.  
    49.     public int EmptySlots
    50.     {
    51.         get{ return emptySlots; }
    52.         set{ emptySlots = value; }
    53.     }
    54.  
    55.  
    56.  
    57.     void Start ()
    58.     {
    59.         isOpen = false;
    60.  
    61.         canvasGroup = GetComponent<CanvasGroup>();
    62.  
    63.         CreateLayout();
    64.     }
    65.  
    66.  
    67.  
    68.     void Update ()
    69.     {
    70.         if (Input.GetMouseButtonUp (0))
    71.         {
    72.             //Removes the selected item from the inventory
    73.             if(!mouseInside && InventoryManager.Instance.From != null)
    74.             {
    75.                 InventoryManager.Instance.From.GetComponent<Image>().color = Color.white;
    76.  
    77.                 InventoryManager.Instance.From.ClearSlot();
    78.  
    79.                 Destroy (GameObject.Find ("Hover"));
    80.  
    81.                 InventoryManager.Instance.To = null;
    82.  
    83.                 InventoryManager.Instance.From = null;
    84.  
    85.                 emptySlots++;
    86.             }
    87.         }
    88.  
    89.         if (InventoryManager.Instance.HoverObject != null)
    90.         {
    91.             Vector2 position;
    92.  
    93.             RectTransformUtility.ScreenPointToLocalPointInRectangle(InventoryManager.Instance.canvas.transform as RectTransform, Input.mousePosition, InventoryManager.Instance.canvas.worldCamera, out position);
    94.  
    95.             position.Set(position.x, position.y - hoverYOffset);
    96.  
    97.             InventoryManager.Instance.HoverObject.transform.position = InventoryManager.Instance.canvas.transform.TransformPoint(position);
    98.         }
    99.  
    100.         if (canvasGroup.alpha == 0)
    101.         {
    102.             canvasGroup.interactable = false;
    103.             canvasGroup.blocksRaycasts = false;
    104.         }
    105.         else
    106.         {
    107.             canvasGroup.interactable = true;
    108.             canvasGroup.blocksRaycasts = true;
    109.         }
    110.     }
    111.  
    112.  
    113.  
    114.     public void OnDrag()
    115.     {
    116.         MoveInventory();
    117.     }
    118.  
    119.  
    120.  
    121.     public void PointerExit()
    122.     {
    123.         mouseInside = false;
    124.     }
    125.  
    126.  
    127.  
    128.     public void PointerEnter()
    129.     {
    130.         if (canvasGroup.alpha > 0)
    131.         {
    132.             mouseInside = true;
    133.         }
    134.     }
    135.  
    136.  
    137.  
    138.     public void Open()
    139.     {
    140.         if(canvasGroup.alpha > 0)
    141.         {
    142.             StartCoroutine("FadeOut");
    143.                
    144.             PutItemBack();
    145.  
    146.             HideToolTip();
    147.  
    148.             isOpen = false;
    149.         }
    150.         else
    151.         {
    152.             StartCoroutine("FadeIn");
    153.  
    154.             isOpen = true;
    155.         }
    156.     }
    157.  
    158.  
    159.  
    160.     public void ShowToolTip(GameObject slot)
    161.     {
    162.         Slot tmpslot = slot.GetComponent<Slot>();
    163.  
    164.         if (slot.GetComponentInParent<Inventory>().isOpen && !tmpslot.IsEmpty && InventoryManager.Instance.HoverObject == null)
    165.         {
    166.             InventoryManager.Instance.visualTextObject.text = tmpslot.CurrentItem.GetToolTip();
    167.  
    168.             InventoryManager.Instance.sizeTextObject.text = InventoryManager.Instance.visualTextObject.text;
    169.  
    170.  
    171.             InventoryManager.Instance.tooltipObject.SetActive(true);
    172.  
    173.  
    174.             float xPos = slot.transform.position.x + slotPaddingLeft;
    175.  
    176.             float yPos = slot.transform.position.y - slot.GetComponent<RectTransform>().sizeDelta.y - slotPaddingTop;
    177.  
    178.             InventoryManager.Instance.tooltipObject.transform.position = new Vector2(xPos, yPos);
    179.         }
    180.  
    181.     }
    182.  
    183.  
    184.  
    185.     public void HideToolTip()
    186.     {
    187.         InventoryManager.Instance.tooltipObject.SetActive(false);
    188.     }
    189.  
    190.  
    191.  
    192.     public void SaveInventory()
    193.     {
    194.         string content = string.Empty;
    195.  
    196.         for (int i = 0; i < allSlots.Count; i++)
    197.         {
    198.             Slot tmp = allSlots[i].GetComponent<Slot>();
    199.  
    200.             if(!tmp.IsEmpty)
    201.             {
    202.                 content += i + "-" + tmp.CurrentItem.Item.ItemName.ToString() + "-" + tmp.Items.Count.ToString() + ";";
    203.             }
    204.         }
    205.  
    206.         PlayerPrefs.SetString (gameObject.name + "content", content);
    207.  
    208.         PlayerPrefs.SetInt (gameObject.name + "slots", slots);
    209.  
    210.         PlayerPrefs.SetInt (gameObject.name + "rows", rows);
    211.  
    212.         PlayerPrefs.SetFloat (gameObject.name + "slotPaddingLeft", slotPaddingLeft);
    213.  
    214.         PlayerPrefs.SetFloat (gameObject.name + "slotPaddingTop", slotPaddingTop);
    215.  
    216.         PlayerPrefs.SetFloat (gameObject.name + "slotSize", slotSize);
    217.  
    218.         PlayerPrefs.SetFloat (gameObject.name + "xPos", inventoryRect.position.x);
    219.  
    220.         PlayerPrefs.SetFloat (gameObject.name + "yPos", inventoryRect.position.y);
    221.  
    222.         PlayerPrefs.Save();
    223.     }
    224.  
    225.  
    226.  
    227.     public void LoadInventory()
    228.     {
    229.         string content = PlayerPrefs.GetString (gameObject.name + "content");
    230.  
    231.         slots = PlayerPrefs.GetInt (gameObject.name + "slots");
    232.  
    233.         rows = PlayerPrefs.GetInt (gameObject.name + "rows");
    234.  
    235.         slotPaddingLeft = PlayerPrefs.GetFloat (gameObject.name + "slotPaddingLeft");
    236.  
    237.         slotPaddingTop = PlayerPrefs.GetFloat (gameObject.name + "slotPaddingTop");
    238.  
    239.         slotSize = PlayerPrefs.GetFloat (gameObject.name + "slotSize");
    240.  
    241.         inventoryRect.position = new Vector3 (PlayerPrefs.GetFloat (gameObject.name + "xPos"), PlayerPrefs.GetFloat (gameObject.name + "yPos"), inventoryRect.position.z);
    242.  
    243.         CreateLayout();
    244.  
    245.         string[] splitContent = content.Split (';');    //0-MANA-3
    246.  
    247.         for (int x = 0; x < splitContent.Length-1; x++)
    248.         {
    249.             string[] splitValues = splitContent[x].Split('-');
    250.  
    251.             int index = Int32.Parse(splitValues[0]);
    252.  
    253.             string itemName = splitValues[1];
    254.  
    255.             int amount = Int32.Parse(splitValues[2]);
    256.  
    257.             Item tmp = null;
    258.  
    259.             //replace the item in inventory has many times has we have before
    260.             for(int i = 0; i < amount; i++)
    261.             {
    262.                 GameObject loadedItem = Instantiate(InventoryManager.Instance.itemObject);
    263.  
    264.                 if(tmp == null)
    265.                 {
    266.                     tmp = InventoryManager.Instance.ItemCountainer.Consumables.Find(item => item.ItemName == itemName);
    267.                 }
    268.                 if(tmp == null)
    269.                 {
    270.                     tmp = InventoryManager.Instance.ItemCountainer.Equipment.Find(item => item.ItemName == itemName);
    271.  
    272.                 }
    273.                 if(tmp == null)
    274.                 {
    275.                     tmp = InventoryManager.Instance.ItemCountainer.Weapons.Find(item => item.ItemName == itemName);
    276.                    
    277.                 }
    278.  
    279.                 loadedItem.AddComponent<ItemScript>();
    280.  
    281.                 loadedItem.GetComponent<ItemScript>().Item = tmp;
    282.  
    283.                 allSlots[index].GetComponent<Slot>().AddItem(loadedItem.GetComponent<ItemScript>());
    284.  
    285.                 Destroy(loadedItem);
    286.             }
    287.         }
    288.     }
    289.  
    290.  
    291.  
    292.     private void CreateLayout()
    293.     {
    294.         if(allSlots != null)
    295.         {
    296.             foreach(GameObject go in allSlots)
    297.             {
    298.                 Destroy(go);
    299.             }
    300.         }
    301.  
    302.         allSlots = new List<GameObject>();
    303.  
    304.         hoverYOffset = slotSize * 0.01f;
    305.  
    306.         emptySlots = slots;
    307.  
    308.         inventoryWidth = (slots/rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    309.         inventoryHeight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
    310.  
    311.         inventoryRect = GetComponent<RectTransform>();
    312.  
    313.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
    314.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHeight);
    315.    
    316.         int columns = slots / rows;
    317.  
    318.         for (int y = 0; y < rows; y++)
    319.         {
    320.             for(int x = 0; x < columns; x++)
    321.             {
    322.                 GameObject newSlot = (GameObject)Instantiate(InventoryManager.Instance.slotPrefab);
    323.  
    324.                 RectTransform slotRect = newSlot.GetComponent<RectTransform>();
    325.  
    326.                 newSlot.name = "Slot";
    327.  
    328.                 //set slot as parent of inventory, who is parent from canvas that draw sprites
    329.                 newSlot.transform.SetParent(this.transform.parent);
    330.  
    331.                 slotRect.localPosition = inventoryRect.localPosition + new Vector3(slotPaddingLeft * (x + 1) + (slotSize * x), -slotPaddingTop * (y + 1) - (slotSize * y));
    332.  
    333.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize * InventoryManager.Instance.canvas.scaleFactor);  
    334.  
    335.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize * InventoryManager.Instance.canvas.scaleFactor);      
    336.  
    337.                 newSlot.transform.SetParent(this.transform);
    338.  
    339.                 allSlots.Add (newSlot);
    340.             }
    341.         }
    342.     }
    343.  
    344.  
    345.  
    346.     public bool AddItem(ItemScript item)
    347.     {
    348.         if (item.Item.MaxSize == 1)
    349.         {
    350.             PlaceEmpty(item);
    351.             return true;
    352.         }
    353.         else
    354.         {
    355.             foreach (GameObject slot in allSlots)
    356.             {
    357.                 Slot tmp = slot.GetComponent<Slot>();
    358.  
    359.                 if(!tmp.IsEmpty)
    360.                 {
    361.                     if(tmp.CurrentItem.Item.ItemName == item.Item.ItemName && tmp.IsAvailable)
    362.                     {
    363.                         tmp.AddItem(item);
    364.                         return true;
    365.                     }
    366.                 }
    367.             }
    368.             if(emptySlots > 0)
    369.             {
    370.                 PlaceEmpty(item);
    371.             }
    372.         }
    373.  
    374.         return false;
    375.     }
    376.  
    377.  
    378.  
    379.     private void MoveInventory()
    380.     {
    381.         Vector2 mousePos;
    382.  
    383.         RectTransformUtility.ScreenPointToLocalPointInRectangle (InventoryManager.Instance.canvas.transform as RectTransform, new Vector3 (Input.mousePosition.x - (inventoryRect.sizeDelta.x / 2 * InventoryManager.Instance.canvas.scaleFactor), Input.mousePosition.y + (inventoryRect.sizeDelta.y / 2 * InventoryManager.Instance.canvas.scaleFactor)), InventoryManager.Instance.canvas.worldCamera, out mousePos);
    384.  
    385.         transform.position = InventoryManager.Instance.canvas.transform.TransformPoint (mousePos);
    386.     }
    387.  
    388.  
    389.  
    390.     private bool PlaceEmpty(ItemScript item)
    391.     {
    392.         if (emptySlots > 0)
    393.         {
    394.             foreach (GameObject slot in allSlots)
    395.             {
    396.                 Slot tmp = slot.GetComponent<Slot>();
    397.  
    398.                 if(tmp.IsEmpty)
    399.                 {
    400.                     tmp.AddItem(item);
    401.                     emptySlots --;
    402.                     return true;
    403.                 }
    404.             }
    405.         }
    406.  
    407.         return false;
    408.     }
    409.  
    410.  
    411.  
    412.     public void MoveItem(GameObject clicked)
    413.     {
    414.         if (clicked.transform.parent.GetComponent<CanvasGroup>().alpha > 0)
    415.         {
    416.             if (InventoryManager.Instance.From == null && clicked.transform.parent.GetComponent<Inventory>().isOpen)
    417.             {
    418.                 if (!clicked.GetComponent<Slot>().IsEmpty)
    419.                 {
    420.                     InventoryManager.Instance.From = clicked.GetComponent<Slot>();
    421.                    
    422.                     InventoryManager.Instance.From.GetComponent<Image>().color = Color.gray;
    423.                    
    424.                    
    425.                     InventoryManager.Instance.HoverObject = (GameObject)Instantiate(InventoryManager.Instance.iconPrefab);
    426.                    
    427.                     InventoryManager.Instance.HoverObject.GetComponent<Image>().sprite = clicked.GetComponent<Image>().sprite;
    428.                    
    429.                     InventoryManager.Instance.HoverObject.name = "Hover";
    430.                    
    431.                    
    432.                     RectTransform hoverTransform = InventoryManager.Instance.HoverObject.GetComponent<RectTransform>();
    433.                    
    434.                     RectTransform clickedTransform = clicked.GetComponent<RectTransform>();
    435.                    
    436.                    
    437.                     hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
    438.                    
    439.                     hoverTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);
    440.                    
    441.                    
    442.                     InventoryManager.Instance.HoverObject.transform.SetParent(GameObject.Find ("Canvas").transform, true);
    443.                    
    444.                     InventoryManager.Instance.HoverObject.transform.localScale = InventoryManager.Instance.From.gameObject.transform.localScale;
    445.                 }
    446.             }
    447.             else if (InventoryManager.Instance.To == null)
    448.             {
    449.                 InventoryManager.Instance.To = clicked.GetComponent<Slot>();
    450.  
    451.                 Destroy(GameObject.Find ("Hover"));
    452.             }
    453.             if (InventoryManager.Instance.To != null && InventoryManager.Instance.From != null)
    454.             {
    455.                 Stack<ItemScript> tmpTo = new Stack<ItemScript>(InventoryManager.Instance.To.Items);
    456.                
    457.                 InventoryManager.Instance.To.AddItems(InventoryManager.Instance.From.Items);
    458.                
    459.                 if(tmpTo.Count == 0)
    460.                 {
    461.                     InventoryManager.Instance.From.ClearSlot();
    462.                 }
    463.                 else
    464.                 {
    465.                     InventoryManager.Instance.From.AddItems(tmpTo);
    466.                 }
    467.                
    468.                 InventoryManager.Instance.From.GetComponent<Image>().color = Color.white;
    469.                
    470.                 //reset "to" and "from"
    471.                 InventoryManager.Instance.To = null;
    472.                
    473.                 InventoryManager.Instance.From = null;
    474.                
    475.                 Destroy(GameObject.Find ("Hover"));
    476.             }
    477.         }
    478.     }
    479.  
    480.  
    481.  
    482.     private void PutItemBack()
    483.     {
    484.         if (InventoryManager.Instance.From != null)
    485.         {
    486.             Destroy(GameObject.Find("Hover"));
    487.  
    488.             InventoryManager.Instance.From.GetComponent<Image>().color = Color.white;
    489.  
    490.             InventoryManager.Instance.From = null;
    491.         }
    492.     }
    493.  
    494.  
    495.  
    496.     private IEnumerator FadeOut()
    497.     {
    498.         if (!fadingOut)
    499.         {
    500.             fadingOut = true;
    501.  
    502.             fadingIn = false;
    503.  
    504.             StopCoroutine("FadeIn");
    505.  
    506.  
    507.             float startAlpha = canvasGroup.alpha;
    508.  
    509.             float rate = 1.0f / fadeTime;
    510.  
    511.             float progress = 0.0f;
    512.  
    513.  
    514.             while(progress < 1.0)
    515.             {
    516.                 canvasGroup.alpha = Mathf.Lerp(startAlpha, 0, progress);
    517.  
    518.                 progress += rate * Time.deltaTime;
    519.  
    520.                 yield return null;
    521.             }
    522.  
    523.             canvasGroup.alpha = 0;
    524.  
    525.             fadingOut = false;
    526.         }
    527.     }
    528.  
    529.  
    530.  
    531.     private IEnumerator FadeIn()
    532.     {
    533.         if (!fadingIn)
    534.         {
    535.             fadingOut = false;
    536.            
    537.             fadingIn = true;
    538.            
    539.             StopCoroutine("FadeOut");
    540.            
    541.            
    542.             float startAlpha = canvasGroup.alpha;
    543.            
    544.             float rate = 1.0f / fadeTime;
    545.            
    546.             float progress = 0.0f;
    547.            
    548.            
    549.             while(progress < 1.0)
    550.             {
    551.                 canvasGroup.alpha = Mathf.Lerp(startAlpha, 1, progress);
    552.                
    553.                 progress += rate * Time.deltaTime;
    554.                
    555.                 yield return null;
    556.             }
    557.            
    558.             canvasGroup.alpha = 1;
    559.            
    560.             fadingIn = false;
    561.         }
    562.     }
    563. }
    564.  
     
  7. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    For one you have too many dependencies in your Click to Move script.

    What I would do, is set a boolean property on the Inventory callen IsOpen and set that to true when the inventory is, well, open.

    Then just check if it is open, and if it is, just disable movement altogether.
    Code (CSharp):
    1.  
    2. void Update ()
    3. {
    4.     if(!Inventory.IsOpen)
    5.     {
    6.         if (Input.GetMouseButton (LEFT_MOUSE_BUTTON))
    7.             SetTargetPosition();
    8.  
    9.         MovePlayer();
    10.     }
    11.  
     
  8. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The question is i already have a isOpen bool in inventory and is set to true when inventory is open (alpha>0) and false when is close (alpha=0) but when i use it in clicktomove script i get an error saying

    Static member Inventory.IsOpen cannot be accessed with an instance reference, qualify it with a type name instead..

    I change the private bool i create in inventory to public static to access it in clicktomove but i get the error...
     
  9. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Don't make it static. In your ClickToMove script you already have a reference to your inventory (that you set from the inspector I guess?)
    Code (CSharp):
    1. public Inventory inventory;
    Use that for the check
     
  10. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Dumb!! It works.. But when its open i cant move at all... Could be good or not... Cuz for game concerns we probably will need open inventory during combart (to take potions for example) and in that way its quite "impossible" . Not with the bank.. cuz the bank are made to be in town but with inventory...

    The ideal is just stop player movement when i hv a item on my mouse "Hover" other way we can have inventory open and move anyway...

    But at least it works. Ty m8
     
  11. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Then just set a bool whenever you pick an item from your inventory :) Same logic applies for constraining movement then :)
     
  12. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Well the problem is that the pick item is inside the inventory script.. so for that i hv to access the clicktomove script and then and in clicktomove set a bool ismoving and when i pick set is moving to false is that? A bit bad explained but i think is that :)
     
  13. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Now that im trying this i figure the problem... Wts going on is that when i click in a icon inside the inventory the player dont move has i expected, but wts going on ithe that in the moment that i leave the inventory window it assumes the click i made over the icon 1stly and thats why it moves to that position when i leave the inventory image...

    Its there a way to fix this diferente then a bool, cuz this seems to be a diferente problem....

    ty
     
  14. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    As the Inventory is a UI element, try any of these to see if they can help:
     
  15. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    1st option works flawless xD Ty m8 really appreciate all the time u lose with me ;)
     
  16. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Glad you found a fix already.

    I'm not sure what you guys used as a fix, but...

    I think you might want to check out GetMouseButtonDown vs. GetMouseButton still though for picking up objects. I'm not sure if that was covered already. That would allow you to pick the item up and keep it on the cursor without moving the character, in theory, if you set it up.
     
  17. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The fix was use eventsystem in my clicktomove script, like the 1st example of the video that Timelog posted in the thread :)