Search Unity

Visualizing an Object's Front Arc Line of Sight

Discussion in 'Scripting' started by Nomad_Artisan, Jun 29, 2013.

  1. Nomad_Artisan

    Nomad_Artisan

    Joined:
    Jun 29, 2013
    Posts:
    20
    Hello,
    I'm trying to draw an object's front arc line of sight for debugging purposes.
    I'm using the following code.
    Code (csharp):
    1.  
    2. void Update ()
    3.     {
    4.        
    5.         Vector3 losRight = transform.TransformDirection(Quaternion.Euler(0, 45, 0) * transform.forward)*5;
    6.         Vector3 losLeft = transform.TransformDirection(Quaternion.Euler(0, -45, 0) * transform.forward)*5;
    7.         Debug.DrawRay(transform.position, losRight, Color.green);
    8.         Debug.DrawRay(transform.position, losLeft, Color.green);
    9.    
    10.     }
    11.  
    I'm getting the correct result, pictured below, until the object rotates, at which point the rays begin to skew.

    $Capture.JPG

    Any Idea how I can keep this front arc visual consistent?

    I was also trying to implement the Handles.DrawSolidArc I found here, but with no luck. I know even less about javascript than I do about C# (fledgling programmer).
    Any help would be greatly appreciated.
    Thank You!
     
  2. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    Code (csharp):
    1. void Update()
    2. {
    3.     float distance = 5f;
    4.     Debug.DrawRay(transform.position, (transform.forward + transform.right) * distance, Color.green);
    5.     Debug.DrawRay(transform.position, (transform.forward - transform.right) * distance, Color.green);
    6. }
     
    Last edited: Jun 29, 2013
  3. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    Here is an example using the Handles.DrawSolidArc in C#. First you must make a folder called 'Editor' and place the following script in it:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(SolidArcExample))]
    6. public class DrawSolidArc : Editor
    7. {
    8.  
    9.     void OnSceneGUI()
    10.     {
    11.         SolidArcExample myTarget = (SolidArcExample)target;
    12.  
    13.         Handles.color = new Color(1f, 1f, 1f, 0.2f);
    14.  
    15.         Handles.DrawSolidArc(myTarget.transform.position,
    16.             myTarget.transform.up,
    17.             -myTarget.transform.right,
    18.             180,
    19.             myTarget.shieldArea);
    20.  
    21.         Handles.color = Color.white;
    22.  
    23.         myTarget.shieldArea = Handles.ScaleValueHandle(myTarget.shieldArea,
    24.             myTarget.transform.position + myTarget.transform.forward * myTarget.shieldArea,
    25.             myTarget.transform.rotation,
    26.             1f,
    27.             Handles.ConeCap,
    28.             1f);
    29.     }
    30. }
    You must then make the following script and place it as a component on the object that you want to have the arc.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class SolidArcExample : MonoBehaviour
    5. {
    6.  
    7.     public float shieldArea = 5f;
    8. }
    9.