Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerMove : MonoBehaviour {
  4.  
  5. //Ustawienia ruchu
  6. public float movementSpeed;
  7. public float jumpForce;
  8. public bool allowMovementInAir;
  9.  
  10. //Krzywa, która opisuje zmianę prędkości ruchu w zależności od nachylenia podłoża. Jeżeli idziemy pod górkę kąt jest >90stopni
  11. //Z górki <90 stopni.
  12. public AnimationCurve slopeCurveModifier;
  13.  
  14.  
  15. //Wewnętrzne zmienne
  16. private Rigidbody rb;
  17. private Vector3 groundContactNormal;
  18.  
  19. [SerializeField]
  20. private bool isGrounded, previouslyGrounded, jumping;
  21.  
  22. void Start () {
  23. rb = GetComponent<Rigidbody>();
  24. }
  25.  
  26.  
  27. void Update () {
  28. GroundCheck();
  29. rb.drag = isGrounded ? 5f : 0f;
  30.  
  31. Vector2 movementInput = GetMovementInput();
  32. Vector3 forwardMovement = transform.forward * movementInput.y;
  33. Vector3 sideMovement = transform.right * movementInput.x;
  34.  
  35. if (isGrounded || allowMovementInAir)
  36. {
  37. //Dodajemy wyliczoną siłę wynikającą z inputu i mnożymy ją razy mnożnik z konta z podłożem oraz prędkość ruchu.
  38. rb.AddForce((forwardMovement + sideMovement) * movementSpeed * SlopeMultiplier());
  39. }
  40.  
  41. if (Input.GetButtonDown("Jump") && isGrounded && !jumping)
  42. {
  43. rb.AddForce(new Vector3(0f, jumpForce, 0f));
  44. jumping = true;
  45. }
  46. }
  47.  
  48. Vector2 GetMovementInput()
  49. {
  50. float x = Input.GetAxis("Horizontal");
  51. float y = Input.GetAxis("Vertical");
  52.  
  53. return new Vector2(x, y);
  54. }
  55.  
  56. private float SlopeMultiplier()
  57. {
  58. //Liczymy kąt pomiędzy graczem a podłożem - na tej podstawie wyliczamy mnożnik prędkości ruchu
  59. float angle = Vector3.Angle(groundContactNormal, transform.forward);
  60. return slopeCurveModifier.Evaluate(angle);
  61. }
  62.  
  63. private void GroundCheck()
  64. {
  65. RaycastHit hitInfo;
  66.  
  67. previouslyGrounded = isGrounded;
  68.  
  69. //Raycast do wyszukiwania podłoża na którym stoimy
  70. if(Physics.SphereCast(transform.position,
  71. GetComponent<CapsuleCollider>().radius,
  72. Vector3.down,
  73. out hitInfo,
  74. 1.02f,
  75. Physics.AllLayers,
  76. QueryTriggerInteraction.Ignore))
  77. {
  78. //Jeżeli na czymś stoimy ustawiamy isGrounded i pobieramy wektor normalny podłoża (wytłumaczyć dzieciom co to normalna)
  79. isGrounded = true;
  80. groundContactNormal = hitInfo.normal;
  81. }
  82. else
  83. {
  84. //W przeciwnym wypadku resetujemy isGrounded i ustawiamy normalną podłoża na wektor skierowany w górę
  85. isGrounded = false;
  86. groundContactNormal = Vector3.up;
  87. }
  88.  
  89. //Czy zakończyliśmy skok?
  90. if (!previouslyGrounded && isGrounded && jumping)
  91. {
  92. jumping = false;
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement