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

Question Draw RayCast works once and then not

Discussion in 'Scripting' started by blu3drag0n, Jul 21, 2023.

  1. blu3drag0n

    blu3drag0n

    Joined:
    Nov 9, 2018
    Posts:
    94
    Hi there,
    one issue driving me crazy for 2 days and I cant figure out why it is behaving that strang.
    I want to make a box selection for my factory game and therefore I wanted to Raycast through the corners of the selection drawn and form a mesh around the Rays.

    Issue is, that for some reason, only my starting point, where I initially click works fine. But the other Rays dont work and they are always FAR off.

    Through trooublshooting I am already only trying to only use the initially clicked position, draw a ray, where the drags starts and then the 2nd Ray should go wherever the mouse currently is, without any calculations in between. But the 2nd Ray (and when I calculated the other 2 corners also) is always far off where the mouse is.

    I have an InputReceiverManager who tracks all the inputs and take the initial click like this:
    Code (CSharp):
    1. InitialMousePositionOnLeftClick = Mouse.current.position.ReadValue();
    The Ray for this one works fine.
    And then on every Update I have a the current mouse position with the same line of code, like this:
    Code (CSharp):
    1. CurrentMousePosition = Mouse.current.position.ReadValue();
    Then I draw Rays accordingly like this when box selection is confirmed:
    Code (CSharp):
    1. Ray initialRay = MainCamera.ScreenPointToRay(InputReceiverManager.Instance.InitialMousePositionOnLeftClick);
    2. Debug.DrawLine(initialRay.origin, initialRay.GetPoint(100f), Color.red);
    3.  
    4. Vector3 cameraOffset = new Vector3(0f, 0f, MainCamera.transform.position.z).Abs();
    5. Ray movingRay = new Ray(MainCamera.transform.position, MainCamera.ScreenToWorldPoint((Vector3)InputReceiverManager.Instance.CurrentMousePosition + cameraOffset) - MainCamera.transform.position);
    6. Debug.DrawRay(movingRay.origin, movingRay.GetPoint(100f), Color.red);
    7.  
    8. Ray movingRay2 = MainCamera.ScreenPointToRay(InputReceiverManager.Instance.CurrentMousePosition);
    9. Debug.DrawRay(movingRay2.origin, movingRay2.GetPoint(100f), Color.red);
    Now see how the "initialRay" is casting/drawing fine and the "movingRay" is far off where it should be:
    raycast.gif

    I also tried calculating it all with ScreenToWorldPoint offsetting the camera position accordingly and go from there.
    The math and result for the Rays is identical then.
    What am I missing ?

    KR
     
    Last edited: Jul 21, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    In your code, isn't it the case that you're passing in a world position as the second argument to Debug.DrawRay when it should actually be a direction? This is why Debug.DrawLine works as it draws a line segment (not a ray) so needs two world positions.

    This should work:
    Code (CSharp):
    1. Debug.DrawRay(movingRay2.origin, movingRay2.GetPoint(100f) - movingRay2.origin, Color.red);
     
    Bunny83 likes this.
  3. blu3drag0n

    blu3drag0n

    Joined:
    Nov 9, 2018
    Posts:
    94
    Oh my goodness, I did the actual Raycast right, but the DrawRay wrong *facepalm*
    Yes indeed, I either had to use DrawLine for my code to work or substract movingRay.origin from the GetPoint() Vector when using DrawRay.
    Thanks!

    I will test out if can figure out the actual box selection through the Rays and come back here if the thread is done :)
     
    Bunny83 and MelvMay like this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Also, for completeness, you can use Physics.Linecast too if you are only interested in line segments when using physics itself.
     
    Bunny83 likes this.
  5. blu3drag0n

    blu3drag0n

    Joined:
    Nov 9, 2018
    Posts:
    94
    MelvMay likes this.