Hi,
I have been stuck on trying to create a wheelchair with wheel colliders for a while now. I can get the wheelchair moving and turning but the wheelchair seems to spin very easily without much input, in fact, I am struggling to go in a straight line.
I have watched and read some resources on wheel colliders (mostly for cars) including:
https://www.edy.es/dev/2011/10/the-stabilizer-bars-creating-physically-realistic-stable-vehicles/ https://docs.unity3d.com/Manual/class-WheelCollider.html https://www.youtube.com/watch?v=j6_SMdWeGFI
and many more. I'm wondering if anyone has done this before and would be grateful for any advice. Please see the code snippets below: public class PlayerController : MonoBehaviour { private float horizontalInput; private float verticalInput; private float steerAngle; public WheelCollider BackleftWheelCollider; public WheelCollider BackrightWheelCollider; public Transform BackleftWheelT; public Transform BackrightWheelT; //public WheelCollider FrontleftWheelCollider; //public WheelCollider FrontrightWheelCollider; public Transform FrontleftWheelT; public Transform FrontrightWheelT; public float maxSteeringAngle; public float motorForce; private bool pressureAppliedToLeft = false; private bool pressureAppliedToRight = false; private new Rigidbody rigidbody; private void Start() { rigidbody = GetComponent();
rigidbody.centerOfMass = new Vector3(0, -0.4f, 0);
}
private void GetInput()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
}
private void Steer()
{
steerAngle = maxSteeringAngle * horizontalInput;
if (horizontalInput > 0)
{
pressureAppliedToRight = true;
pressureAppliedToLeft = false;
}
else if (horizontalInput < 0)
{
pressureAppliedToLeft = true;
pressureAppliedToRight = false;
}
else
{
pressureAppliedToRight = false;
pressureAppliedToLeft = false;
}
//BackleftWheelCollider.steerAngle = steerAngle;
//BackrightWheelCollider.steerAngle = steerAngle;
//FrontleftWheelCollider.steerAngle = steerAngle;
//FrontrightWheelCollider.steerAngle = steerAngle;
}
private void Accelerate()
{
if (pressureAppliedToRight)
{
BackleftWheelCollider.motorTorque = verticalInput * motorForce;
BackrightWheelCollider.motorTorque = 0;
//FrontleftWheelCollider.motorTorque = verticalInput * motorForce;
//FrontrightWheelCollider.motorTorque = 0;
}
else if (pressureAppliedToLeft)
{
BackrightWheelCollider.motorTorque = verticalInput * motorForce;
BackleftWheelCollider.motorTorque = 0;
//FrontrightWheelCollider.motorTorque = verticalInput * motorForce;
//FrontleftWheelCollider.motorTorque = 0;
}
else if (!pressureAppliedToLeft && !pressureAppliedToRight)
{
BackleftWheelCollider.motorTorque = verticalInput * motorForce;
BackrightWheelCollider.motorTorque = verticalInput * motorForce;
//FrontleftWheelCollider.motorTorque = verticalInput * motorForce;
//FrontrightWheelCollider.motorTorque = verticalInput * motorForce;
}
//FrontleftWheelCollider.motorTorque = verticalInput * motorForce;
//FrontrightWheelCollider.motorTorque = verticalInput * motorForce;
}
private void UpdateWheelPoses()
{
UpdateWheelPose(BackleftWheelCollider, BackleftWheelT);
UpdateWheelPose(BackrightWheelCollider, BackrightWheelT);
//UpdateWheelPose(FrontleftWheelCollider, FrontleftWheelT);
//UpdateWheelPose(FrontrightWheelCollider, FrontrightWheelT);
}
private void UpdateWheelPose(WheelCollider collider, Transform transform)
{
Vector3 pos = transform.position;
Quaternion quat = transform.rotation;
collider.GetWorldPose(out pos, out quat);
transform.position = pos;
transform.rotation = quat;
}
private void FixedUpdate()
{
GetInput();
Steer();
Accelerate();
UpdateWheelPoses();
}
}
and anti-roll-bar
public class AntiRollBar : MonoBehaviour
{
public WheelCollider WheelL;
public WheelCollider WheelR;
private Rigidbody carRigidBody;
public float AntiRoll = 5000.0f;
// Start is called before the first frame update
void Start()
{
carRigidBody = GetComponent();
}
// Update is called once per frame
void FixedUpdate()
{
WheelHit hit = new WheelHit();
float travelL = 1.0f;
float travelR = 1.0f;
bool groundedL = WheelL.GetGroundHit(out hit);
if (groundedL)
{
travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
}
bool groundedR = WheelR.GetGroundHit(out hit);
if (groundedR)
{
travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
}
var antiRollForce = (travelL - travelR) * AntiRoll;
if (groundedL)
{
carRigidBody.AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position);
}
if (groundedR)
{
carRigidBody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position);
}
}
}
and Hierarchy:
![alt text][1]
[1]: /storage/temp/179166-wheelchair.jpg
I have watched and read some resources on wheel colliders (mostly for cars) including:
https://www.edy.es/dev/2011/10/the-stabilizer-bars-creating-physically-realistic-stable-vehicles/ https://docs.unity3d.com/Manual/class-WheelCollider.html https://www.youtube.com/watch?v=j6_SMdWeGFI
and many more. I'm wondering if anyone has done this before and would be grateful for any advice. Please see the code snippets below: public class PlayerController : MonoBehaviour { private float horizontalInput; private float verticalInput; private float steerAngle; public WheelCollider BackleftWheelCollider; public WheelCollider BackrightWheelCollider; public Transform BackleftWheelT; public Transform BackrightWheelT; //public WheelCollider FrontleftWheelCollider; //public WheelCollider FrontrightWheelCollider; public Transform FrontleftWheelT; public Transform FrontrightWheelT; public float maxSteeringAngle; public float motorForce; private bool pressureAppliedToLeft = false; private bool pressureAppliedToRight = false; private new Rigidbody rigidbody; private void Start() { rigidbody = GetComponent