Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by trynewz · Nov 29, 2021 at 05:31 PM · unity 5editor-scriptingwaypoint

Why do Unity not showing Gizmos?

I have copied code from this tutorial .But problem is no gizmo is showing for waypoint. What wrong is creating this problem?

WaypointEditor.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 
 
 
 
 [InitializeOnLoad()]
 public class WaypointEditor 
 {
     [DrawGizmo(GizmoType.NonSelected | GizmoType.Selected | GizmoType.Pickable)]
     
     public static void OnDrawGizmos(Waypoint waypoint, GizmoType gizmoType)
     {
         if((gizmoType & GizmoType.Selected) != 0)
         {
             Gizmos.color = Color.yellow;
         }
         else
         {
             Gizmos.color = Color.yellow * 0.5f;
         }
 
         Gizmos.DrawSphere(waypoint.transform.position, 0.1f);
         Gizmos.color = Color.white;
         Gizmos.DrawLine(waypoint.transform.position + (waypoint.transform.right * waypoint.width / 2f),
         waypoint.transform.position - (waypoint.transform.right *waypoint.width / 2f));
 
         if(waypoint.previousWaypoint != null)
         {
             Gizmos.color = Color.red;
             Vector3 offset = waypoint.transform.right * waypoint.width / 2f;
             Vector3 offsetTo = waypoint.previousWaypoint.transform.right * waypoint.previousWaypoint.width / 2f;
 
             Gizmos.DrawLine(waypoint.transform.position + offset,waypoint.previousWaypoint.transform.position + offsetTo );
             
         }
         if(waypoint.nextWaypoint != null)
         {
             Gizmos.color = Color.green;
             Vector3 offset = waypoint.transform.right * -waypoint.width / 2f;
             Vector3 offsetTo = waypoint.nextWaypoint.transform.right * -waypoint.width / 2f;
             Gizmos.DrawLine(waypoint.transform.position + offset, waypoint.nextWaypoint.transform.position + offsetTo );
         }
     }
 }


Waypoint.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Waypoint : MonoBehaviour
 {
     public Waypoint previousWaypoint;
     public Waypoint nextWaypoint;
 
     public List<Waypoint> branches;
 
     [Range(0f,1f)]
     public float branchRatio = 0.5f;
 
     [Range(0,5f)]
     public float width = 1f;
 
 
     public Vector3 GetPosition()
     {
         Vector3 minBound = transform.position + transform.right * width / 2f;
         Vector3 maxBound = transform.position - transform.right * width / 2f;
 
         return Vector3.Lerp(minBound,maxBound,Random.Range(0f, 1f));
     }
 }
 
 

WaypointNavigator.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class WaypointNavigator : MonoBehaviour
 {
     public NavMeshAgent agent;
     public Animator animator;
     public Vector3 velocity;
     public Waypoint currentWaypoint;
     
     int direction;
     void Awake()
     {
         agent = GetComponent<NavMeshAgent>();
         animator = GetComponent<Animator>();
     }
     void Start()
     {
         direction = Mathf.RoundToInt(Random.Range(0f, 1f));
         agent.SetDestination(currentWaypoint.GetPosition());
     }
 
     
     void Update()
     {
         velocity = agent.velocity;
         animator.SetFloat("Velocity",velocity.magnitude * 0.1f);
 
          if(agent.remainingDistance <= agent.stoppingDistance + 0.5f)
             {
                 bool shouldBranch = false;
 
                 if(currentWaypoint.branches != null && currentWaypoint.branches.Count > 0)
                 {
                     shouldBranch = Random.Range(0, 1f)  <= currentWaypoint.branchRatio  ? true : false;
                 }
 
                 if(shouldBranch)
                 {
                     currentWaypoint = currentWaypoint.branches[Random.Range(0,currentWaypoint.branches.Count - 1)]; 
                 }
 
                 if(direction == 0)
                 {
                     if(currentWaypoint.nextWaypoint != null)
                     {
                         currentWaypoint = currentWaypoint.nextWaypoint;
                     }
                     else
                     {
                         currentWaypoint = currentWaypoint.previousWaypoint;
                         direction = 1;
                     }
                 }
                 else if (direction == 1)
                 {
                     if(currentWaypoint.previousWaypoint != null)
                     {
                         currentWaypoint = currentWaypoint.previousWaypoint;
                     }
                     else
                     {
                         currentWaypoint = currentWaypoint.nextWaypoint;
                         direction = 0;
                     }
                 }
                 agent.SetDestination(currentWaypoint.GetPosition());
             }
 
     }
 }

