Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by ryandaniels230 · Apr 13, 2021 at 07:33 PM · wheelcollider

Controlling a wheelchair with wheel colliders

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>();
         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<Rigidbody>();
     }
 
     // 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

wheelchair.jpg (15.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

157 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I increase MotorTorque when entering a trigger collider (C#) 0 Answers

Wheel Collider problem 1 Answer

use Wheel Collider drive to a target position 0 Answers

braketorque making rigidbody sleep 1 Answer

distance between generated skidmark meshes and wheel colliders 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges