Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 WOODSY · Feb 12, 2017 at 01:42 AM · wheelcollider

Solid axle setup?

Okay, so I'm aiming to have a solid axle, that connects at one end to the position of the left wheel, and vice versa for the right wheel.

The only thing I'm struggling to get my head around is how it would work to manipuate the transform like that.

The thing that is the closest to what I want is transform.LookAt() - but that only works for one side.

Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Feb 12, 2017 at 08:53 AM

Quick Edit: I didn't have time to finish testing this right now, so the exact solution might not work but I assure you the concept is pretty solid. find object > get rotation > find second object > change rotation

I don't feel like you were trying very hard to solve this on your own, but it's all pretty basic stuff.

So you want to attach objects to a main object that applies the motion, correct? And you want to be able to attach the wheel during runtime, amiright?

Something like:

     public GameObject mainAxle;
     public GameObject firstWheel;
     public GameObject secondWheel;
     Vector3 mainAxlePosition;
     Vector3 maineAxleRotation;
     bool attachLeftWheel;
     bool attachRightWheel;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (attachLeftWheel) {
             mainAxlePosition = mainAxle.transform.position;
             maineAxleRotation = mainAxle.transform.rotation;
             firstWheel.transform.Translate (mainAxlePosition.x - 3, mainAxlePosition.y, mainAxlePosition.z);
             firstWheel.transform.Rotate (maineAxleRotation);
         }
         if (attachRightWheel) {
             mainAxlePosition = mainAxle.transform.position;
             maineAxleRotation = mainAxle.transform.rotation;
             secondWheel.transform.Translate (mainAxlePosition.x + 3, mainAxlePosition.y, mainAxlePosition.z);
             secondWheel.transform.Rotate (maineAxleRotation);
         }
     }


If the objects are children of the axle the wheels should end up moving when the axle moves, how you attach the axle to other objects is another issue altogether. If you're new to this I recommend you finish watching all the Unity Scripting Tutorials in the learning section of their site, as well as watch some videos from Sebastian Lague. Good Luck.

Comment
Add comment · 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
0

Answer by WOODSY · Feb 13, 2017 at 07:59 AM

I tried your code, and I feel like it's on the right track, but nothing really happened.

Sorry about how poorly I described the answer before, it was too late and I'd just given up.

I've been working with Unity for about 2 and a bit years, so the only reason I'm here is because I'm really stuck!

Anyways, this is what I've got now alt text I do have other vehicles, but this is the one where I do want the suspension to be more accurate. And this is what I'm looking for in terms of the suspension axle alt text I know it's a truck, and not a go kart - but this photo shows off the suspension the best.

Cheers


kart.png (283.4 kB)
springs6682-500.jpg (40.6 kB)
Comment
Add comment · Show 1 · 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 $$anonymous$$ · Feb 13, 2017 at 09:46 PM 0
Share

Well I've only really been getting into Unity the last month or so, sorry I wasn't able to help more.

avatar image
0

Answer by LemonMontage420 · Feb 06, 2021 at 08:42 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 1 · 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 16, 2021 at 07:00 PM 0
Share

How would you make the wheel model follow it and add friction?

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

65 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

Related Questions

When I use WheelColliders, my car drifts to the left. Why? 3 Answers

Applying Torque to wheelcollider 1 Answer

Why Wheel Collider Not Following Tires Shape 0 Answers

With wheel collider my car not landing properly in slops, it gives jumping effect when not all four wheels are grounded? 0 Answers

unity 5 wheel collider problem 3 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