Search Unity

Wrong Position of WheelHit

Discussion in 'Physics' started by mixed2, Oct 13, 2015.

  1. mixed2

    mixed2

    Joined:
    Feb 14, 2015
    Posts:
    18
    Hi, im here again with new curious problem :)
    I have some car and a script, which is generating skidmarks:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SkidScript : MonoBehaviour {
    6.    public GameObject skidSound;
    7.    public float currentFrictionValue;
    8.    public float skidAt = 0.1f;
    9.    public float soundEmition =15;
    10.    public GameObject skidSmoke;
    11.    public float smokeDepth = 0;
    12.    public bool rearWheel = false;
    13.    private float soundWait;
    14.    public float markWidth = 0.025f;
    15.    private int skidding;
    16.    private Vector3[] lastPos = new Vector3[2];
    17.    public Material skidMaterial;
    18.  
    19.  
    20.    void Start () {
    21.      skidSmoke.transform.position = transform.position;
    22.      Vector3 position = skidSmoke.transform.position;
    23.      position.y -= smokeDepth;
    24.      skidSmoke.transform.position = position;
    25.    }
    26.  
    27.    // Update is called once per frame
    28.    void Update () {
    29.      WheelHit hit;
    30.      transform.GetComponent<WheelCollider>().GetGroundHit(out hit);
    31.      currentFrictionValue = Mathf.Abs(hit.sidewaysSlip);
    32.  
    33.      if (skidAt <= currentFrictionValue && soundWait <= 0)
    34.      {
    35.  
    36.        Instantiate(skidSound, hit.point, Quaternion.identity);
    37.        soundWait =1;
    38.      }
    39.  
    40.        soundWait -= Time.deltaTime * soundEmition;
    41.  
    42.      if (skidAt <= currentFrictionValue)
    43.      {
    44.  
    45.        skidSmoke.particleEmitter.emit = true;
    46.        SkidMesh();
    47.      }
    48.      else
    49.      {
    50.        skidSmoke.particleEmitter.emit = false;
    51.        skidding =0;
    52.      }
    53.    }
    54.    void SkidMesh()
    55.    {
    56.      WheelHit hit;
    57.     //HERE IS THE PROBLEM:
    58.      transform.GetComponent<WheelCollider>().GetGroundHit(out hit);
    59.      Debug.Log(hit.point.ToString("F3")+" . "+transform.position.ToString("F3"));
    60.      GameObject mark = new GameObject("Mark");
    61.      MeshFilter filter = mark.AddComponent<MeshFilter>();
    62.      mark.AddComponent<MeshRenderer>();
    63.  
    64.    //some code....
    65.  
    66.    }
    67.  
    68. }
    69.  
    Problem is that position of Wheel Hit in SkidMesh function is wrong, so skid marks also have wrong position (see image)
    wheelhit.png
    What could i do?
    I tried almost everything but i still can't find solution :(
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I would use GetGroundHit to determine if the car is sliding, then use GetWorldPose to determine where to draw the skids marks (note youll have to subtract wheel radius)
     
  3. mixed2

    mixed2

    Joined:
    Feb 14, 2015
    Posts:
    18
    GetWorldPose works only in Unity 5 :(
    Could i just take world position of WheelCollider and substract wheel radius from y pos?
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    yeah I would say so.

    youll probably want to be sure the wheel is hitting the ground as well.
     
  5. mixed2

    mixed2

    Joined:
    Feb 14, 2015
    Posts:
    18
    ok, problem solved