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

how to calculate faster position within 1 ms?

Discussion in 'Scripting' started by econt, Sep 6, 2019.

  1. econt

    econt

    Joined:
    Apr 8, 2019
    Posts:
    52
    Hi,

    I have a car with 4 wheels and I want to get the position of the contact point on the road of each tire within 1ms or faster.

    My first Approach is to send a raycast from the tire to the road to get the position of the contact point. But this solution seems to be slower.

    Is there a faster solution for this and how could it be fit to the time of 1ms?

    Thanks in advance!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    How exactly did you check the time 4 raycasts take? How many colliders are in your scene? Did you use Debug.Log() or similar statements? Raycasts are very well optimized and very fast. Casting 4 rays shouldnt be a problem at all, unless there are tons of colliders in your scene. If you used Debug.Log() or similar statements, then that may be the problem since writing to the console is very slow. If you posted the code used for benchmarking i could say more about this.

    Just for reference, this:
    Code (CSharp):
    1.         float beginTime = Time.realtimeSinceStartup;
    2.         Ray ray;
    3.         RaycastHit hit;
    4.         for (int i = 0; i < 1000; i++)
    5.         {
    6.             Physics.Raycast(transform.position + Vector3.right * i, -Vector3.up, out hit, 100f);
    7.             Vector3 hitPos = hit.point;
    8.         }
    9.         Debug.Log((Time.realtimeSinceStartup-beginTime)*1000 + "ms have passed.");
    Is executed in under 2ms on my system (scene with 8 or so colliders). So <~ 2us per raycast. If they actually hit something or you decrease the max distance to something lower than 100, it will execute even faster (closer to 1ms on my system). And quite frankly, even with adding douzens of colliders the result was pretty much the same.
    So 4 raycasts shouldnt be a problem at all, unless you have hundreds of colliders, or raycasts are not your problem.
     
    Last edited: Sep 6, 2019