Physics.Raycast - WheelCollider (Solved)

As part of a motion platform plugin that I’m working on, I’m trying to map pseudo-suspension values to simulate bumps on only one side of a vehicle.

I’m currently getting noisy telemetry from some wheels at random points in slopes:

OilyCoordinatedHochstettersfrog

I’m checking for distances with Physics.Raycast:

if(Physics.Raycast(wheels[i].transform.position, Vector3.down, out rayhit, Mathf.Infinity))
             {
                 wheelDistances[i] = rayhit.distance;
             }

I’ve tried filtering the noise out with an average median filter of 20 samples, and although it does help reduce the noise, I can’t help but think that I’m doing something wrong with Physics.Raycast…

Any guidance would be appreciated!

Maybe something with the Math Infinity, maybe try a set suspension distance

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SuspensionDistance : MonoBehaviour {

    public WheelCollider _wheelCollider;
    public float suspensionDistance;

    void FixedUpdate () {
        if (_wheelCollider) {
            Vector3 _wheelColliderPosition;
            Quaternion _wheelColliderRotation;
            _wheelCollider.GetWorldPose (out _wheelColliderPosition, out _wheelColliderRotation);
            Vector3 hitPoint = _wheelColliderPosition - (_wheelCollider.transform.up * _wheelCollider.radius);
            suspensionDistance = Vector3.Distance(_wheelCollider.transform.position, hitPoint);
            //
            Debug.DrawLine(_wheelCollider.transform.position, hitPoint);
        }
    }
}
1 Like

Hi Marcos,

Thank you very much for your contribution, I was actually half-dreaming last night thinking, I should just calculate the distance between wheel and the ground hit point, but I had not paid enough attention to the documentation to see WorldPose!

I think this should do the trick - saves me from rebuilding Unity’s source simply to gain access to PhysX’s vehicle SDK.

I wonder why Unity skimmed over giving us access to PxWheelQueryResult::suspJounce …

2 Likes