Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class TireMovement : MonoBehaviour
- {
- [SerializeField] private float rollAccnFwd = 10f;
- [SerializeField] private float rollAccnRight = 10f;
- [SerializeField] private float rollMaxSpeed = 50f;
- [SerializeField] private Rigidbody rb;
- private Vector3 moveInput = Vector3.zero;
- private InputSystem_Actions inputActions;
- void Start()
- {
- inputActions = new InputSystem_Actions();
- inputActions.Enable();
- }
- void OnEnable()
- {
- inputActions = new InputSystem_Actions();
- inputActions.Enable();
- }
- void Update()
- {
- handleMovementInput();
- }
- void FixedUpdate()
- {
- handleRoll();
- }
- void handleMovementInput()
- {
- Vector2 move = inputActions.Player.Move.ReadValue<Vector2>();
- float x = move.x;
- float y = move.y * 0.5f;
- if (y < 0) y *= 0.5f;
- moveInput = new Vector3(y, 0, x).normalized;
- }
- void handleRoll()
- {
- // Add torque based on input
- if (moveInput == Vector3.zero) return;
- if (Math.Abs(moveInput.x) > 0.01f)
- {
- //Forward force to actually move the tire
- rb.AddForce(transform.right * moveInput.x * rollAccnFwd, ForceMode.Force);
- // Your original torque code - unchanged
- Vector3 right = new Vector3(transform.forward.x, 0, transform.forward.z).normalized;
- Vector3 torque = right * moveInput.x * rollAccnFwd;
- rb.AddTorque(-torque, ForceMode.Force);
- }
- Quaternion steerRot = Quaternion.Euler(0f, moveInput.z * rollAccnRight, 0f);
- rb.MoveRotation(rb.rotation * steerRot);
- // Optional: clamp max angular velocity
- rb.maxAngularVelocity = rollMaxSpeed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment