Advertisement
evelynshilosky

InteractionSystem - Part 5

Feb 25th, 2025
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.25 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.  
  15.     private InventorySystem inventorySystem;
  16.     private PlayerMovement playerMovement;
  17.     private bool isPrecisionDrop = false;
  18.     private Dictionary<GameObject, int> originalLayers = new Dictionary<GameObject, int>();
  19.     private float lastPreviewUpdateTime;
  20.     private bool isRotating = false;
  21.     private Vector3 currentRotation;
  22.     private List<GameObject> previewObjects = new List<GameObject>();
  23.  
  24.     private void Start()
  25.     {
  26.         inventorySystem = InventorySystem.Instance;
  27.         playerMovement = GetComponent<PlayerMovement>();
  28.         UpdateItemVisibility();
  29.     }
  30.  
  31.     private void Update()
  32.     {
  33.         if (isPrecisionDrop)
  34.         {
  35.             UpdatePlacementPreview();
  36.             HandleRotation();
  37.         }
  38.         else
  39.         {
  40.             ClearPreviewObjects();
  41.         }
  42.     }
  43.  
  44.     public void TryPickUpItem(InteractableObject interactable)
  45.     {
  46.         Item item = interactable.item;
  47.         bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
  48.  
  49.         if (item.isTwoHanded)
  50.         {
  51.             if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
  52.             {
  53.                 EquipItem(item, true, true);
  54.             }
  55.             else
  56.             {
  57.                 Debug.Log("Cannot pick up two-handed item. Hands are not empty.");
  58.             }
  59.         }
  60.         else if (inventorySystem.rightHandItem == null || isLeftHand)
  61.         {
  62.             EquipItem(item, isLeftHand, false);
  63.         }
  64.         else
  65.         {
  66.             Debug.Log("Cannot pick up item. Both hands are full.");
  67.         }
  68.     }
  69.  
  70.     private void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
  71.     {
  72.         inventorySystem.EquipItem(item, isLeftHand, isTwoHanded);
  73.         UpdateItemPosition(item.gameObject, isLeftHand);
  74.         ChangeItemLayer(item.gameObject, pickupLayer);
  75.         playerMovement.UpdateCarryingAnimations();
  76.     }
  77.  
  78.     public void UpdateItemPosition(GameObject itemObject, bool isLeftHand)
  79.     {
  80.         Transform objectHolder = isLeftHand ? leftObjectHolder : rightObjectHolder;
  81.         Item item = itemObject.GetComponent<Item>();
  82.         Vector3 positionOffset = isLeftHand ? item.leftPositionOffset : item.rightPositionOffset;
  83.         Vector3 rotationOffset = isLeftHand ? item.leftRotationOffset : item.rightRotationOffset;
  84.  
  85.         itemObject.transform.SetParent(objectHolder);
  86.         itemObject.transform.localPosition = positionOffset;
  87.         itemObject.transform.localRotation = Quaternion.Euler(rotationOffset);
  88.     }
  89.  
  90.     private void UpdateItemVisibility()
  91.     {
  92.         if (inventorySystem.leftHandItem != null)
  93.         {
  94.             inventorySystem.leftHandItem.gameObject.SetActive(!isPrecisionDrop);
  95.         }
  96.         if (inventorySystem.rightHandItem != null)
  97.         {
  98.             inventorySystem.rightHandItem.gameObject.SetActive(!isPrecisionDrop);
  99.         }
  100.     }
  101.  
  102.     private void UpdatePlacementPreview()
  103.     {
  104.         if (Time.time - lastPreviewUpdateTime < previewUpdateInterval) return;
  105.  
  106.         lastPreviewUpdateTime = Time.time;
  107.  
  108.         RaycastHit hit;
  109.         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, placementDistance, placementLayerMask))
  110.         {
  111.             List<Item> itemsToPlace = new List<Item>();
  112.             if (inventorySystem.leftHandItem != null) itemsToPlace.Add(inventorySystem.leftHandItem);
  113.             if (inventorySystem.rightHandItem != null) itemsToPlace.Add(inventorySystem.rightHandItem);
  114.  
  115.             ClearPreviewObjects();
  116.  
  117.             Vector3 previewPosition = hit.point + hit.normal * (0.05f + placementHeightOffset);
  118.             Quaternion previewRotation = !isRotating ? Quaternion.LookRotation(hit.normal, Vector3.up) : Quaternion.Euler(currentRotation);
  119.  
  120.             for (int i = 0; i < itemsToPlace.Count; i++)
  121.             {
  122.                 GameObject previewObj = Instantiate(itemsToPlace[i].gameObject);
  123.                 Destroy(previewObj.GetComponent<Rigidbody>());
  124.                 Destroy(previewObj.GetComponent<Collider>());
  125.                 previewObj.AddComponent<Outline>();
  126.  
  127.                 previewObj.transform.position = previewPosition + Vector3.right * (i * 0.5f);
  128.                 previewObj.transform.rotation = previewRotation;
  129.  
  130.                 Outline outline = previewObj.GetComponent<Outline>();
  131.                 outline.OutlineMode = Outline.Mode.OutlineAll;
  132.                 outline.OutlineColor = Color.blue;
  133.                 outline.OutlineWidth = 5f;
  134.  
  135.                 previewObjects.Add(previewObj);
  136.                 previewObj.SetActive(true); // Ensure the preview object is active
  137.             }
  138.  
  139.             bool canPlace = !Physics.Raycast(previewPosition, Vector3.down, placementHeightOffset + 0.05f, placementLayerMask);
  140.             foreach (var obj in previewObjects)
  141.             {
  142.                 obj.GetComponent<Outline>().OutlineColor = canPlace ? Color.blue : Color.blue;
  143.             }
  144.         }
  145.         else
  146.         {
  147.             ClearPreviewObjects();
  148.         }
  149.     }
  150.  
  151.     private void HandleRotation()
  152.     {
  153.         if (Input.GetKeyDown(KeyCode.R))
  154.         {
  155.             isRotating = !isRotating;
  156.             if (isRotating)
  157.             {
  158.                 currentRotation = previewObjects[0].transform.rotation.eulerAngles;
  159.                 UIManager.Instance.ShowInventoryPrompt(null);
  160.                 UIManager.Instance.UpdatePromptText("Rotation mode enabled. Use Q/E to rotate horizontally, Z/C to rotate vertically.");
  161.                 UIManager.Instance.ShowOkayButton();
  162.             }
  163.             else
  164.             {
  165.                 UIManager.Instance.ShowInventoryPrompt(null);
  166.                 UIManager.Instance.UpdatePromptText("Rotation mode disabled.");
  167.                 UIManager.Instance.ShowOkayButton();
  168.             }
  169.         }
  170.  
  171.         if (isRotating && previewObjects.Count > 0)
  172.         {
  173.             if (Input.GetKeyDown(KeyCode.Q))
  174.             {
  175.                 currentRotation.y -= rotationIncrement;
  176.             }
  177.             else if (Input.GetKeyDown(KeyCode.E))
  178.             {
  179.                 currentRotation.y += rotationIncrement;
  180.             }
  181.             else if (Input.GetKeyDown(KeyCode.Z))
  182.             {
  183.                 currentRotation.x -= rotationIncrement;
  184.             }
  185.             else if (Input.GetKeyDown(KeyCode.C))
  186.             {
  187.                 currentRotation.x += rotationIncrement;
  188.             }
  189.  
  190.             foreach (var obj in previewObjects)
  191.             {
  192.                 obj.transform.rotation = Quaternion.Euler(currentRotation);
  193.             }
  194.         }
  195.     }
  196.  
  197.     public void DropItem(bool isLeftHand)
  198.     {
  199.         Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
  200.         if (itemToDrop != null)
  201.         {
  202.             GameObject itemObject = itemToDrop.gameObject;
  203.             itemObject.transform.SetParent(null);
  204.  
  205.             Vector3 dropPosition;
  206.             if (isPrecisionDrop && previewObjects.Count > 0)
  207.             {
  208.                 int index = isLeftHand ? 0 : (previewObjects.Count > 1 ? 1 : 0);
  209.                 dropPosition = previewObjects[index].transform.position;
  210.                 itemObject.transform.position = dropPosition;
  211.                 itemObject.transform.rotation = previewObjects[index].transform.rotation;
  212.             }
  213.             else
  214.             {
  215.                 dropPosition = transform.position + transform.forward * 1f + transform.right * Random.Range(-0.5f, 0.5f);
  216.                 itemObject.transform.position = dropPosition;
  217.                 itemObject.transform.rotation = Random.rotation;
  218.             }
  219.  
  220.             itemObject.SetActive(true);
  221.  
  222.             Rigidbody rb = itemObject.GetComponent<Rigidbody>();
  223.             if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
  224.             rb.isKinematic = false;
  225.             rb.useGravity = true;
  226.             rb.AddForce(Vector3.down * 2f, ForceMode.Impulse);
  227.  
  228.             inventorySystem.UnequipItem(isLeftHand);
  229.             RestoreOriginalLayer(itemObject);
  230.  
  231.             InteractableObject interactable = itemObject.GetComponent<InteractableObject>();
  232.             if (interactable != null)
  233.             {
  234.                 interactable.enabled = true;
  235.             }
  236.  
  237.             playerMovement.UpdateCarryingAnimations();
  238.         }
  239.  
  240.         ClearPreviewObjects();
  241.  
  242.         if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
  243.         {
  244.             isPrecisionDrop = false;
  245.             isRotating = false;
  246.         }
  247.  
  248.         UpdateItemVisibility();
  249.     }
  250.  
  251.     public void DropBothItems()
  252.     {
  253.         DropItem(true);
  254.         DropItem(false);
  255.     }
  256.  
  257.     private void ChangeItemLayer(GameObject itemObject, LayerMask newLayer)
  258.     {
  259.         if (!originalLayers.ContainsKey(itemObject))
  260.         {
  261.             originalLayers[itemObject] = itemObject.layer;
  262.         }
  263.         itemObject.layer = (int)Mathf.Log(newLayer.value, 2);
  264.     }
  265.  
  266.     private void RestoreOriginalLayer(GameObject itemObject)
  267.     {
  268.         if (originalLayers.ContainsKey(itemObject))
  269.         {
  270.             itemObject.layer = originalLayers[itemObject];
  271.             originalLayers.Remove(itemObject);
  272.         }
  273.     }
  274.  
  275.     public void TogglePrecisionDrop()
  276.     {
  277.         isPrecisionDrop = !isPrecisionDrop;
  278.         UpdateItemVisibility();
  279.         UIManager.Instance.ShowInventoryPrompt(null);
  280.         UIManager.Instance.UpdatePromptText(isPrecisionDrop ? "Precision drop enabled" : "Precision drop disabled");
  281.         UIManager.Instance.ShowOkayButton();
  282.     }
  283.  
  284.     private void ClearPreviewObjects()
  285.     {
  286.         foreach (var obj in previewObjects)
  287.         {
  288.             Destroy(obj);
  289.         }
  290.         previewObjects.Clear();
  291.     }
  292.  
  293.     public bool IsPrecisionDropEnabled()
  294.     {
  295.         return isPrecisionDrop;
  296.     }
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement