SphereBikeController.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class SphereBikeController : MonoBehaviour
  6. {
  7. public GameObject frontCollider;
  8. public GameObject rearCollider;
  9. public float factorRps = 0.5f;
  10. public float factorSteer = 0.2f;
  11. public float maxRps = 4f;
  12. private Transform frontTransform;
  13. private Transform rearTransform;
  14. public GameObject bike;
  15. private Rigidbody rb;
  16. private Transform bt;
  17. private float rps = 0f;
  18. private float steer = 0f;
  19. private void Start()
  20. {
  21. frontTransform = frontCollider.transform;
  22. rearTransform = rearCollider.transform;
  23. bt = bike.transform;
  24. rb = bike.GetComponent<Rigidbody>();
  25. }
  26. private void OnGUI()
  27. {
  28. GUI.Box(new Rect(10, 10, 80, 40), $"Rps: {rps}\nSteer: {steer}");
  29. }
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. rps = Input.GetAxis("Vertical") * factorRps;
  34. steer = Input.GetAxis("Horizontal") * factorSteer;
  35. }
  36. private void FixedUpdate()
  37. {
  38. rb.velocity = bt.forward * 2;
  39. rearTransform.RotateAround(rearTransform.position, rearTransform.right, rps*Time.fixedDeltaTime*360f);
  40. }
  41. }