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

Problem with Gizmos lines

Discussion in 'Scripting' started by ControlledDrive, Nov 25, 2020.

  1. ControlledDrive

    ControlledDrive

    Joined:
    Mar 28, 2020
    Posts:
    10
    I'm new to the forums, and I have a little problem. To have a better debugging in some "wayPoints" that I have, through which passes a mobile platform, I made that each point connects with the one that follows, forming a line, like this:

    It works well, but when you run the game, the lines stick to the object on the platform and what you can see here happens:


    Does anyone know how to solve it? This is my code:
    Code (CSharp):
    1. public void OnDrawGizmos()
    2.     {
    3.         if (localWayPoints != null)
    4.         {
    5.             Gizmos.color = Color.red;
    6.             float size = 0.3f;
    7.  
    8.             for (int i = 0; i < localWayPoints.Length; i++)
    9.             {
    10.                 Vector3 globalWayPointsPos = (Application.isPlaying) ? globalWayPoints[i] : localWayPoints[i] + transform.position;
    11.                 Gizmos.DrawLine(globalWayPointsPos - Vector3.up * size, globalWayPointsPos + Vector3.up * size);
    12.                 Gizmos.DrawLine(globalWayPointsPos - Vector3.left * size, globalWayPointsPos + Vector3.left * size);
    13.  
    14.                
    15.                 if (cyclic == true)
    16.                 {
    17.                     Gizmos.color = Color.white;
    18.                     Gizmos.DrawLine(localWayPoints[localWayPoints.Length - 1] + transform.position, localWayPoints[0] + transform.position);
    19.                 }
    20.  
    21.                 for (int k = 1; k < localWayPoints.Length; k++)
    22.                 {
    23.                     Gizmos.color = Color.green;
    24.                     Gizmos.DrawLine(localWayPoints[k - 1] + transform.position, localWayPoints[k] + transform.position);
    25.                 }
    26.            
    27.             }
    28.         }
    29.     }
    (By the way, the white line you see in the code is the same, it sticks to the platform, and has a bool to activate or deactivate it.)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    In your gizmo calls where you add the transform.position, you need instead to add the position of whatever you think that widget should be anchored to.
     
  3. ControlledDrive

    ControlledDrive

    Joined:
    Mar 28, 2020
    Posts:
    10
    Sorry for asking, but I didn't understand the "you need instead to add the position of whatever you think that widget should be anchored to". What do I have to add specifically?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Traditionally you organize your level so that all waypoints of a given path are parented to a blank gameobject.

    This is where you'd put the gizmo drawer.

    This is also where you'd tell your waypoint follower (which might live on the agent itself), "hey go follow those waypoints"

    That way the agent can follow the waypoints but the waypoints don't follow the agent: they are fixed.
     
  5. ControlledDrive

    ControlledDrive

    Joined:
    Mar 28, 2020
    Posts:
    10
    The truth is that I did everything, I added the "globalWayPointsPos", I tried with the Transform.LocalPosition, and nothing. Either they stay fixed but many lines are added, or they are not added, but they move. My main language is not English, so I have some problems with explaining and understanding you. Could you help me by showing me some specific code or function that I can use in this case?