WaypointManagerWindow.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class WaypointManagerWindow : EditorWindow
 {
     [MenuItem("Tools/Waypoint Editor")]
     public static void Open()
     {
         GetWindow<WaypointManagerWindow>();
     }
 
     [SerializeField] public Transform waypointRoot;
 
     private void OnGUI()
     {
         SerializedObject obj = new SerializedObject(this);
         EditorGUILayout.LabelField("Waypoint Window");
         EditorGUILayout.PropertyField(obj.FindProperty("waypointRoot"));
 
 
         if(waypointRoot == null)
         {
             EditorGUILayout.HelpBox("Root transform must be selected.please assign a root transform",
             MessageType.Warning);
 
         }
         else
         {
             EditorGUILayout.BeginVertical("box");
             DrawButtons();
             EditorGUILayout.EndToggleGroup();
         }
 
         obj.ApplyModifiedProperties();
 
     }
 
     void DrawButtons()
     {
         if(GUILayout.Button("Create a New Waypoint."))
         {
             CreateWaypoint();
         }
         if(Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<Waypoint>())
         {
             if(GUILayout.Button("Create Waypoint Before"))
             {
                 CreateBeforeWaypoint();
             }
             if(GUILayout.Button("Create Waypoint After"))
             {
                 CreateAfterWaypoint();
             }
             if(GUILayout.Button("Remove Waypoint!!"))
             {
                 RemoveWaypoint();
             }
             if(GUILayout.Button("Add Branch Waypoint"))
             {
                 CreateBranch();
             }
         }
     }
 
     void CreateWaypoint()
     {
         GameObject waypointObject = 
         new GameObject("Waypoint " + waypointRoot.childCount, typeof(Waypoint));
 
         waypointObject.transform.SetParent(waypointRoot, false);
 
         Waypoint waypoint = waypointObject.GetComponent<Waypoint>();
         if(waypointRoot.childCount > 1)
         {
             waypoint.previousWaypoint =
             waypointRoot.GetChild(waypointRoot.childCount - 2).GetComponent<Waypoint>();
             waypoint.previousWaypoint.nextWaypoint = waypoint;
             //place the waypoint at the last position
             waypoint.transform.position = waypoint.previousWaypoint.transform.position;
             waypoint.transform.forward = waypoint.previousWaypoint.transform.forward; 
         }
 
         Selection.activeGameObject = waypoint.gameObject;
     }
 
     void CreateBranch()
     {
         GameObject waypointObject = 
         new GameObject("Waypoint " + waypointRoot.childCount, typeof(Waypoint));
 
         waypointObject.transform.SetParent(waypointRoot, false);
 
         Waypoint waypoint = waypointObject.GetComponent<Waypoint>();
 
         Waypoint branchedFrom = Selection.activeGameObject.GetComponent<Waypoint>();
         branchedFrom.branches.Add(waypoint);
         
         waypoint.transform.position = branchedFrom.transform.position;
         waypoint.transform.forward = branchedFrom.transform.forward; 
 
         Selection.activeGameObject = waypoint.gameObject;
 
     }
 
     
 
     void CreateBeforeWaypoint()
     {
         GameObject waypointObject = new GameObject("Waypoint " + waypointRoot.childCount, typeof(Waypoint));
         waypointObject.transform.SetParent(waypointRoot, false);
 
         Waypoint newWaypoint = waypointObject.GetComponent<Waypoint>();
 
         Waypoint selectedWaypoint = Selection.activeGameObject.GetComponent<Waypoint>();
 
         waypointObject.transform.position = selectedWaypoint.transform.position;
         waypointObject.transform.forward = selectedWaypoint.transform.forward;
 
         if(selectedWaypoint.previousWaypoint != null)
         {
             newWaypoint.previousWaypoint = selectedWaypoint.previousWaypoint;
             selectedWaypoint.previousWaypoint.nextWaypoint = newWaypoint;
         }
         newWaypoint.nextWaypoint = selectedWaypoint;
         selectedWaypoint.previousWaypoint = newWaypoint;
         newWaypoint.transform.SetSiblingIndex(selectedWaypoint.transform.GetSiblingIndex());
         Selection.activeGameObject = newWaypoint.gameObject;
     }
 
     void CreateAfterWaypoint()
     {
         GameObject waypointObject = new GameObject("Waypoint " + waypointRoot.childCount, typeof(Waypoint));
         waypointObject.transform.SetParent(waypointRoot, false);
 
         Waypoint newWaypoint = waypointObject.GetComponent<Waypoint>();
 
         Waypoint selectedWaypoint = Selection.activeGameObject.GetComponent<Waypoint>();
 
         waypointObject.transform.position = selectedWaypoint.transform.position;
         waypointObject.transform.forward = selectedWaypoint.transform.forward;
 
         newWaypoint.previousWaypoint = selectedWaypoint;
         if(selectedWaypoint.nextWaypoint != null)
         {
             selectedWaypoint.nextWaypoint.previousWaypoint = newWaypoint;
             newWaypoint.nextWaypoint = selectedWaypoint.nextWaypoint;
         }
         selectedWaypoint.nextWaypoint = newWaypoint;
         newWaypoint.transform.SetSiblingIndex(selectedWaypoint.transform.GetSiblingIndex());
         Selection.activeGameObject = newWaypoint.gameObject;
     }
 
     void RemoveWaypoint()
     {
         Waypoint selectedWaypoint = Selection.activeGameObject.GetComponent<Waypoint>();
         if(selectedWaypoint.nextWaypoint != null)
         {
             selectedWaypoint.nextWaypoint.previousWaypoint = selectedWaypoint.previousWaypoint;
 
         }
         if(selectedWaypoint.previousWaypoint != null)
         {
             selectedWaypoint.previousWaypoint.previousWaypoint = selectedWaypoint.nextWaypoint;
             Selection.activeGameObject = selectedWaypoint.previousWaypoint.gameObject;
 
         }
 
         DestroyImmediate(selectedWaypoint.gameObject);
 
     }
    
 }


