Guest User

cod

a guest
Aug 31st, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4.  
  5. public class TireMovement : MonoBehaviour
  6. {
  7. [SerializeField] private float rollAccnFwd = 10f;
  8. [SerializeField] private float rollAccnRight = 10f;
  9. [SerializeField] private float rollMaxSpeed = 50f;
  10. [SerializeField] private Rigidbody rb;
  11.  
  12. private Vector3 moveInput = Vector3.zero;
  13.  
  14. private InputSystem_Actions inputActions;
  15.  
  16. void Start()
  17. {
  18. inputActions = new InputSystem_Actions();
  19. inputActions.Enable();
  20. }
  21.  
  22. void OnEnable()
  23. {
  24. inputActions = new InputSystem_Actions();
  25. inputActions.Enable();
  26. }
  27. void Update()
  28. {
  29. handleMovementInput();
  30. }
  31.  
  32. void FixedUpdate()
  33. {
  34. handleRoll();
  35. }
  36.  
  37. void handleMovementInput()
  38. {
  39. Vector2 move = inputActions.Player.Move.ReadValue<Vector2>();
  40. float x = move.x;
  41. float y = move.y * 0.5f;
  42. if (y < 0) y *= 0.5f;
  43.  
  44. moveInput = new Vector3(y, 0, x).normalized;
  45. }
  46.  
  47. void handleRoll()
  48. {
  49. // Add torque based on input
  50. if (moveInput == Vector3.zero) return;
  51.  
  52. if (Math.Abs(moveInput.x) > 0.01f)
  53. {
  54. //Forward force to actually move the tire
  55. rb.AddForce(transform.right * moveInput.x * rollAccnFwd, ForceMode.Force);
  56.  
  57. // Your original torque code - unchanged
  58. Vector3 right = new Vector3(transform.forward.x, 0, transform.forward.z).normalized;
  59. Vector3 torque = right * moveInput.x * rollAccnFwd;
  60.  
  61. rb.AddTorque(-torque, ForceMode.Force);
  62. }
  63. Quaternion steerRot = Quaternion.Euler(0f, moveInput.z * rollAccnRight, 0f);
  64.  
  65. rb.MoveRotation(rb.rotation * steerRot);
  66.  
  67. // Optional: clamp max angular velocity
  68. rb.maxAngularVelocity = rollMaxSpeed;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment