Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- [RequireComponent(typeof(AudioSource))]
- public class playermovement : MonoBehaviour
- {
- public float playerwalkspeed = 160f;
- public static playermovement staticMethodHelper;
- public float playerwalkspeeddef = 160f;
- public float playerrunspeed = 320f;
- public float jump = 850f;
- public static int health = 100;
- public static int AbleToJump = 1;
- public float airModifier = 0.4f;
- private Vector3 moveDirection;
- private float verticalRotation = 0;
- private bool isStrafing;
- public bool isWalking;
- public bool bottomCollision = true;
- private float horizontalInput;
- private float verticalInput;
- public Rigidbody m_Rigidbody;
- public Transform orientation;
- public Transform groundCheck;
- public LayerMask ground;
- public Image damageOverlay;
- public CameraShake cameraShake;
- public MotorcycleController motorcycle;
- private AudioSource source;
- public AudioClip footsteps;
- private Coroutine fadeCoroutine;
- private Coroutine handleUICoroutine;
- private float bobbingSpeed = 0.18f; // Adjust speed of bobbing
- private float bobbingAmount = 0.05f; // Adjust intensity of bobbing
- private float midpoint = 0f; // Default position
- private float timer = 0.0f;
- void Awake()
- {
- source = GetComponent<AudioSource>();
- staticMethodHelper = this;
- }
- void Start()
- {
- if (damageOverlay != null)
- damageOverlay.color = new Color(1, 0, 0, 0);
- }
- void Update()
- {
- if (!MotorcycleController.isRiding) {
- HandleSpeed();
- HandleAudio();
- HandleMovementInput();
- HandleJump();
- }
- }
- void FixedUpdate()
- {
- if (!MotorcycleController.isRiding) {
- CheckGroundStatus();
- ApplyMovement();
- LimitVelocity();
- ApplyGravity();
- }
- HandleRotation();
- HeadBobbing();
- StrafeCam();
- Die();
- if (MotorcycleController.isRiding) {
- TestUnmount();
- }
- }
- private void HeadBobbing()
- {
- if (!isWalking || !bottomCollision)
- {
- // Reset camera position when not moving
- timer = 0;
- Camera.main.transform.localPosition = new Vector3(Camera.main.transform.localPosition.x, midpoint, Camera.main.transform.localPosition.z);
- return;
- }
- float waveSlice = Mathf.Sin(timer);
- timer += bobbingSpeed * Time.deltaTime * (playerwalkspeed / playerwalkspeeddef); // Scale based on speed
- if (timer > Mathf.PI * 2)
- {
- timer -= Mathf.PI * 2;
- }
- float bobOffset = (waveSlice * bobbingAmount);
- Camera.main.transform.localPosition = new Vector3(Camera.main.transform.localPosition.x, midpoint + bobOffset, Camera.main.transform.localPosition.z);
- }
- private void HandleSpeed()
- {
- if (Input.GetKey(KeyCode.LeftShift))
- {
- playerrunspeed = (water.waterstatus == 1) ? 10f : 15f;
- playerwalkspeed = playerrunspeed;
- isStrafing = true;
- }
- else
- {
- playerwalkspeeddef = (water.waterstatus == 1) ? 5f : 10f;
- playerwalkspeed = playerwalkspeeddef;
- isStrafing = false;
- }
- }
- private void TestUnmount() {
- if (Input.GetKeyDown(KeyCode.E))
- {
- print("dismount");
- motorcycle.Dismount();
- }
- }
- private void HandleAudio()
- {
- source.enabled = isWalking && bottomCollision;
- }
- private void HandleMovementInput()
- {
- horizontalInput = Input.GetAxisRaw("Horizontal");
- verticalInput = Input.GetAxisRaw("Vertical");
- moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
- }
- private void HandleRotation()
- {
- float horRot = Input.GetAxis("Mouse X");
- Quaternion deltaRotation = Quaternion.Euler(new Vector3(0, horRot * 128, 0) * Time.fixedDeltaTime);
- m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
- if (!MotorcycleController.isRiding) {
- verticalRotation -= Input.GetAxis("Mouse Y");
- verticalRotation = Mathf.Clamp(verticalRotation, -20, 20);
- }
- if (MotorcycleController.isRiding) {
- verticalRotation -= Input.GetAxis("Mouse Y");
- verticalRotation = Mathf.Clamp(verticalRotation, -10, 5);
- }
- Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation * 4, 0, 0);
- if (MotorcycleController.isRiding)
- {
- if (verticalRotation > 0)
- {
- if (handleUICoroutine != null) StopCoroutine(handleUICoroutine);
- handleUICoroutine = StartCoroutine(MoveHandleUI(Vector3.zero, 1f)); // Move up and become visible
- }
- else
- {
- if (handleUICoroutine != null) StopCoroutine(handleUICoroutine);
- handleUICoroutine = StartCoroutine(MoveHandleUI(new Vector3(0, -50, 0), 0f)); // Move down and fade out
- }
- }
- }
- IEnumerator MoveHandleUI(Vector3 targetPos, float targetAlpha)
- {
- RectTransform handleTransform = motorcycle.handleUI.GetComponent<RectTransform>();
- CanvasGroup handleCanvasGroup = motorcycle.handleUI.GetComponent<CanvasGroup>();
- if (handleCanvasGroup == null)
- {
- handleCanvasGroup = motorcycle.handleUI.gameObject.AddComponent<CanvasGroup>();
- }
- Vector3 startPos = handleTransform.anchoredPosition;
- float startAlpha = handleCanvasGroup.alpha;
- float duration = 0.1f;
- float elapsed = 0f;
- while (elapsed < duration)
- {
- elapsed += Time.deltaTime;
- float t = elapsed / duration;
- handleTransform.anchoredPosition = Vector3.Lerp(startPos, targetPos, t);
- handleCanvasGroup.alpha = Mathf.Lerp(startAlpha, targetAlpha, t);
- yield return null;
- }
- handleTransform.anchoredPosition = targetPos;
- handleCanvasGroup.alpha = targetAlpha;
- }
- private void HandleJump()
- {
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded() && AbleToJump == 1)
- {
- bottomCollision = true;
- m_Rigidbody.AddForce(Vector3.up * jump);
- m_Rigidbody.drag = 0;
- }
- else if (!isGrounded())
- {
- bottomCollision = false;
- }
- }
- private void ApplyMovement()
- {
- if (bottomCollision)
- {
- m_Rigidbody.AddForce(moveDirection.normalized * playerwalkspeed * 10f, ForceMode.Force);
- m_Rigidbody.drag = 2;
- }
- else
- {
- m_Rigidbody.AddForce(moveDirection.normalized * playerwalkspeed * 10f * airModifier, ForceMode.Force);
- }
- }
- private void LimitVelocity()
- {
- Vector3 flatVel = new Vector3(m_Rigidbody.velocity.x, 0f, m_Rigidbody.velocity.z);
- if (flatVel.magnitude > playerwalkspeed)
- {
- Vector3 limitedVel = flatVel.normalized * playerwalkspeed;
- m_Rigidbody.velocity = new Vector3(limitedVel.x, m_Rigidbody.velocity.y, limitedVel.z);
- isWalking = true;
- }
- else
- {
- isWalking = false;
- }
- }
- private void ApplyGravity()
- {
- if (water.waterstatus == 1)
- {
- Physics.gravity = new Vector3(0, -5.0F, 0);
- playerwalkspeed = 5;
- playerwalkspeeddef = 5;
- playerrunspeed = 10f;
- }
- else
- {
- Physics.gravity = new Vector3(0, -35.0F, 0);
- }
- m_Rigidbody.mass = 1f;
- }
- private void CheckGroundStatus()
- {
- bottomCollision = Physics.Raycast(transform.position, Vector3.down, 10);
- }
- private bool isGrounded()
- {
- return Physics.CheckSphere(groundCheck.position, 0.29f, ground);
- }
- private void OnDrawGizmos()
- {
- Gizmos.color = Color.green;
- Gizmos.DrawSphere(groundCheck.position, 0.1f);
- }
- public static void Damage(int damage)
- {
- health -= damage;
- staticMethodHelper.ShakeCamera();
- staticMethodHelper.FadeRedScreen();
- }
- public void ShakeCamera()
- {
- if (cameraShake != null)
- cameraShake.TriggerShake(0.1f);
- }
- public void FadeRedScreen()
- {
- if (damageOverlay != null)
- {
- if (fadeCoroutine != null) StopCoroutine(fadeCoroutine);
- fadeCoroutine = StartCoroutine(FadeToTransparent());
- }
- }
- public void StrafeCam() {
- if (Input.GetAxis("Horizontal") > 0 && isStrafing == true) {
- Vector3 startPosition = new Vector3(0,0,0);
- Vector3 finalPosition = new Vector3(0,0,-50F);
- Vector3 smoothedPosition = Vector3.Slerp(startPosition, finalPosition,1 - Mathf.Pow(0.7F, Time.deltaTime));
- Camera.main.transform.Rotate(smoothedPosition);
- }
- if (Input.GetAxis("Horizontal") < 0 && isStrafing == true) {
- Vector3 startPosition = new Vector3(0,0,0);
- Vector3 finalPosition = new Vector3(0,0,50F);
- Vector3 smoothedPosition = Vector3.Slerp(startPosition, finalPosition,1 - Mathf.Pow(0.7F, Time.deltaTime));
- Camera.main.transform.Rotate(smoothedPosition);
- }
- }
- private void Die() {
- if (health <= 0) {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- health = 100;
- }
- }
- IEnumerator FadeToTransparent()
- {
- damageOverlay.color = new Color(1, 0, 0, 0.5f);
- yield return new WaitForSeconds(0.1f);
- float duration = 0.2f;
- float elapsed = 0f;
- while (elapsed < duration)
- {
- elapsed += Time.deltaTime;
- float alpha = Mathf.Lerp(0.5f, 0f, elapsed / duration);
- damageOverlay.color = new Color(1, 0, 0, alpha);
- yield return null;
- }
- damageOverlay.color = new Color(1, 0, 0, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment