Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class InteractionSystem : MonoBehaviour
- {
- public float interactionRange = 2f;
- public LayerMask interactableLayers;
- public Transform rightObjectHolder;
- public Transform leftObjectHolder;
- private InventorySystem inventorySystem;
- private PlayerMovement playerMovement;
- private Dictionary<GameObject, int> originalLayers = new Dictionary<GameObject, int>();
- private Coroutine pickUpCoroutine;
- private void Start()
- {
- inventorySystem = InventorySystem.Instance;
- playerMovement = GetComponent<PlayerMovement>();
- }
- public void TryPickUpItem()
- {
- RaycastHit hit;
- if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, interactionRange, interactableLayers))
- {
- InteractableObject interactable = hit.collider.GetComponent<InteractableObject>();
- if (interactable != null)
- {
- PickUpItem(interactable);
- }
- }
- }
- public void PickUpItem(InteractableObject interactable)
- {
- Item item = interactable.item;
- bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
- if (item.isTwoHanded)
- {
- if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
- {
- StartPickUpAnimation(item, interactable.gameObject, true, true, interactable.transform);
- }
- else
- {
- Debug.Log("<color=red>Cannot pick up two-handed item. Hands are not empty.</color>");
- }
- }
- else if (inventorySystem.rightHandItem == null || isLeftHand)
- {
- StartPickUpAnimation(item, interactable.gameObject, isLeftHand, false, interactable.transform);
- }
- else
- {
- Debug.Log("<color=red>Cannot pick up item. Both hands are full.</color>");
- }
- }
- private void StartPickUpAnimation(Item item, GameObject itemObject, bool isLeftHand, bool isTwoHanded, Transform itemTransform)
- {
- if (pickUpCoroutine != null)
- {
- StopCoroutine(pickUpCoroutine);
- }
- pickUpCoroutine = StartCoroutine(PickUpAnimationCoroutine(item, itemObject, isLeftHand, isTwoHanded, itemTransform));
- }
- private IEnumerator PickUpAnimationCoroutine(Item item, GameObject itemObject, bool isLeftHand, bool isTwoHanded, Transform itemTransform)
- {
- string height = playerMovement.GetPickUpHeight(itemTransform.position);
- string animationName = GetPickUpAnimationName(isLeftHand, height);
- playerMovement.currentAnimator.Play(animationName);
- // Wait for half of the animation duration
- yield return new WaitForSeconds(playerMovement.currentAnimator.GetCurrentAnimatorStateInfo(0).length * 0.5f);
- // Equip and prepare the item
- EquipAndPrepareItem(item, itemObject, isLeftHand, isTwoHanded);
- // Wait for the remaining animation duration
- yield return new WaitForSeconds(playerMovement.currentAnimator.GetCurrentAnimatorStateInfo(0).length * 0.5f);
- // Update carrying animations
- playerMovement.UpdateCarryingAnimations();
- }
- private string GetPickUpAnimationName(bool isLeftHand, string height)
- {
- string hand = isLeftHand ? "Left" : "Right";
- return $"Pick up Single Handed Object from {height} ({hand})";
- }
- private void EquipAndPrepareItem(Item item, GameObject itemObject, bool isLeftHand, bool isTwoHanded)
- {
- inventorySystem.EquipItem(item, isLeftHand, isTwoHanded);
- PrepareItemForHolding(itemObject, isLeftHand);
- ChangeItemLayer(itemObject, LayerMask.NameToLayer("Player"));
- }
- private void PrepareItemForHolding(GameObject itemObject, bool isLeftHand)
- {
- Rigidbody rb = itemObject.GetComponent<Rigidbody>();
- if (rb != null) rb.isKinematic = true;
- Item item = itemObject.GetComponent<Item>();
- Transform objectHolder = isLeftHand ? leftObjectHolder : rightObjectHolder;
- Vector3 positionOffset = isLeftHand ? item.leftPositionOffset : item.rightPositionOffset;
- Vector3 rotationOffset = isLeftHand ? item.leftRotationOffset : item.rightRotationOffset;
- itemObject.transform.SetParent(objectHolder);
- itemObject.transform.localPosition = positionOffset;
- itemObject.transform.localRotation = Quaternion.Euler(rotationOffset);
- }
- private void ChangeItemLayer(GameObject itemObject, int newLayer)
- {
- if (!originalLayers.ContainsKey(itemObject))
- {
- originalLayers[itemObject] = itemObject.layer;
- }
- itemObject.layer = newLayer;
- }
- public void DropItem(bool isLeftHand)
- {
- Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
- if (itemToDrop != null)
- {
- GameObject itemObject = itemToDrop.gameObject;
- itemObject.transform.SetParent(null);
- itemObject.transform.position = transform.position + transform.forward * 1.5f;
- itemObject.transform.rotation = Quaternion.identity;
- Rigidbody rb = itemObject.GetComponent<Rigidbody>();
- if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
- rb.isKinematic = false;
- inventorySystem.UnequipItem(isLeftHand);
- RestoreOriginalLayer(itemObject);
- }
- }
- private void RestoreOriginalLayer(GameObject itemObject)
- {
- if (originalLayers.TryGetValue(itemObject, out int originalLayer))
- {
- itemObject.layer = originalLayer;
- originalLayers.Remove(itemObject);
- }
- }
- public void DropBothItems()
- {
- DropItem(true);
- DropItem(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement