- Home /
OnDrawGizmos() can not show boundary
I made a small plane and was going to add a boundary to the plane's range, and I chose OnDrawGizmos(), but I had a problem.
The problem is that I can't see the rectangle boundary I drew in my project panel.
This is my code. I put it on the plane. In fact, if I replace this code with some other simple boundary code, I can't see the boundary too.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoundsCheck : MonoBehaviour
{
[Header("Set Dynamically")]
public float camWidth;
public float camHeight;
private void Awake()
{
camHeight = Camera.main.orthographicSize;
camWidth = camHeight * Camera.main.aspect;
}
private void OnDrawGizmos()
{
if(!Application.isPlaying)
{
return;
}
Vector3 boundSize = new Vector3(camWidth * 2, camHeight * 2, 0.1f);
Gizmos.DrawWireCube(Vector3.zero, boundSize);
}
}
And the Gizmos menu does have this script.
Hope someone can give advice, if there is anything I need to add, please feel free to ask, thanks a million.
Gizmos.DrawWireCube(Vector3.zero, boundSize);
are you sure you want to draw this cube at (0,0,0)
and not something like transform.position
?
I think I really want the origin to be the center. And even if I changed the origin to the current position of the plane, I still didn't see the boundary. I was very confused.
Answer by andrew-lukasik · Sep 04, 2021 at 09:04 AM
If the thing you want to just draw camera orthographic bounds, this is how you do that:
DrawCameraOgraphicBounds.cs
using UnityEngine;
public class DrawCameraOgraphicBounds : MonoBehaviour
{
[SerializeField] Camera _camera = null;
void OnDrawGizmos ()
{
if( _camera!=null )
{
if( !_camera.orthographic )
{
Debug.LogWarning("camera is not orthographic",gameObject);
return;
}
Gizmos.matrix = _camera.cameraToWorldMatrix;// special camera matrix
{
float h = _camera.orthographicSize;
float w = h * _camera.aspect;
float near = _camera.nearClipPlane;
float far = _camera.farClipPlane;
Vector3 size = new Vector3( w*2 , h*2 , far-near );
Vector3 localOffset = new Vector3( 0 , 0 , -size.z*0.5f - near );
Gizmos.color = new Color(0,1,0,0.05f); Gizmos.DrawCube( localOffset , size );
Gizmos.color = new Color(1,1,0,1); Gizmos.DrawWireCube( localOffset , size );
}
}
}
}
Thank you very much. Thanks to your code, I wonder if there is a problem with my own project.
I reloaded the project layout and was surprised to find that everything worked out.
For some reason, my previous project layout caused my camera to lose even its edge lines.
In a word, you have successfully solved my problem, I wish you my best wishes.