Advertisement
Guest User

movement

a guest
Oct 20th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class movement : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private float fallMultiplier = 3f;
  9. [SerializeField]
  10. private float lowJumpMultiplier = 2f;
  11. [SerializeField]
  12. private float gravityScale = 1f;
  13. [SerializeField]
  14. private float speed = 5f;
  15. Rigidbody PlayerRB;
  16. [Range(1,10)]
  17. private float jumpVelocity;
  18. [SerializeField]
  19. private float cooldown;
  20.  
  21. //groundcheck, I hope
  22. [SerializeField] LayerMask groundMask;
  23. List<Collider> groundTouchPoints = new List<Collider>();
  24.  
  25. void OnCollisionStay(Collision collision)
  26. {
  27. List<ContactPoint> contactPoints = new List<ContactPoint>();
  28. int numberOfContacts = collision.GetContacts(contactPoints);
  29. for (int i = 0; i < numberOfContacts; i++)
  30. {
  31. Collider collider = collision.collider;
  32. if (RoundedNormalVectorAngle(contactPoints[i].normal,3) <= 45 && CompareLayerIndex(collision.transform, groundMask) && !groundTouchPoints.Contains(collider))
  33. {
  34. groundTouchPoints.Add(collider);
  35. }
  36. else if (!IsStillTouchingGround(contactPoints,numberOfContacts) && groundTouchPoints.Contains(collider))
  37. {
  38. groundTouchPoints.Remove(collider);
  39. }
  40. }
  41. }
  42.  
  43. void OnCollisionExit(Collision collision)
  44. {
  45. Collider collider = collision.collider;
  46. if (groundTouchPoints.Contains(collider))
  47. {
  48. groundTouchPoints.Remove(collider);
  49. }
  50. }
  51.  
  52. private void Start()
  53. {
  54. PlayerRB = GetComponent<Rigidbody>();
  55. jumpVelocity = 10;
  56. }
  57. // Update is called once per frame
  58. void FixedUpdate()
  59. {
  60. //sprint
  61. if (Input.GetKey(KeyCode.LeftShift))
  62. {
  63. speed = 10f;
  64. }
  65. else
  66. {
  67. speed = 5f;
  68. }
  69. //movement
  70. float xMov = Input.GetAxisRaw("Horizontal");
  71. float zMov = Input.GetAxisRaw("Vertical");
  72.  
  73.  
  74. PlayerRB.velocity = new Vector3(xMov * speed, PlayerRB.velocity.y, zMov * speed);
  75.  
  76.  
  77.  
  78.  
  79. }
  80. private void Update()
  81. {
  82. //jump
  83. if (Input.GetKeyDown(KeyCode.Space) && groundTouchPoints.Count > 0)
  84. {
  85. PlayerRB.velocity = Vector3.up * jumpVelocity;
  86. }
  87. //fall
  88. if (PlayerRB.velocity.y < 0)
  89. {
  90. PlayerRB.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
  91. }
  92. else if(PlayerRB.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
  93. {
  94. PlayerRB.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
  95. }
  96. }
  97.  
  98. float RoundedNormalVectorAngle(Vector3 normal, uint decimalAccuracy)
  99. {
  100. int accuracy = (int)Mathf.Pow(10, decimalAccuracy);
  101. return Mathf.RoundToInt(Vector3.Angle(normal, Vector3.up)*accuracy)/accuracy;
  102. }
  103. bool CompareLayerIndex(Transform transform, LayerMask layer)
  104. {
  105. return Mathf.Pow(2, transform.gameObject.layer) == layer;
  106.  
  107. }
  108. bool IsStillTouchingGround(List<ContactPoint> contactPoints,int numberOfContacts)
  109. {
  110. for(int i = 0; i < numberOfContacts; i++)
  111. {
  112. if (RoundedNormalVectorAngle(contactPoints[i].normal, 3) <= 45)
  113. {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement