- Home /
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
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.
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 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 I know it's a truck, and not a go kart - but this photo shows off the suspension the best.
Cheers
Well I've only really been getting into Unity the last month or so, sorry I wasn't able to help more.
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!
How would you make the wheel model follow it and add friction?
Your answer
Follow this Question
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
unity 5 wheel collider problem 3 Answers