Advertisement
evelynshilosky

PlayerMovement - Part 1

Jan 21st, 2025 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.89 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.     private const string ANIM_IS_WALKING = "isWalking";
  34.     private const string ANIM_IS_RUNNING = "isRunning";
  35.     private const string ANIM_IS_JUMPING = "isJumping";
  36.     private const string ANIM_IS_CLIMBING = "isClimbing";
  37.     private const string ANIM_IS_CROUCHING = "isCrouching";
  38.     private const string ANIM_IS_SWIMMING = "isSwimming";
  39.     private const string ANIM_IS_TREADING_WATER = "isTreadingWater";
  40.  
  41.     private const string ANIM_JUMP = "Jump";
  42.  
  43.     private const float ANIMATION_TRANSITION_SPEED = 0.1f;
  44.  
  45.     public Animator currentAnimator;
  46.     public bool isMale = true;
  47.  
  48.     private bool isWalking;
  49.     private bool isRunning;
  50.     private bool isJumping;
  51.     private bool isClimbing;
  52.     private bool isCrouching;
  53.     private bool isSwimming;
  54.     private bool isTreadingWater;
  55.  
  56.     private Vector3 velocity;
  57.     private bool isGrounded;
  58.  
  59.     private InteractionSystem interactionSystem;
  60.     private InventorySystem inventorySystem;
  61.  
  62.     void Start()
  63.     {
  64.         controller = GetComponent<CharacterController>();
  65.         interactionSystem = GetComponent<InteractionSystem>();
  66.         inventorySystem = InventorySystem.Instance;
  67.         UpdatePlayerReferences();
  68.  
  69.         originalMaleCameraPosition = new Vector3(standCameraX, standCameraY, standCameraZ);
  70.         crouchMaleCameraPosition = new Vector3(crouchCameraX, crouchCameraY, crouchCameraZ);
  71.  
  72.         // Set initial camera position
  73.         if (isMale)
  74.         {
  75.             maleCamera.localPosition = originalMaleCameraPosition;
  76.         }
  77.     }
  78.  
  79.     void Update()
  80.     {
  81.         HandleMovement();
  82.         HandleInteractions();
  83.         UpdateAnimatorParameters();
  84.         UpdateCameraPosition();
  85.     }
  86.  
  87.     void UpdatePlayerReferences()
  88.     {
  89.         currentAnimator = isMale ? maleAnimator : femaleAnimator;
  90.     }
  91.  
  92.     void HandleMovement()
  93.     {
  94.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  95.  
  96.         if (isGrounded && velocity.y < 0)
  97.         {
  98.             velocity.y = -2f;
  99.         }
  100.  
  101.         float x = Input.GetAxis("Horizontal");
  102.         float z = Input.GetAxis("Vertical");
  103.  
  104.         Vector3 move = transform.right * x + transform.forward * z;
  105.  
  106.         controller.Move(move * speed * Time.deltaTime);
  107.  
  108.         bool wasWalking = isWalking;
  109.         isWalking = move.magnitude > 0.1f && !isRunning;
  110.         isRunning = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && move.magnitude > 0.1f;
  111.  
  112.         // Immediately update walking animation
  113.         if (wasWalking != isWalking)
  114.         {
  115.             UpdateWalkingAnimation();
  116.         }
  117.  
  118.         if (Input.GetButtonDown("Jump") && isGrounded)
  119.         {
  120.             StartCoroutine(JumpCoroutine());
  121.         }
  122.  
  123.         velocity.y += gravity * Time.deltaTime;
  124.  
  125.         controller.Move(velocity * Time.deltaTime);
  126.  
  127.         bool wasCrouching = isCrouching;
  128.         isCrouching = Input.GetKey(KeyCode.LeftControl);
  129.  
  130.         // Handle camera position change and animation when crouching state changes
  131.         if (isCrouching != wasCrouching)
  132.         {
  133.             UpdateCameraPosition();
  134.             UpdateCrouchingAnimation();
  135.         }
  136.     }
  137.  
  138.     private IEnumerator JumpCoroutine()
  139.     {
  140.         // Immediately stop any current animation and play the jump animation
  141.         currentAnimator.Play(ANIM_JUMP, 0, 0f);
  142.         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  143.         isJumping = true;
  144.         yield return new WaitForSeconds(currentAnimator.GetCurrentAnimatorStateInfo(0).length);
  145.         isJumping = false;
  146.     }
  147.  
  148.     void HandleInteractions()
  149.     {
  150.         if (Input.GetMouseButtonDown(0))
  151.         {
  152.             interactionSystem.TryPickUpItem();
  153.         }
  154.         if (Input.GetKeyDown(KeyCode.G))
  155.         {
  156.             interactionSystem.DropItem(false);
  157.         }
  158.         if (Input.GetKeyDown(KeyCode.H))
  159.         {
  160.             interactionSystem.DropItem(true);
  161.         }
  162.         if (Input.GetKeyDown(KeyCode.F))
  163.         {
  164.             interactionSystem.DropBothItems();
  165.         }
  166.     }
  167.  
  168.     void UpdateAnimatorParameters()
  169.     {
  170.         currentAnimator.SetBool(ANIM_IS_RUNNING, isRunning);
  171.         currentAnimator.SetBool(ANIM_IS_JUMPING, isJumping);
  172.         currentAnimator.SetBool(ANIM_IS_CLIMBING, isClimbing);
  173.         currentAnimator.SetBool(ANIM_IS_CROUCHING, isCrouching);
  174.         currentAnimator.SetBool(ANIM_IS_SWIMMING, isSwimming);
  175.         currentAnimator.SetBool(ANIM_IS_TREADING_WATER, isTreadingWater);
  176.     }
  177.  
  178.     void UpdateWalkingAnimation()
  179.     {
  180.         if (isWalking)
  181.         {
  182.             currentAnimator.CrossFadeInFixedTime("Walking", ANIMATION_TRANSITION_SPEED);
  183.         }
  184.         else
  185.         {
  186.             currentAnimator.CrossFadeInFixedTime("Idle", ANIMATION_TRANSITION_SPEED);
  187.         }
  188.     }
  189.  
  190.     void UpdateCrouchingAnimation()
  191.     {
  192.         if (isCrouching)
  193.         {
  194.             currentAnimator.CrossFadeInFixedTime("Crouching Idle", ANIMATION_TRANSITION_SPEED);
  195.         }
  196.         else
  197.         {
  198.             currentAnimator.CrossFadeInFixedTime("Idle", ANIMATION_TRANSITION_SPEED);
  199.         }
  200.     }
  201.  
  202.     void UpdateCameraPosition()
  203.     {
  204.         if (isMale)
  205.         {
  206.             maleCamera.localPosition = isCrouching ? crouchMaleCameraPosition : originalMaleCameraPosition;
  207.         }
  208.     }
  209.  
  210.     public void UpdateCarryingAnimations()
  211.     {
  212.         bool leftHandItem = inventorySystem.leftHandItem != null;
  213.         bool rightHandItem = inventorySystem.rightHandItem != null;
  214.         bool twoHandedItem = leftHandItem && inventorySystem.leftHandItem.isTwoHanded;
  215.  
  216.         currentAnimator.SetBool("CarryingLeftHandItem", leftHandItem && !twoHandedItem);
  217.         currentAnimator.SetBool("CarryingRightHandItem", rightHandItem && !twoHandedItem);
  218.         currentAnimator.SetBool("CarryingTwoHandedItem", twoHandedItem);
  219.         currentAnimator.SetBool("CarryingTwoItems", leftHandItem && rightHandItem && !twoHandedItem);
  220.     }
  221.  
  222.     public string GetPickUpHeight(Vector3 itemPosition)
  223.     {
  224.         if (highTrigger.bounds.Contains(itemPosition))
  225.             return "High Surface";
  226.         else if (normalTrigger.bounds.Contains(itemPosition))
  227.             return "Normal Surface";
  228.         else if (groundTrigger.bounds.Contains(itemPosition))
  229.             return "Ground";
  230.         else
  231.             return "Normal Surface"; // Default
  232.     }
  233.  
  234.     public void PickUpItem(InteractableObject interactable)
  235.     {
  236.         if (interactionSystem != null)
  237.         {
  238.             interactionSystem.PickUpItem(interactable);
  239.         }
  240.         else
  241.         {
  242.             Debug.LogError("InteractionSystem is not initialized in PlayerMovement.");
  243.         }
  244.     }
  245.  
  246.     public void SetClimbingState(bool climbing)
  247.     {
  248.         isClimbing = climbing;
  249.     }
  250.  
  251.     public void SetSwimmingState(bool swimming)
  252.     {
  253.         isSwimming = swimming;
  254.     }
  255.  
  256.     public void SetTreadingWaterState(bool treadingWater)
  257.     {
  258.         isTreadingWater = treadingWater;
  259.     }
  260. }
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement