- Home /
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?
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 );
}
}
}
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));
}
}
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());
}
}
}
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
Your answer
