evelynshilosky

PlayerMovement - Part 6.1

Mar 27th, 2025
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.83 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerMovement : MonoBehaviour
  5. {
  6.     public CharacterController controller;
  7.     public float speed = 12f;
  8.     public float gravity = -9.81f * 2;
  9.     public float jumpHeight = 3f;
  10.     public Transform groundCheck;
  11.     public float groundDistance = 0.4f;
  12.     public LayerMask groundMask;
  13.     public Animator maleAnimator;
  14.     public Animator femaleAnimator;
  15.     public Transform maleCamera;
  16.     public Transform femaleCamera;
  17.  
  18.     public BoxCollider groundTrigger;
  19.     public BoxCollider normalTrigger;
  20.     public BoxCollider highTrigger;
  21.  
  22.     public float crouchCameraX = 0.068f;
  23.     public float crouchCameraY = 0.942f;
  24.     public float crouchCameraZ = 0.407f;
  25.  
  26.     public float standCameraX = 0.04599719f;
  27.     public float standCameraY = 1.734f;
  28.     public float standCameraZ = 0.1539853f;
  29.  
  30.     private Vector3 originalMaleCameraPosition;
  31.     private Vector3 crouchMaleCameraPosition;
  32.  
  33.     public bool isWalking;
  34.     public bool isRunning;
  35.     public bool isJumping;
  36.     public bool isClimbing;
  37.     public bool isCrouching;
  38.     public bool isSwimming;
  39.     public bool isTreadingWater;
  40.     public bool CarryingLeftHandItem;
  41.     public bool CarryingRightHandItem;
  42.     public bool CarryingTwoHandedItem;
  43.     public bool CarryingTwoItems;
  44.     public bool isMoving;
  45.  
  46.     private const string ANIM_IS_WALKING = "isWalking";
  47.     private const string ANIM_IS_RUNNING = "isRunning";
  48.     private const string ANIM_IS_JUMPING = "isJumping";
  49.     private const string ANIM_IS_CLIMBING = "isClimbing";
  50.     private const string ANIM_IS_CROUCHING = "isCrouching";
  51.     private const string ANIM_IS_SWIMMING = "isSwimming";
  52.     private const string ANIM_IS_TREADING_WATER = "isTreadingWater";
  53.  
  54.     private const string ANIM_JUMP = "Jump";
  55.  
  56.     private const float ANIMATION_TRANSITION_SPEED = 0.1f;
  57.  
  58.     public Animator currentAnimator;
  59.     public bool isMale = true;
  60.  
  61.     private Vector3 velocity;
  62.     private bool isGrounded;
  63.  
  64.     private InteractionSystem interactionSystem;
  65.     private InventorySystem inventorySystem;
  66.  
  67.     private Vector3 lastPosition;
  68.     private bool isJumpingCoroutineRunning = false;
  69.  
  70.     public float interactionRange = 2f;
  71.     public LayerMask interactableLayers;
  72.  
  73.     void Start()
  74.     {
  75.         controller = GetComponent<CharacterController>();
  76.         interactionSystem = GetComponent<InteractionSystem>();
  77.         inventorySystem = InventorySystem.Instance;
  78.         UpdatePlayerReferences();
  79.  
  80.         originalMaleCameraPosition = new Vector3(standCameraX, standCameraY, standCameraZ);
  81.         crouchMaleCameraPosition = new Vector3(crouchCameraX, crouchCameraY, crouchCameraZ);
  82.  
  83.         if (isMale)
  84.         {
  85.             maleCamera.localPosition = originalMaleCameraPosition;
  86.         }
  87.  
  88.         lastPosition = transform.position;
  89.     }
  90.  
  91.     void Update()
  92.     {
  93.         HandleMovement();
  94.         HandleInteractions();
  95.         UpdateAnimatorParameters();
  96.         UpdateCameraPosition();
  97.         UpdateHeldItemPositions();
  98.     }
  99.  
  100.     void UpdatePlayerReferences()
  101.     {
  102.         currentAnimator = isMale ? maleAnimator : femaleAnimator;
  103.     }
  104.  
  105.     void HandleMovement()
  106.     {
  107.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  108.  
  109.         if (isGrounded && velocity.y < 0)
  110.         {
  111.             velocity.y = -2f;
  112.             isJumping = false;
  113.         }
  114.  
  115.         float x = Input.GetAxis("Horizontal");
  116.         float z = Input.GetAxis("Vertical");
  117.  
  118.         Vector3 move = transform.right * x + transform.forward * z;
  119.  
  120.         controller.Move(move * speed * Time.deltaTime);
  121.  
  122.         bool wasWalking = isWalking;
  123.         isWalking = move.magnitude > 0.1f && !isRunning;
  124.         isRunning = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && move.magnitude > 0.1f;
  125.  
  126.         if (wasWalking != isWalking)
  127.         {
  128.             UpdateWalkingAnimation();
  129.         }
  130.  
  131.         if (Input.GetButtonDown("Jump") && isGrounded && !isJumpingCoroutineRunning)
  132.         {
  133.             StartCoroutine(JumpCoroutine());
  134.         }
  135.  
  136.         velocity.y += gravity * Time.deltaTime;
  137.  
  138.         controller.Move(velocity * Time.deltaTime);
  139.  
  140.         bool wasCrouching = isCrouching;
  141.         isCrouching = Input.GetKey(KeyCode.LeftControl);
  142.  
  143.         if (isCrouching != wasCrouching)
  144.         {
  145.             UpdateCameraPosition();
  146.             UpdateCrouchingAnimation();
  147.         }
  148.     }
  149.  
  150.     private void HandleInteractions()
  151.     {
  152.         if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
  153.         {
  154.             RaycastHit hit;
  155.             if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, interactionRange, interactableLayers))
  156.             {
  157.                 InteractableObject interactable = hit.collider.GetComponent<InteractableObject>();
  158.                 if (interactable != null)
  159.                 {
  160.                     if (Input.GetMouseButtonDown(1) && interactable.item.isStorageItem)
  161.                     {
  162.                         // Right-click on storage item
  163.                         interactable.Interact(this, true);
  164.                     }
  165.                     else if (Input.GetMouseButtonDown(0))
  166.                     {
  167.                         // Left-click on any interactable
  168.                         interactable.Interact(this, false);
  169.                     }
  170.                 }
  171.             }
  172.         }
  173.  
  174.         if (Input.GetKeyDown(KeyCode.P))
  175.         {
  176.             interactionSystem.TogglePrecisionDrop();
  177.         }
  178.  
  179.         if (Input.GetKeyDown(KeyCode.G))
  180.         {
  181.             interactionSystem.DropItem(false);
  182.         }
  183.         if (Input.GetKeyDown(KeyCode.H))
  184.         {
  185.             interactionSystem.DropItem(true);
  186.         }
  187.         if (Input.GetKeyDown(KeyCode.F))
  188.         {
  189.             interactionSystem.DropBothItems();
  190.         }
  191.     }
  192.  
  193.     void UpdateAnimatorParameters()
  194.     {
  195.         currentAnimator.SetBool(ANIM_IS_WALKING, isWalking);
  196.         currentAnimator.SetBool(ANIM_IS_RUNNING, isRunning);
  197.         currentAnimator.SetBool(ANIM_IS_JUMPING, isJumping);
  198.         currentAnimator.SetBool(ANIM_IS_CLIMBING, isClimbing);
  199.         currentAnimator.SetBool(ANIM_IS_CROUCHING, isCrouching);
  200.         currentAnimator.SetBool(ANIM_IS_SWIMMING, isSwimming);
  201.         currentAnimator.SetBool(ANIM_IS_TREADING_WATER, isTreadingWater);
  202.         currentAnimator.SetBool("isMoving", isMoving);
  203.     }
  204.  
  205.     void UpdateWalkingAnimation()
  206.     {
  207.         if (isWalking)
  208.         {
  209.             currentAnimator.CrossFadeInFixedTime("Walking", ANIMATION_TRANSITION_SPEED);
  210.         }
  211.         else
  212.         {
  213.             currentAnimator.CrossFadeInFixedTime("Idle", ANIMATION_TRANSITION_SPEED);
  214.         }
  215.     }
  216.  
  217.     void UpdateCrouchingAnimation()
  218.     {
  219.         currentAnimator.CrossFadeInFixedTime(isCrouching ? "Crouching Idle" : "Idle", ANIMATION_TRANSITION_SPEED);
  220.     }
  221.  
  222.     void UpdateCameraPosition()
  223.     {
  224.         if (isMale)
  225.         {
  226.             maleCamera.localPosition = isCrouching ? crouchMaleCameraPosition : originalMaleCameraPosition;
  227.         }
  228.     }
  229.  
  230.     public void PlayGrabbingAnimation(bool isLeftHand, string height)
  231.     {
  232.         string animationName = GetPickUpAnimationName(isLeftHand, height);
  233.         currentAnimator.Play(animationName, 0, 0f);
  234.     }
  235.  
  236.     private string GetPickUpAnimationName(bool isLeftHand, string height)
  237.     {
  238.         string hand = isLeftHand ? "Left" : "Right";
  239.         return $"Pick up Single Handed Object from {height} ({hand})";
  240.     }
  241.  
  242.     public void UpdateCarryingAnimations()
  243.     {
  244.         bool leftHandItem = inventorySystem.leftHandItem != null;
  245.         bool rightHandItem = inventorySystem.rightHandItem != null;
  246.         bool twoHandedItem = leftHandItem && inventorySystem.leftHandItem.isTwoHanded;
  247.  
  248.         string carryAnimation = GetCarryingAnimationName(leftHandItem, rightHandItem, twoHandedItem);
  249.  
  250.         if (!string.IsNullOrEmpty(carryAnimation))
  251.         {
  252.             currentAnimator.CrossFadeInFixedTime(carryAnimation, ANIMATION_TRANSITION_SPEED);
  253.         }
  254.  
  255.         currentAnimator.SetBool("CarryingLeftHandItem", leftHandItem && !twoHandedItem);
  256.         currentAnimator.SetBool("CarryingRightHandItem", rightHandItem && !twoHandedItem);
  257.         currentAnimator.SetBool("CarryingTwoHandedItem", twoHandedItem);
  258.         currentAnimator.SetBool("CarryingTwoItems", leftHandItem && rightHandItem && !twoHandedItem);
  259.     }
  260.  
  261.     private string GetCarryingAnimationName(bool leftHandItem, bool rightHandItem, bool twoHandedItem)
  262.     {
  263.         if (twoHandedItem) return "carrying a two handed item";
  264.         if (leftHandItem && rightHandItem) return "carrying two single handed items";
  265.         if (leftHandItem) return "Carrying one handed item (Left)";
  266.         if (rightHandItem) return "Carrying one handed item (Right)";
  267.         return null;
  268.     }
  269.  
  270.     void UpdateHeldItemPositions()
  271.     {
  272.         if (inventorySystem.leftHandItem != null)
  273.         {
  274.             UpdateItemPosition(inventorySystem.leftHandItem.gameObject, true);
  275.         }
  276.         if (inventorySystem.rightHandItem != null)
  277.         {
  278.             UpdateItemPosition(inventorySystem.rightHandItem.gameObject, false);
  279.         }
  280.     }
  281.  
  282.     public void UpdateItemPosition(GameObject itemObject, bool isLeftHand)
  283.     {
  284.         Transform objectHolder = isLeftHand ? interactionSystem.leftObjectHolder : interactionSystem.rightObjectHolder;
  285.         Item item = itemObject.GetComponent<Item>();
  286.  
  287.         bool isHoldingTwoItems = inventorySystem.leftHandItem != null && inventorySystem.rightHandItem != null;
  288.  
  289.         Vector3 positionOffset = isLeftHand ?
  290.             (isHoldingTwoItems ? item.leftTwoPositionOffset : item.leftPositionOffset) :
  291.             (isHoldingTwoItems ? item.rightTwoPositionOffset : item.rightPositionOffset);
  292.  
  293.         Vector3 rotationOffset = isLeftHand ?
  294.             (isHoldingTwoItems ? item.leftTwoRotationOffset : item.leftRotationOffset) :
  295.             (isHoldingTwoItems ? item.rightTwoRotationOffset : item.rightRotationOffset);
  296.  
  297.         itemObject.transform.position = objectHolder.position + objectHolder.TransformDirection(positionOffset);
  298.         itemObject.transform.rotation = objectHolder.rotation * Quaternion.Euler(rotationOffset);
  299.     }
  300.  
  301.     public string GetPickUpHeight(Vector3 itemPosition)
  302.     {
  303.         if (highTrigger.bounds.Contains(itemPosition))
  304.             return "High Surface";
  305.         else if (normalTrigger.bounds.Contains(itemPosition))
  306.             return "Normal Surface";
  307.         else if (groundTrigger.bounds.Contains(itemPosition))
  308.             return "Ground";
  309.         else
  310.             return "Normal Surface";
  311.     }
  312.  
  313.     private IEnumerator JumpCoroutine()
  314.     {
  315.         isJumpingCoroutineRunning = true;
  316.         currentAnimator.Play(ANIM_JUMP, 0, 0f);
  317.         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  318.         isJumping = true;
  319.         yield return new WaitForSeconds(currentAnimator.GetCurrentAnimatorStateInfo(0).length);
  320.         isJumpingCoroutineRunning = false;
  321.     }
  322.  
  323.     public void TryPickUpItem(InteractableObject interactable)
  324.     {
  325.         Item item = interactable.item;
  326.  
  327.         if (item.isBackpack)
  328.         {
  329.             // Store reference and show backpack-specific prompt
  330.             interactionSystem.currentBackpackItem = item;
  331.             UIManager.Instance.ShowBackpackPrompt(item);
  332.         }
  333.         else
  334.         {
  335.             // Existing pickup logic
  336.             bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
  337.  
  338.             if (item.isTwoHanded)
  339.             {
  340.                 if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
  341.                 {
  342.                     EquipItem(item, true, true);
  343.                 }
  344.                 else
  345.                 {
  346.                     Debug.Log("Cannot pick up two-handed item. Hands are not empty.");
  347.                 }
  348.             }
  349.             else if (inventorySystem.rightHandItem == null || isLeftHand)
  350.             {
  351.                 EquipItem(item, isLeftHand, false);
  352.             }
  353.             else
  354.             {
  355.                 Debug.Log("Cannot pick up item. Both hands are full.");
  356.             }
  357.         }
  358.     }
  359.  
  360.     private void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
  361.     {
  362.         inventorySystem.EquipItem(item, isLeftHand, isTwoHanded);
  363.         interactionSystem.UpdateItemPosition(item.gameObject, isLeftHand);
  364.         interactionSystem.ChangeItemLayer(item.gameObject, interactionSystem.pickupLayer);
  365.         UpdateCarryingAnimations();
  366.     }
  367.  
  368.     public void SetClimbingState(bool climbing)
  369.     {
  370.         isClimbing = climbing;
  371.     }
  372.  
  373.     public void SetSwimmingState(bool swimming)
  374.     {
  375.         isSwimming = swimming;
  376.     }
  377.  
  378.     public void SetTreadingWaterState(bool treadingWater)
  379.     {
  380.         isTreadingWater = treadingWater;
  381.     }
  382. }
  383.  
Advertisement
Add Comment
Please, Sign In to add comment