Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by Mr-Mechanical · Jul 15, 2015 at 08:46 PM · suspension

Is it possible to make Solid-axle suspension with WheelColliders + Joint?

I want to make a solid-axle suspension system using joints in unity. Is this possible? If you don't know what a solid-axle is, check out this wiki article: https://en.wikipedia.org/wiki/Beam_axle

Thanks in advanced! :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LemonMontage420 · Feb 06, 2021 at 08:40 AM

I know this is a REALLY late response, but if OP or anyone else has trouble with different types of suspensions and/or needs solid axle suspension, I've created these two scripts that have really great results for both independent and dependent (solid axle) suspension setups.`using System.Collections; using System.Collections.Generic; using UnityEngine;

public class New_Physics_Wheel : MonoBehaviour { [SerializeField] public enum SuspensionType { IndependentSuspension, SolidAxleSuspension }; public SuspensionType suspensionType; private Rigidbody rb; public RaycastHit hit; public float wheelRadius; public float wheelMass; public float restLength; public float springTravel; public float suspensionStiffness; public float suspensionDamper; public bool isGrounded; public float springForce; public float springLength; private float maxLength; private float minLength; private float lastLength; private float springVelocity; private Vector3 suspensionForce; private float damperForce;

 public Transform solidAxle;
 private New_Physics_SolidAxle _solidAxle;

 // Start is called before the first frame update
 void Awake()
 {
     rb = transform.root.GetComponent<Rigidbody>();
     if (transform.parent == solidAxle)
     {
         _solidAxle = solidAxle.GetComponent<New_Physics_SolidAxle>();
     }
     else
     {
         _solidAxle = null;
     }
     springLength = restLength;
     maxLength = restLength + springTravel;
     minLength = restLength - springLength;
 }

 // Update is called once per frame
 void FixedUpdate()
 {
     if (Physics.Raycast(transform.position, -transform.up, out hit, maxLength + wheelRadius))
     {
         isGrounded = true;
     }
     else
     {
         isGrounded = false;
         springLength = maxLength;
     }
     if (isGrounded)
     {
         SuspensionForces();
     }
 }

 void SuspensionForces()
 {
     lastLength = springLength;
     springLength = hit.distance - wheelRadius;

     springVelocity = (lastLength - springLength) / Time.fixedDeltaTime;


     springForce = suspensionStiffness * (restLength - springLength);
     damperForce = suspensionDamper * springVelocity;

     if (suspensionType == SuspensionType.SolidAxleSuspension)
     {
         if (transform.parent == solidAxle)
         {
             if (transform.name == "Wheel_RL")
             {
                 solidAxleSuspensionForce = _solidAxle.solidAxleForce;
             }
             if (transform.name == "Wheel_RR")
             {
                 solidAxleSuspensionForce = -_solidAxle.solidAxleForce;
             }
         }
         suspensionForce = (springForce + damperForce + solidAxleSuspensionForce) * transform.up;

     }
     else if (suspensionType == SuspensionType.IndependentSuspension)
     {
         suspensionForce = (springForce + damperForce) * transform.up;
     }
     
     rb.AddForceAtPosition(suspensionForce, hit.point);
 }

 void LateUpdate()
 {
     Debug.DrawRay(new Vector3(transform.position.x, transform.position.y - springLength, transform.position.z), new Vector3(0, -wheelRadius, 0), Color.red, 0, false);
     springLength = Mathf.Clamp(springLength, minLength, maxLength);
 }

} ` and:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class New_Physics_SolidAxle : MonoBehaviour
 {
     private New_Physics_Wheel[] wheels;
     public float currentSpringDifference;
     public AnimationCurve test;
     public float solidAxleForce;
 
     // Start is called before the first frame update
     void Awake()
     {
         wheels = GetComponentsInChildren<New_Physics_Wheel>();
     }
 
     // Update is called once per frame
     void Update()
     {
         currentSpringDifference = Mathf.Abs(wheels[1].springLength) - Mathf.Abs(wheels[0].springLength);
         for (int i = 0; i < wheels.Length; i++)
         {
             solidAxleForce = test.Evaluate(currentSpringDifference) * wheels[i].maxSolidAxleForce;
         }
     }
 
     private void LateUpdate()
     {
         currentSpringDifference = Mathf.Clamp(currentSpringDifference, 0, (wheels[0].restLength + wheels[0].springTravel) - (wheels[0].restLength - wheels[0].springTravel));
     }
 }

This setup should work with any vehicle with an even number of wheels (excluding 2), as I've tried 4, 6, and 8 wheel setups with different suspension types per axle with success. Just place an object in the middle of an axle you wish to convert from independent to solid axle, parent the two wheels on that axle to it, give it the "New_Physics_SolidAxle" script, and make two keyframes in the animation curve, one with a time of whatever the (max spring length - minspring length) value is, and another with the min spring value set as time. Latter should have 1 as the value and the former should have -1. You don't need to do any of what was just described if you want an independent suspension setup.

Some good settings for the "New_Physics_Wheel" script are:

With a solid Axle suspension setup:

Suspension type: solid axle suspension wheel radius: whatever the radius of your visual wheels are which you can easily fine tune using the Debug.DrawRay method as a marker Wheel mass: 15 Rest Length: 0.38 Spring Travel: 0.16 Suspension Stiffness: 30000 Suspension Damper: 5000 Max Solid Axle Force: 50000 Solid Axle: The transform of the object in the middle of the axle

With an independent suspension setup:

Suspension type: independent suspension wheel radius: whatever the radius of your visual wheels are which you can easily fine tune using the Debug.DrawRay method as a marker Wheel mass: 15 Rest Length: 0.38 Spring Travel: 0.16 Suspension Stiffness: 30000 Suspension Damper: 5000 Max Solid Axle Force: 0 Solid Axle: none

Hope this helps somebody!

Comment
Add comment · Show 2 · Share
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
avatar image unity_44234FBC39E5661FBF5B · Nov 05, 2021 at 04:26 PM 0
Share

I tried your scripts, but I'm getting a IndexOutOfRangeException error when I hit play.

avatar image unity_44234FBC39E5661FBF5B unity_44234FBC39E5661FBF5B · Dec 13, 2021 at 05:16 PM 0
Share

You need to put the wheel collider in the object with the axle script.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mapping movement to a WheelCollider suspension? 2 Answers

Can't get a raycast suspension system to work properly. 0 Answers

How to create car suspension system? 1 Answer

Coding my sunspension Need HELP PLS 0 Answers

Can i use wheel colliders with a character controller? 1 Answer


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