- Home /
Using Gizmos.DrawWireCube to draw position bounds?
I am trying to draw the rectangular bounds of a BoxCollider2D that holds the bounds of where my camera can go. Unfortunately I'm having some trouble acquiring the x, y coordinates and applying them via scripting. I can draw a box but can't figure out how to make it the exact dimensions of the "green" outline.
Here is what I have:
using UnityEngine;
using System.Collections;
public class DrawCameraBounds : MonoBehaviour {
private float CamBoundsX;
private float CamBoundsY;
void Start()
{
//have to assign variable meaning in function?
//CamBounds = GameObject.Find("CameraBounds").GetComponent<BoxCollider2D>;
CamBoundsX = collider.bounds.size.x;
CamBoundsY = collider.bounds.size.y;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(transform.position, new Vector2(CamBoundsX+5 , CamBoundsY+5));
}
}
Thanks in advance.
EDIT: Here is what I currently have (I changed the BoxCollider2D to a BoxCollider):
public class DrawCameraBounds : MonoBehaviour {
public float CamBoundsX;
public float CamBoundsY;
void Start()
{
CamBoundsX = GetComponent<BoxCollider>().collider.bounds.size.x;
CamBoundsY = GetComponent<BoxCollider>().collider.bounds.size.y;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(transform.position, new Vector2(CamBoundsX+5, CamBoundsY+5));
}
}
I also experimented with this, but it didn't seem to make any difference.
public class DrawCameraBounds2 : MonoBehaviour {
private Vector3 CamBounds;
void Start()
{
CamBounds = GameObject.Find("CameraBounds").GetComponent<BoxCollider>().size;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(transform.position, CamBoundsX);
}
}
Answer by Pete12345 · Jul 03, 2020 at 09:43 AM
In case anyone needs this in the future. This code from github will give you the results without rotation.
https://gist.github.com/unitycoder/58f4b5d80f423d29e35c814a9556f9d9
.
.
If you do need rotation then this thread shows how to rotate points around a pivot, so you can do that for each point.
https://answers.unity.com/questions/532297/rotate-a-vector-around-a-certain-point.html
Your answer
Follow this Question
Related Questions
Console error when trying to use gizmos? 2 Answers
Cube Movement using Touch?? 0 Answers
BoundsInt.allPositionsWithin is not working? 1 Answer
Checki f Player is within area? C# 1 Answer
Draw line on cube ? 2 Answers