Advertisement
evelynshilosky

InteractionSystem - Part 6.2.1.1

Apr 8th, 2025
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.57 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class InteractionSystem : MonoBehaviour
  5. {
  6.     public Transform leftObjectHolder;
  7.     public Transform rightObjectHolder;
  8.     public LayerMask pickupLayer;
  9.     public float placementDistance = 5f;
  10.     public LayerMask placementLayerMask;
  11.     public float previewUpdateInterval = 0.1f;
  12.     public float placementHeightOffset = 0.1f;
  13.     public float rotationIncrement = 45f;
  14.     public Transform backpackWearPosition;
  15.  
  16.     private InventorySystem inventorySystem;
  17.     private PlayerMovement playerMovement;
  18.     private UIManager uiManager;
  19.     private bool isPrecisionDrop = false;
  20.     private Dictionary<GameObject, int> originalLayers = new Dictionary<GameObject, int>();
  21.     private float lastPreviewUpdateTime;
  22.     private bool isRotating = false;
  23.     private Vector3 currentRotation;
  24.     private List<GameObject> previewObjects = new List<GameObject>();
  25.     public Item currentBackpackItem;
  26.  
  27.     private void Start()
  28.     {
  29.         inventorySystem = InventorySystem.Instance;
  30.         playerMovement = GetComponent<PlayerMovement>();
  31.         uiManager = UIManager.Instance;
  32.         UpdateItemVisibility();
  33.     }
  34.  
  35.     private void Update()
  36.     {
  37.         if (isPrecisionDrop)
  38.         {
  39.             UpdatePlacementPreview();
  40.             HandleRotation();
  41.         }
  42.         else
  43.         {
  44.             ClearPreviewObjects();
  45.         }
  46.  
  47.         if (Input.GetKeyDown(KeyCode.E) && currentBackpackItem != null && !inventorySystem.IsBackpackEquipped)
  48.         {
  49.             WearBackpack();
  50.         }
  51.     }
  52.  
  53.     public void TryPickUpItem(InteractableObject interactable)
  54.     {
  55.         Item item = interactable.item;
  56.  
  57.         if (item.isBackpack && !inventorySystem.IsBackpackEquipped)
  58.         {
  59.             PickUpBackpack(item);
  60.         }
  61.         else
  62.         {
  63.             bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
  64.             EquipItem(item, isLeftHand, item.isTwoHanded);
  65.             playerMovement.UpdateCarryingAnimations();
  66.         }
  67.     }
  68.  
  69.     public void InteractWithStorage(Item storageItem, bool isRightClick)
  70.     {
  71.         if (isRightClick)
  72.         {
  73.             UIManager.Instance.ShowInventoryPrompt(storageItem); // Show prompt for opening storage
  74.         }
  75.     }
  76.  
  77.     private void PickUpBackpack(Item backpackItem)
  78.     {
  79.         bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
  80.         EquipItem(backpackItem, isLeftHand, false);
  81.         currentBackpackItem = backpackItem;
  82.         uiManager.ShowBackpackPrompt(backpackItem);
  83.     }
  84.  
  85.     public void HandleBackpackPrompt(bool accepted)
  86.     {
  87.         if (accepted)
  88.         {
  89.             WearBackpack();
  90.         }
  91.         else
  92.         {
  93.             uiManager.UpdateBackpackPrompt();
  94.             // Don't close the prompt here
  95.         }
  96.     }
  97.  
  98.     public void WearBackpack()
  99.     {
  100.         if (currentBackpackItem != null)
  101.         {
  102.             bool wasLeftHand = inventorySystem.leftHandItem == currentBackpackItem;
  103.             inventorySystem.UnequipItem(wasLeftHand);
  104.  
  105.             currentBackpackItem.transform.SetParent(backpackWearPosition);
  106.             currentBackpackItem.transform.localPosition = Vector3.zero;
  107.             currentBackpackItem.transform.localRotation = Quaternion.identity;
  108.  
  109.             Destroy(currentBackpackItem.GetComponent<Rigidbody>());
  110.             MeshCollider meshCollider = currentBackpackItem.GetComponent<MeshCollider>();
  111.             if (meshCollider != null) meshCollider.enabled = false;
  112.  
  113.             inventorySystem.EquipBackpack(currentBackpackItem);
  114.             currentBackpackItem = null;
  115.  
  116.             UIManager.Instance.ClosePrompt();
  117.             playerMovement.UpdateCarryingAnimations();
  118.         }
  119.     }
  120.  
  121.     public void UnequipBackpack()
  122.     {
  123.         if (inventorySystem.IsBackpackEquipped)
  124.         {
  125.             Item backpack = inventorySystem.backpack;
  126.             inventorySystem.UnequipBackpack();
  127.  
  128.             MeshCollider meshCollider = backpack.GetComponent<MeshCollider>();
  129.             if (meshCollider != null) meshCollider.enabled = true;
  130.  
  131.             if (backpack.GetComponent<Rigidbody>() == null)
  132.             {
  133.                 backpack.gameObject.AddComponent<Rigidbody>();
  134.             }
  135.  
  136.             backpack.transform.SetParent(null);
  137.             backpack.transform.position = transform.position + transform.forward * 1.5f;
  138.  
  139.             playerMovement.UpdateCarryingAnimations();
  140.         }
  141.     }
  142.  
  143.     private void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
  144.     {
  145.         inventorySystem.EquipItem(item, isLeftHand, isTwoHanded);
  146.         UpdateItemPosition(item.gameObject, isLeftHand);
  147.         ChangeItemLayer(item.gameObject, pickupLayer);
  148.         playerMovement.UpdateCarryingAnimations();
  149.     }
  150.  
  151.     public void UpdateItemPosition(GameObject itemObject, bool isLeftHand)
  152.     {
  153.         Transform objectHolder = isLeftHand ? leftObjectHolder : rightObjectHolder;
  154.         Item item = itemObject.GetComponent<Item>();
  155.         Vector3 positionOffset = isLeftHand ? item.leftPositionOffset : item.rightPositionOffset;
  156.         Vector3 rotationOffset = isLeftHand ? item.leftRotationOffset : item.rightRotationOffset;
  157.  
  158.         itemObject.transform.SetParent(objectHolder);
  159.         itemObject.transform.localPosition = positionOffset;
  160.         itemObject.transform.localRotation = Quaternion.Euler(rotationOffset);
  161.     }
  162.  
  163.     private void UpdateItemVisibility()
  164.     {
  165.         if (inventorySystem.leftHandItem != null)
  166.         {
  167.             inventorySystem.leftHandItem.gameObject.SetActive(!isPrecisionDrop);
  168.         }
  169.         if (inventorySystem.rightHandItem != null)
  170.         {
  171.             inventorySystem.rightHandItem.gameObject.SetActive(!isPrecisionDrop);
  172.         }
  173.     }
  174.  
  175.     private void UpdatePlacementPreview()
  176.     {
  177.         if (Time.time - lastPreviewUpdateTime < previewUpdateInterval) return;
  178.  
  179.         lastPreviewUpdateTime = Time.time;
  180.  
  181.         RaycastHit hit;
  182.         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, placementDistance, placementLayerMask))
  183.         {
  184.             List<Item> itemsToPlace = new List<Item>();
  185.             if (inventorySystem.leftHandItem != null) itemsToPlace.Add(inventorySystem.leftHandItem);
  186.             if (inventorySystem.rightHandItem != null) itemsToPlace.Add(inventorySystem.rightHandItem);
  187.  
  188.             ClearPreviewObjects();
  189.  
  190.             Vector3 previewPosition = hit.point + hit.normal * (0.05f + placementHeightOffset);
  191.             Quaternion previewRotation = !isRotating ? Quaternion.LookRotation(hit.normal, Vector3.up) : Quaternion.Euler(currentRotation);
  192.  
  193.             for (int i = 0; i < itemsToPlace.Count; i++)
  194.             {
  195.                 GameObject previewObj = Instantiate(itemsToPlace[i].gameObject);
  196.                 Destroy(previewObj.GetComponent<Rigidbody>());
  197.                 Destroy(previewObj.GetComponent<Collider>());
  198.                 previewObj.AddComponent<Outline>();
  199.  
  200.                 previewObj.transform.position = previewPosition + Vector3.right * (i * 0.5f);
  201.                 previewObj.transform.rotation = previewRotation;
  202.  
  203.                 Outline outline = previewObj.GetComponent<Outline>();
  204.                 outline.OutlineMode = Outline.Mode.OutlineAll;
  205.                 outline.OutlineColor = Color.green;
  206.                 outline.OutlineWidth = 5f;
  207.  
  208.                 previewObjects.Add(previewObj);
  209.                 previewObj.SetActive(true);
  210.             }
  211.  
  212.             bool canPlace = !Physics.Raycast(previewPosition, Vector3.down, placementHeightOffset + 0.05f, placementLayerMask);
  213.             foreach (var obj in previewObjects)
  214.             {
  215.                 obj.GetComponent<Outline>().OutlineColor = canPlace ? Color.green : Color.red;
  216.             }
  217.         }
  218.         else
  219.         {
  220.             ClearPreviewObjects();
  221.         }
  222.     }
  223.  
  224.     private void HandleRotation()
  225.     {
  226.         if (Input.GetKeyDown(KeyCode.R))
  227.         {
  228.             isRotating = !isRotating;
  229.             if (isRotating)
  230.             {
  231.                 currentRotation = previewObjects[0].transform.rotation.eulerAngles;
  232.                 uiManager.ShowInventoryPrompt(null);
  233.                 uiManager.UpdatePromptText("Rotation mode enabled. Use Q/E to rotate horizontally, Z/C to rotate vertically.");
  234.                 uiManager.ShowOkayButton();
  235.             }
  236.             else
  237.             {
  238.                 uiManager.ShowInventoryPrompt(null);
  239.                 uiManager.UpdatePromptText("Rotation mode disabled.");
  240.                 uiManager.ShowOkayButton();
  241.             }
  242.         }
  243.  
  244.         if (isRotating && previewObjects.Count > 0)
  245.         {
  246.             if (Input.GetKeyDown(KeyCode.Q))
  247.             {
  248.                 currentRotation.y -= rotationIncrement;
  249.             }
  250.             else if (Input.GetKeyDown(KeyCode.E))
  251.             {
  252.                 currentRotation.y += rotationIncrement;
  253.             }
  254.             else if (Input.GetKeyDown(KeyCode.Z))
  255.             {
  256.                 currentRotation.x -= rotationIncrement;
  257.             }
  258.             else if (Input.GetKeyDown(KeyCode.C))
  259.             {
  260.                 currentRotation.x += rotationIncrement;
  261.             }
  262.  
  263.             foreach (var obj in previewObjects)
  264.             {
  265.                 obj.transform.rotation = Quaternion.Euler(currentRotation);
  266.             }
  267.         }
  268.     }
  269.  
  270.     public void DropItem(bool isLeftHand)
  271.     {
  272.         Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
  273.         if (itemToDrop != null)
  274.         {
  275.             GameObject itemObject = itemToDrop.gameObject;
  276.             itemObject.transform.SetParent(null);
  277.  
  278.             Vector3 dropPosition;
  279.             if (isPrecisionDrop && previewObjects.Count > 0)
  280.             {
  281.                 int index = isLeftHand ? 0 : (previewObjects.Count > 1 ? 1 : 0);
  282.                 dropPosition = previewObjects[index].transform.position;
  283.                 itemObject.transform.position = dropPosition;
  284.                 itemObject.transform.rotation = previewObjects[index].transform.rotation;
  285.             }
  286.             else
  287.             {
  288.                 dropPosition = transform.position + transform.forward * 1f + transform.right * Random.Range(-0.5f, 0.5f);
  289.                 itemObject.transform.position = dropPosition;
  290.                 itemObject.transform.rotation = Random.rotation;
  291.             }
  292.  
  293.             itemObject.SetActive(true);
  294.  
  295.             Rigidbody rb = itemObject.GetComponent<Rigidbody>();
  296.             if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
  297.             rb.isKinematic = false;
  298.             rb.useGravity = true;
  299.             rb.AddForce(Vector3.down * 2f, ForceMode.Impulse);
  300.  
  301.             inventorySystem.UnequipItem(isLeftHand);
  302.             RestoreOriginalLayer(itemObject);
  303.  
  304.             InteractableObject interactable = itemObject.GetComponent<InteractableObject>();
  305.             if (interactable != null)
  306.             {
  307.                 interactable.enabled = true;
  308.             }
  309.  
  310.             playerMovement.UpdateCarryingAnimations();
  311.         }
  312.  
  313.         ClearPreviewObjects();
  314.  
  315.         if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
  316.         {
  317.             isPrecisionDrop = false;
  318.             isRotating = false;
  319.         }
  320.  
  321.         UpdateItemVisibility();
  322.     }
  323.  
  324.     public void DropBothItems()
  325.     {
  326.         DropItem(true);
  327.         DropItem(false);
  328.     }
  329.  
  330.     public void ChangeItemLayer(GameObject itemObject, LayerMask newLayer)
  331.     {
  332.         if (!originalLayers.ContainsKey(itemObject))
  333.         {
  334.             originalLayers[itemObject] = itemObject.layer;
  335.         }
  336.         itemObject.layer = (int)Mathf.Log(newLayer.value, 2);
  337.     }
  338.  
  339.     private void RestoreOriginalLayer(GameObject itemObject)
  340.     {
  341.         if (originalLayers.ContainsKey(itemObject))
  342.         {
  343.             itemObject.layer = originalLayers[itemObject];
  344.             originalLayers.Remove(itemObject);
  345.         }
  346.     }
  347.  
  348.     public void TogglePrecisionDrop()
  349.     {
  350.         isPrecisionDrop = !isPrecisionDrop;
  351.         UpdateItemVisibility();
  352.         uiManager.ShowInventoryPrompt(null);
  353.         uiManager.UpdatePromptText(isPrecisionDrop ? "Precision drop enabled" : "Precision drop disabled");
  354.         uiManager.ShowOkayButton();
  355.     }
  356.  
  357.     private void ClearPreviewObjects()
  358.     {
  359.         foreach (var obj in previewObjects)
  360.         {
  361.             Destroy(obj);
  362.         }
  363.         previewObjects.Clear();
  364.     }
  365.  
  366.     public bool IsPrecisionDropEnabled()
  367.     {
  368.         return isPrecisionDrop;
  369.     }
  370. }
  371.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement