Last active
May 27, 2025 04:00
-
-
Save st4rdog/9948b72d2902533475d3d03321985af9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created from http://docs.unity3d.com/Manual/WheelColliderTutorial.html | |
// For EasySuspension with BoxyCarWizard.cs see https://x.com/anthonyyakovlev/status/585060413370400768 | |
// - Tutorial https://www.youtube.com/watch?v=xQcJAa6Ooa4 | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
[System.Serializable] | |
public class AxleInfo | |
{ | |
[Header("Settings")] | |
public bool motor; | |
public bool steering; | |
public bool hasBrakes; | |
[Header("References")] | |
public WheelCollider leftWheel; | |
public WheelCollider rightWheel; | |
} | |
public class CarS : MonoBehaviour { | |
[Header("Info")] | |
public float currentSpeed; | |
public Vector3 currentVelocity; | |
[Header("Settings")] | |
public List<AxleInfo> axleInfos; | |
public float maxMotorTorque = 400f; | |
public float maxSteeringAngle = 30f; | |
public float currentBrakeTorque = 0f; | |
// COMPONENTS | |
private Rigidbody rb; | |
//============================================ | |
// FUNCTIONS (UNITY) | |
//============================================ | |
void Awake() | |
{ | |
rb = GetComponent<Rigidbody>(); | |
} | |
void Update() | |
{ | |
// BRAKE | |
if (Input.GetButton("Brake")) | |
{ | |
currentBrakeTorque = 10000f; | |
} | |
else | |
{ | |
currentBrakeTorque = 0f; | |
} | |
// JUMP | |
if (Input.GetButtonDown("Jump")) | |
{ | |
rb.AddForce(rb.centerOfMass + new Vector3(0f, 10000f, 0f), ForceMode.Impulse); | |
} | |
// BOOST | |
if (Input.GetButton("Boost")) | |
{ | |
rb.AddForce(transform.forward * 1000f, ForceMode.Impulse); | |
} | |
} | |
void FixedUpdate() | |
{ | |
// SPEED | |
currentVelocity = rb.velocity; | |
currentSpeed = rb.velocity.magnitude; | |
// INPUT | |
float motor = maxMotorTorque * Input.GetAxis("Accelerator"); | |
float steering = maxSteeringAngle * Input.GetAxis("Horizontal"); | |
foreach (AxleInfo axleInfo in axleInfos) | |
{ | |
if (axleInfo.steering) | |
{ | |
axleInfo.leftWheel.steerAngle = steering; | |
axleInfo.rightWheel.steerAngle = steering; | |
} | |
if (axleInfo.motor) | |
{ | |
axleInfo.leftWheel.motorTorque = motor; | |
axleInfo.rightWheel.motorTorque = motor; | |
} | |
if (axleInfo.hasBrakes) | |
{ | |
axleInfo.leftWheel.brakeTorque = currentBrakeTorque; | |
axleInfo.rightWheel.brakeTorque = currentBrakeTorque; | |
} | |
ApplyLocalPositionToVisuals(axleInfo.leftWheel); | |
ApplyLocalPositionToVisuals(axleInfo.rightWheel); | |
} | |
} | |
//============================================ | |
// FUNCTIONS (CUSTOM) | |
//============================================ | |
// FINDS THE VISUAL WHEEL, CORRECTLY APPLIES THE TRANSFORM | |
public void ApplyLocalPositionToVisuals(WheelCollider collider) | |
{ | |
if (collider.transform.childCount == 0) return; | |
// GET WHEEL | |
Transform visualWheel = collider.transform.GetChild(0); | |
// GET POS/ROT | |
Vector3 position; | |
Quaternion rotation; | |
collider.GetWorldPose(out position, out rotation); | |
// APPLY POS/ROT | |
visualWheel.transform.position = position; | |
visualWheel.transform.rotation = rotation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment