har5452

Untitled

Feb 15th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5.  
  6. [RequireComponent(typeof(AudioSource))]
  7. public class playermovement : MonoBehaviour
  8. {
  9. public float playerwalkspeed = 160f;
  10. public static playermovement staticMethodHelper;
  11. public float playerwalkspeeddef = 160f;
  12. public float playerrunspeed = 320f;
  13. public float jump = 850f;
  14. public static int health = 100;
  15. public static int AbleToJump = 1;
  16. public float airModifier = 0.4f;
  17.  
  18. private Vector3 moveDirection;
  19. private float verticalRotation = 0;
  20. private bool isStrafing;
  21. public bool isWalking;
  22. public bool bottomCollision = true;
  23. private float horizontalInput;
  24. private float verticalInput;
  25.  
  26. public Rigidbody m_Rigidbody;
  27. public Transform orientation;
  28. public Transform groundCheck;
  29. public LayerMask ground;
  30. public Image damageOverlay;
  31. public CameraShake cameraShake;
  32. public MotorcycleController motorcycle;
  33. private AudioSource source;
  34. public AudioClip footsteps;
  35. private Coroutine fadeCoroutine;
  36. private Coroutine handleUICoroutine;
  37. private float bobbingSpeed = 0.18f; // Adjust speed of bobbing
  38. private float bobbingAmount = 0.05f; // Adjust intensity of bobbing
  39. private float midpoint = 0f; // Default position
  40. private float timer = 0.0f;
  41.  
  42. void Awake()
  43. {
  44. source = GetComponent<AudioSource>();
  45. staticMethodHelper = this;
  46. }
  47.  
  48. void Start()
  49. {
  50. if (damageOverlay != null)
  51. damageOverlay.color = new Color(1, 0, 0, 0);
  52. }
  53.  
  54. void Update()
  55. {
  56. if (!MotorcycleController.isRiding) {
  57. HandleSpeed();
  58. HandleAudio();
  59. HandleMovementInput();
  60. HandleJump();
  61. }
  62. }
  63.  
  64.  
  65.  
  66. void FixedUpdate()
  67. {
  68. if (!MotorcycleController.isRiding) {
  69. CheckGroundStatus();
  70. ApplyMovement();
  71. LimitVelocity();
  72. ApplyGravity();
  73. }
  74.  
  75. HandleRotation();
  76. HeadBobbing();
  77. StrafeCam();
  78. Die();
  79.  
  80. if (MotorcycleController.isRiding) {
  81. TestUnmount();
  82. }
  83. }
  84.  
  85. private void HeadBobbing()
  86. {
  87. if (!isWalking || !bottomCollision)
  88. {
  89. // Reset camera position when not moving
  90. timer = 0;
  91. Camera.main.transform.localPosition = new Vector3(Camera.main.transform.localPosition.x, midpoint, Camera.main.transform.localPosition.z);
  92. return;
  93. }
  94.  
  95. float waveSlice = Mathf.Sin(timer);
  96. timer += bobbingSpeed * Time.deltaTime * (playerwalkspeed / playerwalkspeeddef); // Scale based on speed
  97.  
  98. if (timer > Mathf.PI * 2)
  99. {
  100. timer -= Mathf.PI * 2;
  101. }
  102.  
  103. float bobOffset = (waveSlice * bobbingAmount);
  104. Camera.main.transform.localPosition = new Vector3(Camera.main.transform.localPosition.x, midpoint + bobOffset, Camera.main.transform.localPosition.z);
  105. }
  106.  
  107.  
  108. private void HandleSpeed()
  109. {
  110. if (Input.GetKey(KeyCode.LeftShift))
  111. {
  112. playerrunspeed = (water.waterstatus == 1) ? 10f : 15f;
  113. playerwalkspeed = playerrunspeed;
  114. isStrafing = true;
  115. }
  116. else
  117. {
  118. playerwalkspeeddef = (water.waterstatus == 1) ? 5f : 10f;
  119. playerwalkspeed = playerwalkspeeddef;
  120. isStrafing = false;
  121. }
  122. }
  123.  
  124. private void TestUnmount() {
  125. if (Input.GetKeyDown(KeyCode.E))
  126. {
  127. print("dismount");
  128. motorcycle.Dismount();
  129. }
  130. }
  131.  
  132. private void HandleAudio()
  133. {
  134. source.enabled = isWalking && bottomCollision;
  135. }
  136.  
  137. private void HandleMovementInput()
  138. {
  139. horizontalInput = Input.GetAxisRaw("Horizontal");
  140. verticalInput = Input.GetAxisRaw("Vertical");
  141. moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
  142. }
  143.  
  144. private void HandleRotation()
  145. {
  146. float horRot = Input.GetAxis("Mouse X");
  147. Quaternion deltaRotation = Quaternion.Euler(new Vector3(0, horRot * 128, 0) * Time.fixedDeltaTime);
  148. m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
  149. if (!MotorcycleController.isRiding) {
  150. verticalRotation -= Input.GetAxis("Mouse Y");
  151. verticalRotation = Mathf.Clamp(verticalRotation, -20, 20);
  152. }
  153.  
  154. if (MotorcycleController.isRiding) {
  155. verticalRotation -= Input.GetAxis("Mouse Y");
  156. verticalRotation = Mathf.Clamp(verticalRotation, -10, 5);
  157. }
  158.  
  159. Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation * 4, 0, 0);
  160. if (MotorcycleController.isRiding)
  161. {
  162. if (verticalRotation > 0)
  163. {
  164. if (handleUICoroutine != null) StopCoroutine(handleUICoroutine);
  165. handleUICoroutine = StartCoroutine(MoveHandleUI(Vector3.zero, 1f)); // Move up and become visible
  166. }
  167. else
  168. {
  169. if (handleUICoroutine != null) StopCoroutine(handleUICoroutine);
  170. handleUICoroutine = StartCoroutine(MoveHandleUI(new Vector3(0, -50, 0), 0f)); // Move down and fade out
  171. }
  172. }
  173. }
  174.  
  175. IEnumerator MoveHandleUI(Vector3 targetPos, float targetAlpha)
  176. {
  177. RectTransform handleTransform = motorcycle.handleUI.GetComponent<RectTransform>();
  178. CanvasGroup handleCanvasGroup = motorcycle.handleUI.GetComponent<CanvasGroup>();
  179.  
  180. if (handleCanvasGroup == null)
  181. {
  182. handleCanvasGroup = motorcycle.handleUI.gameObject.AddComponent<CanvasGroup>();
  183. }
  184.  
  185. Vector3 startPos = handleTransform.anchoredPosition;
  186. float startAlpha = handleCanvasGroup.alpha;
  187. float duration = 0.1f;
  188. float elapsed = 0f;
  189.  
  190. while (elapsed < duration)
  191. {
  192. elapsed += Time.deltaTime;
  193. float t = elapsed / duration;
  194.  
  195. handleTransform.anchoredPosition = Vector3.Lerp(startPos, targetPos, t);
  196. handleCanvasGroup.alpha = Mathf.Lerp(startAlpha, targetAlpha, t);
  197.  
  198. yield return null;
  199. }
  200.  
  201. handleTransform.anchoredPosition = targetPos;
  202. handleCanvasGroup.alpha = targetAlpha;
  203. }
  204.  
  205. private void HandleJump()
  206. {
  207. if (Input.GetKeyDown(KeyCode.Space) && isGrounded() && AbleToJump == 1)
  208. {
  209. bottomCollision = true;
  210. m_Rigidbody.AddForce(Vector3.up * jump);
  211. m_Rigidbody.drag = 0;
  212. }
  213. else if (!isGrounded())
  214. {
  215. bottomCollision = false;
  216. }
  217. }
  218.  
  219. private void ApplyMovement()
  220. {
  221. if (bottomCollision)
  222. {
  223. m_Rigidbody.AddForce(moveDirection.normalized * playerwalkspeed * 10f, ForceMode.Force);
  224. m_Rigidbody.drag = 2;
  225. }
  226. else
  227. {
  228. m_Rigidbody.AddForce(moveDirection.normalized * playerwalkspeed * 10f * airModifier, ForceMode.Force);
  229. }
  230. }
  231.  
  232. private void LimitVelocity()
  233. {
  234. Vector3 flatVel = new Vector3(m_Rigidbody.velocity.x, 0f, m_Rigidbody.velocity.z);
  235.  
  236. if (flatVel.magnitude > playerwalkspeed)
  237. {
  238. Vector3 limitedVel = flatVel.normalized * playerwalkspeed;
  239. m_Rigidbody.velocity = new Vector3(limitedVel.x, m_Rigidbody.velocity.y, limitedVel.z);
  240. isWalking = true;
  241. }
  242. else
  243. {
  244. isWalking = false;
  245. }
  246. }
  247.  
  248. private void ApplyGravity()
  249. {
  250. if (water.waterstatus == 1)
  251. {
  252. Physics.gravity = new Vector3(0, -5.0F, 0);
  253. playerwalkspeed = 5;
  254. playerwalkspeeddef = 5;
  255. playerrunspeed = 10f;
  256. }
  257. else
  258. {
  259. Physics.gravity = new Vector3(0, -35.0F, 0);
  260. }
  261.  
  262. m_Rigidbody.mass = 1f;
  263. }
  264.  
  265. private void CheckGroundStatus()
  266. {
  267. bottomCollision = Physics.Raycast(transform.position, Vector3.down, 10);
  268. }
  269.  
  270. private bool isGrounded()
  271. {
  272. return Physics.CheckSphere(groundCheck.position, 0.29f, ground);
  273. }
  274.  
  275. private void OnDrawGizmos()
  276. {
  277. Gizmos.color = Color.green;
  278. Gizmos.DrawSphere(groundCheck.position, 0.1f);
  279. }
  280.  
  281. public static void Damage(int damage)
  282. {
  283. health -= damage;
  284. staticMethodHelper.ShakeCamera();
  285. staticMethodHelper.FadeRedScreen();
  286. }
  287.  
  288. public void ShakeCamera()
  289. {
  290. if (cameraShake != null)
  291. cameraShake.TriggerShake(0.1f);
  292. }
  293.  
  294. public void FadeRedScreen()
  295. {
  296. if (damageOverlay != null)
  297. {
  298. if (fadeCoroutine != null) StopCoroutine(fadeCoroutine);
  299. fadeCoroutine = StartCoroutine(FadeToTransparent());
  300. }
  301. }
  302.  
  303.  
  304.  
  305. public void StrafeCam() {
  306. if (Input.GetAxis("Horizontal") > 0 && isStrafing == true) {
  307. Vector3 startPosition = new Vector3(0,0,0);
  308. Vector3 finalPosition = new Vector3(0,0,-50F);
  309. Vector3 smoothedPosition = Vector3.Slerp(startPosition, finalPosition,1 - Mathf.Pow(0.7F, Time.deltaTime));
  310. Camera.main.transform.Rotate(smoothedPosition);
  311. }
  312.  
  313. if (Input.GetAxis("Horizontal") < 0 && isStrafing == true) {
  314. Vector3 startPosition = new Vector3(0,0,0);
  315. Vector3 finalPosition = new Vector3(0,0,50F);
  316. Vector3 smoothedPosition = Vector3.Slerp(startPosition, finalPosition,1 - Mathf.Pow(0.7F, Time.deltaTime));
  317. Camera.main.transform.Rotate(smoothedPosition);
  318. }
  319. }
  320.  
  321. private void Die() {
  322. if (health <= 0) {
  323. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  324. health = 100;
  325. }
  326. }
  327.  
  328.  
  329. IEnumerator FadeToTransparent()
  330. {
  331. damageOverlay.color = new Color(1, 0, 0, 0.5f);
  332. yield return new WaitForSeconds(0.1f);
  333.  
  334. float duration = 0.2f;
  335. float elapsed = 0f;
  336.  
  337. while (elapsed < duration)
  338. {
  339. elapsed += Time.deltaTime;
  340. float alpha = Mathf.Lerp(0.5f, 0f, elapsed / duration);
  341. damageOverlay.color = new Color(1, 0, 0, alpha);
  342. yield return null;
  343. }
  344.  
  345. damageOverlay.color = new Color(1, 0, 0, 0);
  346. }
  347. }
  348.  
Advertisement
Add Comment
Please, Sign In to add comment