Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Player / CharacterMovement.cs
@Rackday Rackday on 21 Aug 2024 4 KB Project Added
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Animations;
  5. public class CharacterMovement : MonoBehaviour
  6. {
  7. private Rigidbody rb;
  8. [SerializeField]
  9. private Transform cameraTransform;
  10. [SerializeField]
  11. private int maxNumberOfJumps = 2;
  12. [HideInInspector]
  13. public int numberOfJumps { get; private set; } = 0;
  14. [SerializeField]
  15. private float GroundDis = 2f;
  16. [SerializeField]
  17. private float playerSpeed = 10f;
  18. private float noClipSpeed = 5f;
  19. [SerializeField]
  20. private float jumpForce = 1.0f;
  21. [SerializeField]
  22. private float gravityScale = 5f;
  23. [HideInInspector]
  24. public bool jump {get; private set;}
  25. private bool noClip;
  26. private ParticleSystem ps;
  27. private bool emitingParticles = false;
  28. //[SerializeField] private Animator animator;
  29. //Animations
  30. [HideInInspector]
  31. public bool canMove = true;
  32. void Start()
  33. {
  34. jump = false;
  35. noClip = false;
  36. rb = gameObject.GetComponent<Rigidbody>();
  37. ps = GetComponentInChildren<ParticleSystem>();
  38. }
  39. private void Update()
  40. {
  41. if ((Input.GetButtonDown("Jump") && OnGround() == true) || (Input.GetButtonDown("Jump") && numberOfJumps < maxNumberOfJumps))
  42. {
  43. jump = true;
  44. }
  45. if (OnGround()) numberOfJumps = 0;
  46. }
  47. // Update is called once per frame
  48. void FixedUpdate()
  49. {
  50. if (!PauseMenuBehaviour.isPaused)
  51. {
  52. float horizontal = (Input.GetAxis("Horizontal"));
  53. float vertical = (Input.GetAxis("Vertical"));
  54. //if(animator.GetFloat("WalkingFloat") != Mathf.Abs(horizontal) + Mathf.Abs(vertical))
  55. //animator.SetFloat("WalkingFloat", Mathf.Abs(horizontal)+Mathf.Abs(vertical));
  56. if (!noClip)
  57. {
  58. rb.velocity = (new Vector3(horizontal * playerSpeed, 0, vertical * playerSpeed) * Time.fixedDeltaTime) + new Vector3(0, rb.velocity.y, 0);
  59. rb.velocity = Quaternion.AngleAxis(cameraTransform.rotation.eulerAngles.y, Vector3.up) * rb.velocity;
  60. rb.velocity.Normalize();
  61. if (jump == true && numberOfJumps < maxNumberOfJumps)
  62. {
  63. //animator.SetTrigger("Jump");
  64. numberOfJumps++;
  65. jump = false;
  66. rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
  67. rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
  68. if (ps != null && emitingParticles)
  69. {
  70. ps.Play();
  71. Debug.Log("Particle");
  72. }
  73. }
  74. if (rb.useGravity)
  75. {
  76. rb.AddForce(gravityScale * Physics.gravity);
  77. }
  78. Vector3 moveVector = new Vector3(rb.velocity.normalized.x, 0, rb.velocity.normalized.z);
  79. transform.LookAt(transform.position + moveVector * 2f, Vector3.up);
  80. } else
  81. {
  82. if (horizontal != 0 || vertical != 0 || Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.LeftControl))
  83. {
  84. float jumpHeight = 0;
  85. if (Input.GetKey(KeyCode.Space))
  86. {
  87. jumpHeight = jumpForce;
  88. }
  89. if (Input.GetKey(KeyCode.LeftControl))
  90. {
  91. jumpHeight = -jumpForce;
  92. }
  93. Vector3 moveDir = Vector3.Normalize((transform.right * horizontal) + (transform.forward * vertical));
  94. //transform.LookAt(transform.position + moveDir);
  95. transform.position += new Vector3(moveDir.x * (noClipSpeed * Time.fixedDeltaTime),jumpHeight * Time.fixedDeltaTime ,moveDir.z * (noClipSpeed * Time.fixedDeltaTime));
  96. }
  97. }
  98. }
  99. }
  100. private bool OnGround()
  101. {
  102. RaycastHit hit;
  103. bool status = Physics.Raycast(transform.position, Vector3.down,out hit, GroundDis);
  104. if (hit.collider != null &&hit.collider.gameObject.CompareTag("Bridge"))
  105. {
  106. return false;
  107. }
  108. emitingParticles = status;
  109. return status;
  110. }
  111. public void SetNoClip(bool state)
  112. {
  113. rb.useGravity = !state;
  114. rb.isKinematic = state;
  115. noClip = state;
  116. }
  117. /* private void OnApplicationFocus(bool focus)
  118. {
  119. if (focus)
  120. {
  121. if (!PauseMenuBehaviour.isPaused)
  122. {
  123. Cursor.lockState = CursorLockMode.Locked;
  124. } else
  125. {
  126. Cursor.lockState = CursorLockMode.None;
  127. }
  128. }
  129. else
  130. {
  131. Cursor.lockState = CursorLockMode.None;
  132. }
  133. }*/
  134. }