Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

RaycastCommand gives incorrect results when direction isn't normalized

Discussion in 'Entity Component System' started by cameron_oltmann, May 9, 2018.

  1. cameron_oltmann

    cameron_oltmann

    Joined:
    Jun 7, 2017
    Posts:
    8
    I'm loving the C# job system so far. In the process of doing some investigation toward jobifying AI sensor code, I ran into a bug with RaycastCommand. Incorrect results are returned if the direction of the raycast isn't normalized. I've submitted a bug report with a sample project. Case number is 1035922.

    Keep up the great work, guys! I'm loving the direction Unity has been moving lately.
     
    RaL likes this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    Did you ever get anything back on if this is a bug or not?

    I can totally understand it if it's our responsibility to normalize the input direction, but there's nothing in the docs about this.
     
  3. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,984
    I agree. If this intentional, then the docs should be updated to reflect this behavior. I wasted a lot of time debugging this tonight.

    RaycastCommand is currently unreliable if the direction vector is not normalized. This behavior is different from the Physics.Raycast function, which does not require a normalized direction vector. It seemed like RaycastCommand was completely ignoring the max distance value when the direction vector was not normalized.

    Here is a link to another thread about this bug (or undocumented behavior):
    https://forum.unity.com/threads/raycastcommand-maxdistance-doesnt-work.531918/
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    I think forcing a normalized direction vector is a good idea but it should throw an exception when it's given a vector that isn't normalized, rather than just returning unexpected or undefined results.
     
    Last edited: Jul 7, 2018
  5. Scott-Michaud

    Scott-Michaud

    Joined:
    May 26, 2015
    Posts:
    18
    That would mean that it would have to perform the normalization at run-time to check, which would slow down inputs that are already guaranteed to be normalized. If they are okay with this performance hit, then they should just force normalization on everything. If not, then they should leave it to us to sanitize our inputs and make sure it's normalized. (They could also give us a parameter to choose but things like that should be done sparingly.)

    But, yes, at the very least it should be well documented.
     
  6. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,984
    I agree the focus with this command should be only on performance. All I really want is to have the docs improved. I am happy to provide a normalized direction vector, but the docs should tell me that is expected.

    The two pages that should immediately be updated:
    https://docs.unity3d.com/ScriptReference/RaycastCommand.html
    https://docs.unity3d.com/ScriptReference/RaycastCommand-direction.html

    If Unity updated those two pages and did nothing else, I would be completely satisfied on this topic.

    Instead of saying "The direction of the ray", it should say "The direction of the ray as a Normalized Vector3".

    And in the code example, they need to add a comment. Something like this:
    Vector3 direction = Vector3.forward; // direction must be Normalized
     
    Last edited: Jul 7, 2018
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    This can just be an editor only check though. Basically just how it is atm with an Assert.AreEqual(direction.normalized, direction)

    It'll be stripped at at runtime.
     
  8. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    To be honest, a "direction" vector is always a unit vector else it's not really a direction vector but just any vector with a direction and a length.
    So if you don't pass a direction vector into a method that expects a direction vector you can't expect the outcome to be right. The vector length can have an effect on different mathematical functions or optimizations.
     
    Last edited: Jul 8, 2018
  9. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,984
    The problem with that mindset is that the Physics.Raycast accepted and worked with non-normalized direction vectors. When coders migrate existing Physics.Raycast code to RaycastCommand, they will run into this issue. Improved docs and an editor assert would save people a lot of time on this issue.
     
    Enrico-Monese and RaL like this.