I also write this GizmoDraw.cs to see gizmos in runtime which works fine

 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 
 
 public class GizmoDraw : MonoBehaviour
 {
     public List<Waypoint> waypoints;
     
     void Awake()
     {
         int count = transform.childCount;
         for(int i =0; i<count; i++)
         {
             waypoints.Add(transform.GetChild(i).GetComponent<Waypoint>());
         }
 
         
     }
 
     void Update()
     {
         Debug.Log("Total " + waypoints.Count);
     }
     void OnDrawGizmos()
     {
         // Draw a yellow sphere at the transform's position
         /*Gizmos.color = Color.yellow;
         Gizmos.DrawSphere(transform.position, 1);
         */
 
         
             Gizmos.color = Color.yellow ;
         
 
         foreach(Waypoint waypoint in waypoints)
         { 
             Gizmos.DrawSphere(waypoint.transform.position, 0.1f);
             Gizmos.color = Color.white;
             Gizmos.DrawLine(waypoint.transform.position + (waypoint.transform.right * waypoint.width / 2f),
             waypoint.transform.position - (waypoint.transform.right *waypoint.width / 2f));
 
             if(waypoint.previousWaypoint != null)
             {
                 Gizmos.color = Color.red;
                 Vector3 offset = waypoint.transform.right * waypoint.width / 2f;
                 Vector3 offsetTo = waypoint.previousWaypoint.transform.right * waypoint.previousWaypoint.width / 2f;
 
                 Gizmos.DrawLine(waypoint.transform.position + offset,waypoint.previousWaypoint.transform.position + offsetTo );
                 
             }
             
             if(waypoint.nextWaypoint != null)
             {
                 Gizmos.color = Color.green;
                 Vector3 offset = waypoint.transform.right * -waypoint.width / 2f;
                 Vector3 offsetTo = waypoint.nextWaypoint.transform.right * -waypoint.width / 2f;
                 Gizmos.DrawLine(waypoint.transform.position + offset, waypoint.nextWaypoint.transform.position + offsetTo );
             }
 
             if(waypoint.branches != null)
             {
                 foreach(Waypoint branch in waypoint.branches)
                 {
                     Gizmos.color = Color.blue;
                     Gizmos.DrawLine(waypoint.transform.position, branch.transform.position);
                 }
             }
         }
     }
 }

It will be helpful for any suggestion.

nogizmo-min.png (454.2 kB)
pedestrianspawn-min.png (437.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity editor not saving object changes 4 Answers

[ExecuteInEditMode] OnEnable Running twice on Play? 1 Answer

Building custom Font with script, can not change CharacterInfo 1 Answer

BoxBoundsHandle rotation 2 Answers

in editor object placement by script in editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges