Search Unity

Raycast visualisation problem with Mathf.Sin

Discussion in 'Scripting' started by Deleted User, Jan 5, 2020.

  1. Deleted User

    Deleted User

    Guest

    Hello,

    I have the following snippet of code from my raycast
    Code (CSharp):
    1. Ray rayL = new Ray(transform.position + Vector3.up * 0.5f + transform.right * -detectionAngle * 0.5f - transform.right * detectionAngle * 0.0f * Mathf.Sin(Time.time * 50), transform.TransformDirection(Vector3.forward) * detectionDistance);
    in particular :
    Code (CSharp):
    1. + transform.right * -detectionAngle * 0.5f - transform.right * detectionAngle * 0.0f * Mathf.Sin(Time.time * 50)
    I have the exact same code in my OnDrawGizmos to visualise the raycast, but the rays are static in the editor view when in Play mode, is the code wrong ? or does OnDrawGizmos not let me visualise this kind of movement, or am I just not understanding this in the way that I think ?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Too many characters on one line. My brain went "Tilt."

    Break it down, use temporary variables, then you can actually reason about what you are doing, and even use Debug.Log() to display intermediate results.
     
  3. Deleted User

    Deleted User

    Guest

    I agree, too many characters ! :p

    I do understand what it's doing, but I'm unsure as to why my OnDrawGizmos isn't updating to reflect the Mathf.Sin part, so it should be raycasting on the left side of my object, and the ray should be oscillating between 0 and 0.5, but when I press Play and look at my object, the editor view only shows the OnDrawGizmos rays as being static, I wondered if this was a limitation of OnDrawGizmos, or if my Mathf.Sin function wasn't working the way I thought it was.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Well, if you would care to actually post some of your newly refactored one-thing-per line code, perhaps I could examine it... :)
     
  5. Deleted User

    Deleted User

    Guest

    Sure thing ... :cool:

    Code (CSharp):
    1.     // Visualise Raycasts ( if avoidObstacles is true )
    2.  
    3.     private void OnDrawGizmos()
    4.     {
    5.         if (avoidObstacles)
    6.         {
    7.             Gizmos.color = Color.red;
    8.  
    9.             // Left
    10.  
    11.             Gizmos.DrawRay(transform.position + Vector3.up * 0.5f + transform.right * -detectionAngle * 0.5f - transform.right * detectionAngle * 0.0f * Mathf.Sin(Time.time * 50), transform.TransformDirection(Vector3.forward) * detectionDistance);
    12.  
    13.             // Right
    14.  
    15.             Gizmos.DrawRay(transform.position + Vector3.up * 0.5f + transform.right * detectionAngle * 0.5f + transform.right * detectionAngle * 0.0f * Mathf.Sin(Time.time * 50), transform.TransformDirection(Vector3.forward) * detectionDistance);
    16.         }
    17.     }
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Well, you're multiplying by 0.0f. That's probably not what you want to do...
     
    Deleted User and StarManta like this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    So @Baste has the main issue, but this is also suspicious. When you make that multiplier not be 0, this bit is going to make the raycast jitter back and forth about 8 times per second, which is probably not what you want. The Mathf trigonometry functions use radians, not degrees.
     
    Deleted User likes this.