Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics.Raycast - WheelCollider (Solved)

Discussion in 'Physics' started by ismaelflorit, May 9, 2019.

  1. ismaelflorit

    ismaelflorit

    Joined:
    Apr 16, 2019
    Posts:
    38
    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:



    I'm checking for distances with Physics.Raycast:

    Code (CSharp):
    1. if(Physics.Raycast(wheels[i].transform.position, Vector3.down, out rayhit, Mathf.Infinity))
    2.              {
    3.                  wheelDistances[i] = rayhit.distance;
    4.              }
    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!
     
  2. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    Maybe something with the Math Infinity, maybe try a set suspension distance
     
  3. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SuspensionDistance : MonoBehaviour {
    6.  
    7.     public WheelCollider _wheelCollider;
    8.     public float suspensionDistance;
    9.  
    10.     void FixedUpdate () {
    11.         if (_wheelCollider) {
    12.             Vector3 _wheelColliderPosition;
    13.             Quaternion _wheelColliderRotation;
    14.             _wheelCollider.GetWorldPose (out _wheelColliderPosition, out _wheelColliderRotation);
    15.             Vector3 hitPoint = _wheelColliderPosition - (_wheelCollider.transform.up * _wheelCollider.radius);
    16.             suspensionDistance = Vector3.Distance(_wheelCollider.transform.position, hitPoint);
    17.             //
    18.             Debug.DrawLine(_wheelCollider.transform.position, hitPoint);
    19.         }
    20.     }
    21. }
     
    ismaelflorit likes this.
  4. ismaelflorit

    ismaelflorit

    Joined:
    Apr 16, 2019
    Posts:
    38
    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 ...
     
    Edy and Marcos-Schultz like this.