Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerMovement : MonoBehaviour
- {
- public CharacterController controller;
- public float speed = 12f;
- public float gravity = -9.81f * 2;
- public float jumpHeight = 3f;
- public Transform groundCheck;
- public float groundDistance = 0.4f;
- public LayerMask groundMask;
- public Animator maleAnimator;
- public Animator femaleAnimator;
- public Transform maleCamera;
- public Transform femaleCamera;
- public BoxCollider groundTrigger;
- public BoxCollider normalTrigger;
- public BoxCollider highTrigger;
- public float crouchCameraX = 0.068f;
- public float crouchCameraY = 0.942f;
- public float crouchCameraZ = 0.407f;
- public float standCameraX = 0.04599719f;
- public float standCameraY = 1.734f;
- public float standCameraZ = 0.1539853f;
- private Vector3 originalMaleCameraPosition;
- private Vector3 crouchMaleCameraPosition;
- private const string ANIM_IS_WALKING = "isWalking";
- private const string ANIM_IS_RUNNING = "isRunning";
- private const string ANIM_IS_JUMPING = "isJumping";
- private const string ANIM_IS_CLIMBING = "isClimbing";
- private const string ANIM_IS_CROUCHING = "isCrouching";
- private const string ANIM_IS_SWIMMING = "isSwimming";
- private const string ANIM_IS_TREADING_WATER = "isTreadingWater";
- private const string ANIM_JUMP = "Jump";
- private const float ANIMATION_TRANSITION_SPEED = 0.1f;
- public Animator currentAnimator;
- public bool isMale = true;
- private bool isWalking;
- private bool isRunning;
- private bool isJumping;
- private bool isClimbing;
- private bool isCrouching;
- private bool isSwimming;
- private bool isTreadingWater;
- private Vector3 velocity;
- private bool isGrounded;
- private InteractionSystem interactionSystem;
- private InventorySystem inventorySystem;
- void Start()
- {
- controller = GetComponent<CharacterController>();
- interactionSystem = GetComponent<InteractionSystem>();
- inventorySystem = InventorySystem.Instance;
- UpdatePlayerReferences();
- originalMaleCameraPosition = new Vector3(standCameraX, standCameraY, standCameraZ);
- crouchMaleCameraPosition = new Vector3(crouchCameraX, crouchCameraY, crouchCameraZ);
- // Set initial camera position
- if (isMale)
- {
- maleCamera.localPosition = originalMaleCameraPosition;
- }
- }
- void Update()
- {
- HandleMovement();
- HandleInteractions();
- UpdateAnimatorParameters();
- UpdateCameraPosition();
- }
- void UpdatePlayerReferences()
- {
- currentAnimator = isMale ? maleAnimator : femaleAnimator;
- }
- void HandleMovement()
- {
- isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- if (isGrounded && velocity.y < 0)
- {
- velocity.y = -2f;
- }
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- Vector3 move = transform.right * x + transform.forward * z;
- controller.Move(move * speed * Time.deltaTime);
- bool wasWalking = isWalking;
- isWalking = move.magnitude > 0.1f && !isRunning;
- isRunning = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && move.magnitude > 0.1f;
- // Immediately update walking animation
- if (wasWalking != isWalking)
- {
- UpdateWalkingAnimation();
- }
- if (Input.GetButtonDown("Jump") && isGrounded)
- {
- StartCoroutine(JumpCoroutine());
- }
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime);
- bool wasCrouching = isCrouching;
- isCrouching = Input.GetKey(KeyCode.LeftControl);
- // Handle camera position change and animation when crouching state changes
- if (isCrouching != wasCrouching)
- {
- UpdateCameraPosition();
- UpdateCrouchingAnimation();
- }
- }
- private IEnumerator JumpCoroutine()
- {
- // Immediately stop any current animation and play the jump animation
- currentAnimator.Play(ANIM_JUMP, 0, 0f);
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- isJumping = true;
- yield return new WaitForSeconds(currentAnimator.GetCurrentAnimatorStateInfo(0).length);
- isJumping = false;
- }
- void HandleInteractions()
- {
- if (Input.GetMouseButtonDown(0))
- {
- interactionSystem.TryPickUpItem();
- }
- if (Input.GetKeyDown(KeyCode.G))
- {
- interactionSystem.DropItem(false);
- }
- if (Input.GetKeyDown(KeyCode.H))
- {
- interactionSystem.DropItem(true);
- }
- if (Input.GetKeyDown(KeyCode.F))
- {
- interactionSystem.DropBothItems();
- }
- }
- void UpdateAnimatorParameters()
- {
- currentAnimator.SetBool(ANIM_IS_RUNNING, isRunning);
- currentAnimator.SetBool(ANIM_IS_JUMPING, isJumping);
- currentAnimator.SetBool(ANIM_IS_CLIMBING, isClimbing);
- currentAnimator.SetBool(ANIM_IS_CROUCHING, isCrouching);
- currentAnimator.SetBool(ANIM_IS_SWIMMING, isSwimming);
- currentAnimator.SetBool(ANIM_IS_TREADING_WATER, isTreadingWater);
- }
- void UpdateWalkingAnimation()
- {
- if (isWalking)
- {
- currentAnimator.CrossFadeInFixedTime("Walking", ANIMATION_TRANSITION_SPEED);
- }
- else
- {
- currentAnimator.CrossFadeInFixedTime("Idle", ANIMATION_TRANSITION_SPEED);
- }
- }
- void UpdateCrouchingAnimation()
- {
- if (isCrouching)
- {
- currentAnimator.CrossFadeInFixedTime("Crouching Idle", ANIMATION_TRANSITION_SPEED);
- }
- else
- {
- currentAnimator.CrossFadeInFixedTime("Idle", ANIMATION_TRANSITION_SPEED);
- }
- }
- void UpdateCameraPosition()
- {
- if (isMale)
- {
- maleCamera.localPosition = isCrouching ? crouchMaleCameraPosition : originalMaleCameraPosition;
- }
- }
- public void UpdateCarryingAnimations()
- {
- bool leftHandItem = inventorySystem.leftHandItem != null;
- bool rightHandItem = inventorySystem.rightHandItem != null;
- bool twoHandedItem = leftHandItem && inventorySystem.leftHandItem.isTwoHanded;
- currentAnimator.SetBool("CarryingLeftHandItem", leftHandItem && !twoHandedItem);
- currentAnimator.SetBool("CarryingRightHandItem", rightHandItem && !twoHandedItem);
- currentAnimator.SetBool("CarryingTwoHandedItem", twoHandedItem);
- currentAnimator.SetBool("CarryingTwoItems", leftHandItem && rightHandItem && !twoHandedItem);
- }
- public string GetPickUpHeight(Vector3 itemPosition)
- {
- if (highTrigger.bounds.Contains(itemPosition))
- return "High Surface";
- else if (normalTrigger.bounds.Contains(itemPosition))
- return "Normal Surface";
- else if (groundTrigger.bounds.Contains(itemPosition))
- return "Ground";
- else
- return "Normal Surface"; // Default
- }
- public void PickUpItem(InteractableObject interactable)
- {
- if (interactionSystem != null)
- {
- interactionSystem.PickUpItem(interactable);
- }
- else
- {
- Debug.LogError("InteractionSystem is not initialized in PlayerMovement.");
- }
- }
- public void SetClimbingState(bool climbing)
- {
- isClimbing = climbing;
- }
- public void SetSwimmingState(bool swimming)
- {
- isSwimming = swimming;
- }
- public void SetTreadingWaterState(bool treadingWater)
- {
- isTreadingWater = treadingWater;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement