WorldInteraction.cs detects interactions and then decides where to send the object, to inventory or to worlditem script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System; // http://trinary.tech/category/mec/ more efficient coroutines documentation using MEC; // Eric's interaction script will handle raycasting for world objects, Inventory is kind of the UI counterpart. So inventory to itemsinventory as interaction to itemsinworld? // this script will handle dragging in 3d, right click context menu, updating information in inventory. // add zoom to mouse wheel and depth of field surroundings. Only zoom on interactable objects, when mouseoff then return zoom to normal after a second. public class WorldInteraction : MonoBehaviour { #region variables to check if item is pickable public Inventory inventoryPanel; private GameObject item; private GameObject inventoryItem; //InventoryItem [HideInInspector] public WorldItem worldItem; private bool itemDragCheckRunning = false; // private GameObject inventoryCounterpart; #endregion [HideInInspector] public RaycastHit raycastHit; private Ray ray; //[HideInInspector] public Vector3 itemSpawnPoint; [HideInInspector] public bool hitDetected; #region watch for mouse drag private CoroutineHandle pauseHandle; private Vector3 mousePositionSaved;// = new Vector3(0, 0, 0); #endregion private void Awake() { // drag coroutine preparation mousePositionSaved = Input.mousePosition; } private void Start() { } private void Update() { // while mouse is over inventory prevent raycasting, set up toggle so you can hold f or click to toggle if (Input.GetKey(KeyCode.F)) { // Debug.Log(inventoryPanel.canvasRaycastRunning); if (!inventoryPanel.canvasRaycastRunning) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray.origin, ray.direction, out raycastHit, 2)) // not Mathf.Infinity please, about arms length or 6 ft to be able to grab at feet? 1.8288 { // Debug.Log("Did Hit: " + hit.collider + "\n" + hit.collider + " is tagged: " + hit.collider.tag); hitDetected = true; if (!itemDragCheckRunning) { // in fact just wrap everything that a raycast can allow to happen nicely in drag item check item = raycastHit.transform.gameObject; // if this isn't also in the dragcheckrunning then if the raycast hits something else it will be deleted, lol worldItem = item.GetComponent<WorldItem>(); // this causes null error on line 112 if mouse drag causes raycast to hit another item before swapping to card drag if (worldItem != null && worldItem.pickable) { // Debug.Log("Item is pick up able " + itemsInWorld.pickable); if (Input.GetMouseButtonDown(0)) { //Debug.Log("mouse has been clicked on a pickable item and mouse position is" + Input.mousePosition); mousePositionSaved = Input.mousePosition; Timing.RunCoroutine(_ItemDragCheck()); } } // get enum type to determine how to use Use(), overload, send enum type, send something else, idk yet // or maybe different methods in world item for each enum type UseDoor, UseElevator, UseCassettePlayer etc if (worldItem != null && worldItem.usable) { Debug.Log("world item is usable, it's probably a door"); if (Input.GetMouseButtonDown(0)) { // run world item use and send item type to set use conditions to for proper object. Door, keypad worldItem.Use(); // for now all doing is setting door open closem use } } } } if (!Physics.Raycast(ray.origin, ray.direction, out raycastHit, 2)) { hitDetected = false; } } } } IEnumerator<float> _ItemDragCheck() { //Debug.Log("Item drag check is trying to start"); itemDragCheckRunning = true; while (!Input.GetMouseButtonUp(0)) { //Debug.Log("Item drag check started"); if (Input.mousePosition != mousePositionSaved) { //Debug.Log("mouse has been dragged on pickable item"); //Debug.Log(itemsInWorld.inventoryCounterpart); // pull card from deck, set to mouse position, destroy object inventoryItem = worldItem.inventoryCounterpart.gameObject; // null exception comes on this line when the mouse raycast is moved away from the object too fast raycasting onto the wrong surface inventoryItem.GetComponent<InventoryItem>().StartItemDrag(inventoryItem); Destroy(item); itemDragCheckRunning = false; break; } yield return 0f; } } |