2
0

SimpleBikeController.cs 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class SimpleBikeController : MonoBehaviour
  6. {
  7. public WheelCollider[] steeringColliders;
  8. public WheelCollider[] motorColliders;
  9. public float factorTorque = 0.5f;
  10. public float factorSteer = 0.2f;
  11. public float maxMotorTorque = 100f;
  12. private float torque = 0f;
  13. private float steer = 0f;
  14. private void OnGUI()
  15. {
  16. GUI.Box(new Rect(10 ,10, 80, 40), $"Torque: {torque}\nSteer: {steer}");
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. torque = Input.GetAxis("Vertical") * factorTorque;
  22. steer = Input.GetAxis("Horizontal") * factorSteer;
  23. }
  24. private void FixedUpdate()
  25. {
  26. foreach (var c in steeringColliders)
  27. {
  28. c.steerAngle = steer;
  29. }
  30. foreach (var c in motorColliders)
  31. {
  32. c.motorTorque = Mathf.Clamp(torque, 0 ,maxMotorTorque);
  33. }
  34. }
  35. }