0% found this document useful (0 votes)
63 views

RB Script

This C# script controls the rigidbody of a character in a 3D game. It gets player input to move the character forward, backward, left and right. It checks if the character is grounded to allow jumping. When the jump button is pressed and the character is grounded, a force is applied to the rigidbody to jump. It also allows dashing by applying a force in the character's forward direction when the dash button is pressed. The rigidbody is moved each fixed update based on the player's input vector and speed.

Uploaded by

api-534839865
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

RB Script

This C# script controls the rigidbody of a character in a 3D game. It gets player input to move the character forward, backward, left and right. It checks if the character is grounded to allow jumping. When the jump button is pressed and the character is grounded, a force is applied to the rigidbody to jump. It also allows dashing by applying a force in the character's forward direction when the dash button is pressed. The rigidbody is moved each fixed update based on the player's input vector and speed.

Uploaded by

api-534839865
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. using System.

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.

You might also like