RB Script
RB Script
Collections;
2. using System.Collections.Generic;
3. using UnityEngine;
4. using UnityEngine.InputSystem;
5.
6. public class RigidbodyController : MonoBehaviour
7. {
8.
9. public float Speed = 5f;
10. public float JumpHeight = 2f;
11. public float GroundDistance = 0.2f;
12. public float DashDistance = 5f;
13. public LayerMask Ground;
14.
15. private Rigidbody rb;
16. private Vector3 playerInput = Vector3.zero;
17. private bool playerGrounded = true;
18. private Transform groundChecker;
19.
20. // Start is called before the first frame update
21. void Start()
22. {
23. rb = GetComponent<Rigidbody>();
24. groundChecker = transform.GetChild(0);
25. }
26.
27. // Update is called once per frame
28. void Update()
29. {
30. playerGrounded = Physics.CheckSphere(groundChecker.position,
GroundDistance, Ground, QueryTriggerInteraction.Ignore);
31.
32. playerInput = Vector3.zero;
33. playerInput.x = Input.GetAxis("Horizontal");
34. playerInput.z = Input.GetAxis("Vertical");
35. if (playerInput != Vector3.zero)
36. transform.forward = playerInput; //makes the player face forward towards
wherever they translate
37.
38. if (Input.GetButtonDown("Jump") && playerGrounded) //if the player is on the
floor when the jump button is pressed
39. {
40. rb.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y),
ForceMode.VelocityChange);
41. }
42.
43. if (Input.GetButtonDown("Dash"))
44. {
45. Vector3 dashVelocity = Vector3.Scale(transform.forward, DashDistance * new
Vector3((Mathf.Log(1f / (Time.deltaTime * rb.drag + 1)) / -Time.deltaTime), 0,
(Mathf.Log(1f / (Time.deltaTime * rb.drag + 1)) / -Time.deltaTime)));
46. rb.AddForce(dashVelocity, ForceMode.VelocityChange);
47. }
48. }
49.
50. void FixedUpdate()
51. {
52. rb.MovePosition(rb.position + playerInput * Speed * Time.fixedDeltaTime);
53. }
54. }
55.