Advertisement
evelynshilosky

InteractionSystem - Part 1

Jan 21st, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class InteractionSystem : MonoBehaviour
  6. {
  7.     public float interactionRange = 2f;
  8.     public LayerMask interactableLayers;
  9.     public Transform rightObjectHolder;
  10.     public Transform leftObjectHolder;
  11.  
  12.     private InventorySystem inventorySystem;
  13.     private PlayerMovement playerMovement;
  14.     private Dictionary<GameObject, int> originalLayers = new Dictionary<GameObject, int>();
  15.  
  16.     private Coroutine pickUpCoroutine;
  17.  
  18.     private void Start()
  19.     {
  20.         inventorySystem = InventorySystem.Instance;
  21.         playerMovement = GetComponent<PlayerMovement>();
  22.     }
  23.  
  24.     public void TryPickUpItem()
  25.     {
  26.         RaycastHit hit;
  27.         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, interactionRange, interactableLayers))
  28.         {
  29.             InteractableObject interactable = hit.collider.GetComponent<InteractableObject>();
  30.             if (interactable != null)
  31.             {
  32.                 PickUpItem(interactable);
  33.             }
  34.         }
  35.     }
  36.  
  37.     public void PickUpItem(InteractableObject interactable)
  38.     {
  39.         Item item = interactable.item;
  40.         bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
  41.  
  42.         if (item.isTwoHanded)
  43.         {
  44.             if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
  45.             {
  46.                 StartPickUpAnimation(item, interactable.gameObject, true, true, interactable.transform);
  47.             }
  48.             else
  49.             {
  50.                 Debug.Log("<color=red>Cannot pick up two-handed item. Hands are not empty.</color>");
  51.             }
  52.         }
  53.         else if (inventorySystem.rightHandItem == null || isLeftHand)
  54.         {
  55.             StartPickUpAnimation(item, interactable.gameObject, isLeftHand, false, interactable.transform);
  56.         }
  57.         else
  58.         {
  59.             Debug.Log("<color=red>Cannot pick up item. Both hands are full.</color>");
  60.         }
  61.     }
  62.  
  63.     private void StartPickUpAnimation(Item item, GameObject itemObject, bool isLeftHand, bool isTwoHanded, Transform itemTransform)
  64.     {
  65.         if (pickUpCoroutine != null)
  66.         {
  67.             StopCoroutine(pickUpCoroutine);
  68.         }
  69.         pickUpCoroutine = StartCoroutine(PickUpAnimationCoroutine(item, itemObject, isLeftHand, isTwoHanded, itemTransform));
  70.     }
  71.  
  72.     private IEnumerator PickUpAnimationCoroutine(Item item, GameObject itemObject, bool isLeftHand, bool isTwoHanded, Transform itemTransform)
  73.     {
  74.         string height = playerMovement.GetPickUpHeight(itemTransform.position);
  75.         string animationName = GetPickUpAnimationName(isLeftHand, height);
  76.  
  77.         playerMovement.currentAnimator.Play(animationName);
  78.  
  79.         // Wait for half of the animation duration
  80.         yield return new WaitForSeconds(playerMovement.currentAnimator.GetCurrentAnimatorStateInfo(0).length * 0.5f);
  81.  
  82.         // Equip and prepare the item
  83.         EquipAndPrepareItem(item, itemObject, isLeftHand, isTwoHanded);
  84.  
  85.         // Wait for the remaining animation duration
  86.         yield return new WaitForSeconds(playerMovement.currentAnimator.GetCurrentAnimatorStateInfo(0).length * 0.5f);
  87.  
  88.         // Update carrying animations
  89.         playerMovement.UpdateCarryingAnimations();
  90.     }
  91.  
  92.     private string GetPickUpAnimationName(bool isLeftHand, string height)
  93.     {
  94.         string hand = isLeftHand ? "Left" : "Right";
  95.         return $"Pick up Single Handed Object from {height} ({hand})";
  96.     }
  97.  
  98.     private void EquipAndPrepareItem(Item item, GameObject itemObject, bool isLeftHand, bool isTwoHanded)
  99.     {
  100.         inventorySystem.EquipItem(item, isLeftHand, isTwoHanded);
  101.         PrepareItemForHolding(itemObject, isLeftHand);
  102.         ChangeItemLayer(itemObject, LayerMask.NameToLayer("Player"));
  103.     }
  104.  
  105.     private void PrepareItemForHolding(GameObject itemObject, bool isLeftHand)
  106.     {
  107.         Rigidbody rb = itemObject.GetComponent<Rigidbody>();
  108.         if (rb != null) rb.isKinematic = true;
  109.  
  110.         Item item = itemObject.GetComponent<Item>();
  111.         Transform objectHolder = isLeftHand ? leftObjectHolder : rightObjectHolder;
  112.         Vector3 positionOffset = isLeftHand ? item.leftPositionOffset : item.rightPositionOffset;
  113.         Vector3 rotationOffset = isLeftHand ? item.leftRotationOffset : item.rightRotationOffset;
  114.  
  115.         itemObject.transform.SetParent(objectHolder);
  116.         itemObject.transform.localPosition = positionOffset;
  117.         itemObject.transform.localRotation = Quaternion.Euler(rotationOffset);
  118.     }
  119.  
  120.     private void ChangeItemLayer(GameObject itemObject, int newLayer)
  121.     {
  122.         if (!originalLayers.ContainsKey(itemObject))
  123.         {
  124.             originalLayers[itemObject] = itemObject.layer;
  125.         }
  126.         itemObject.layer = newLayer;
  127.     }
  128.  
  129.     public void DropItem(bool isLeftHand)
  130.     {
  131.         Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
  132.         if (itemToDrop != null)
  133.         {
  134.             GameObject itemObject = itemToDrop.gameObject;
  135.             itemObject.transform.SetParent(null);
  136.             itemObject.transform.position = transform.position + transform.forward * 1.5f;
  137.             itemObject.transform.rotation = Quaternion.identity;
  138.  
  139.             Rigidbody rb = itemObject.GetComponent<Rigidbody>();
  140.             if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
  141.             rb.isKinematic = false;
  142.  
  143.             inventorySystem.UnequipItem(isLeftHand);
  144.             RestoreOriginalLayer(itemObject);
  145.         }
  146.     }
  147.  
  148.     private void RestoreOriginalLayer(GameObject itemObject)
  149.     {
  150.         if (originalLayers.TryGetValue(itemObject, out int originalLayer))
  151.         {
  152.             itemObject.layer = originalLayer;
  153.             originalLayers.Remove(itemObject);
  154.         }
  155.     }
  156.  
  157.     public void DropBothItems()
  158.     {
  159.         DropItem(true);
  160.         DropItem(false);
  161.     }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement