RbMovement.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class RbMovement : MonoBehaviour
  6. {
  7. private Transform t;
  8. public Transform bottom;
  9. private Rigidbody rb;
  10. // Start is called before the first frame update
  11. private float speedIncreasePerSecond = 2f;
  12. private float leanIncreasePerSecond = 10f;
  13. private float steerIncreasePerSecond = 15f;
  14. private float speed;
  15. private float lean;
  16. private float steer;
  17. void Start()
  18. {
  19. rb = GetComponent<Rigidbody>();
  20. t = transform;
  21. }
  22. private void Update()
  23. {
  24. if (Input.GetKey(KeyCode.W))
  25. {
  26. speed += speedIncreasePerSecond * Time.deltaTime;
  27. }
  28. else if (Input.GetKeyUp(KeyCode.W))
  29. {
  30. speed = 0;
  31. }
  32. if (Input.GetKey(KeyCode.S))
  33. {
  34. speed -= speedIncreasePerSecond * Time.deltaTime;
  35. }
  36. else if (Input.GetKeyUp(KeyCode.S))
  37. {
  38. speed = 0;
  39. }
  40. if (Input.GetKey(KeyCode.A))
  41. {
  42. steer += steerIncreasePerSecond * Time.deltaTime;
  43. }
  44. if (Input.GetKey(KeyCode.D))
  45. {
  46. steer -= steerIncreasePerSecond * Time.deltaTime;
  47. }
  48. if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
  49. {
  50. steer = 0;
  51. }
  52. if (Input.GetKey(KeyCode.Q))
  53. {
  54. lean -= leanIncreasePerSecond * Time.deltaTime;
  55. }
  56. if (Input.GetKey(KeyCode.E))
  57. {
  58. lean += leanIncreasePerSecond * Time.deltaTime;
  59. }
  60. if (Input.GetKeyUp(KeyCode.Q) || Input.GetKeyUp(KeyCode.E))
  61. {
  62. lean = 0f;
  63. }
  64. }
  65. // Update is called once per frame
  66. void FixedUpdate()
  67. {
  68. t.RotateAround(t.position, t.up, steer);
  69. t.RotateAround(bottom.position, t.forward, lean);
  70. rb.MovePosition(rb.position + t.forward * (Time.deltaTime * speed));
  71. }
  72. }