Search Unity

Linecast vs. Raycast ; float to int ; FixedUpdate

Discussion in 'Scripting' started by Der Dude, Aug 9, 2006.

  1. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hello,

    what I would like to know is, how do Physics.Linecast and Physics.Raycast compare to each other on a performance basis. I have to calculate the starting and ending point of my line anyway, so would Linecast be faster?

    Another thing: how do I convert floats to ints? In JavaScript there is a function "parseInt( someFloat )", but it won't work with Unity JavaScript. Casting it to int like in Java ( int a = (int) someFloat ) doesn't work either.
    Mathf.round rounds to the nearest Integer, but returns a float. So thats no help either.

    Thanks in advance.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Linecast is equally fast as raycast. In the raycast you can also pass a distance how long the raycast goes. So essentially it is the same operation, often LineCast is more convenient to use.


    Casting to int works like this:
    Code (csharp):
    1.  
    2. var p : int = 22.3;
    3.  
     
    chadfranklin47 likes this.
  3. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Thanks, it was just too easy =)
     
  4. billunitydev

    billunitydev

    Joined:
    Aug 8, 2013
    Posts:
    5
    I'm surprised they are equally fast... I would have expected a raycast to be cheaper because a raycast has the luxury of omitting any faces where Vector3.Dot(ray, facenormal) > 0.0f (i.e. the ray and facenormal are in the same direction).

    I would have expected them to be in the same Order of complexity (1 linecast = 2 raycasts)... but definitely not "equally" fast.
     
  5. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51
    Not sure about Physics.Linecast (the documentation is sparse), but Physics2D.Linecast says

    The direction of the line is assumed to extend from the start point to the end point. Only the first collider encountered in that direction will be reported.
    So they should be the same so long as you don't use Math.Infinity for the length of your ray (which might cause more collision check calculations than necessary.
     
    AmbroiseRabier likes this.
  6. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    226
    Was wondering why there was no LinecastCommand alternative to RaycastCommand and was surprised to get an answer way back from 2006. Maybe if not for performance, it could be done though for convenience ;). Thanks for your work and dedication over the years!