I am working on making my own very simple wheel-collider. However, this requires being able to judge the forces on each individual wheel collider.
The natural solution for me seems to be to create spherecolliders, then read the forces from their collisions with their surfaces they are in contact with to calculate friction and slippage.
After reading off col.impactForceSum, col.impulse, and even col.frictionForceSum, i’m getting zeroes across the board, despite gravity being enabled.
Is there a way to find the rest of the force that counteracts gravity in unity?
Alright guys i figured this one out myself, or at least a workaround for it.
i was getting zeroes (or near zeroes) across the board for the collisions on different sphere colliders, despite gravity being in effect. Unfortunately with, unity’s gravity, as soon as the rigidbody stopped moving, the OnCollisionStay method stopped being called, and the last value i could get out of the Collision passed to it was zeroes across the board.
The workaround for this was to make a “fake” gravity script:
using UnityEngine;
using System.Collections;
public class fakeGravity : MonoBehaviour {
public float G;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
GetComponent<Rigidbody>().AddForce(-transform.up * G, ForceMode.Acceleration);
}
}
Note: it is important that this is in a FixedUpdate function otherwise gravity could be applied twice once in a while making the physics engine resolve it with an impulse twice as high.
I’ve yet to isolate each wheel, as the way children are handled, i cant put the script on each individual collider as children cant have rigidbodies, however, I was able to prove to myself that it was working with this script and offsetting the positions of the wheels and checking if the series of collisions had different impulses. Sure enough, as I moved one pair closer to the CG, i saw one value grow while the other fell:
using UnityEngine;
using System.Collections;
public class customWheelCollider : MonoBehaviour {
Collision currentCollision;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
}
void OnCollisionEnter (Collision col){
currentCollision = col;
}
void OnCollisionStay(Collision col){
currentCollision = col;
print(currentCollision.impulse);
}
void OnCollisionExit(Collision col)
{
currentCollision = null;
}
}
Forces aren’t really stored in the rigidbody. They are simply accumulated into one resulting force / torque and directly applied to velocity / angularVelocity. What you want to do is to split the velocity into seperate axis and analyse and adjust each axis seperately. When done you just recombine them. You can use Vector3.Project to project the velocity vector onto your desired axis. The important slip axis would be the rotation axis of your wheel.
Another way besides using Vector3.Project is to use Transform.InverseTransformDirection of the transform object of your wheel. That way the velocity vector is already seperated into it’s local axis.