I want to display the hitboxes of my character in game with a debug mod. Is it possible to do so ? How to display them ?
It's a 3d game with different shapes of colliders. Thank you and have a good day.
I want to display the hitboxes of my character in game with a debug mod. Is it possible to do so ? How to display them ?
It's a 3d game with different shapes of colliders. Thank you and have a good day.
You can try and use the OnDrawGizmos() method to draw different of shapes.
To create a visuallization of your hitbox you can also combine the Gizmos.DrawCube to create a 3D box and define it's position which will be the center of your character and it's size which will be the same as your collider size.
Here is an example directly from Unity's documentation:
using UnityEngine;
using System.Collections;
public class CharacterClass : MonoBehaviour
{
void OnDrawGizmos()
{
// Draw a green box at the transform's position
Gizmos.color = Color.green;
Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
}
}
If you want to make this more dynamic, you get a reference of your box collider and directly use it's size instead of adding it manually for each time, like this:
using UnityEngine;
using System.Collections;
public class CharacterClass : MonoBehaviour
{
private BoxCollider collider;
void Awake()
{
collider = GetComponent<BoxCollider>();
}
void OnDrawGizmos()
{
// Draw a green box at the transform's position
Gizmos.color = Color.green;
Gizmos.DrawCube(transform.position, collider.size);
}
}
Updated Version to work at runtime using Debug.DrawLine
public Color boxColor = Color.green;
void DrawHitBox()
{
Vector3 boxPosition = boxCollider.transform.position + boxCollider.center;
Vector3 boxSize = boxCollider.size;
Vector3 point1 = boxPosition + new Vector3(-boxSize.x, -boxSize.y, -boxSize.z) * 0.5f;
Vector3 point2 = boxPosition + new Vector3(-boxSize.x, -boxSize.y, boxSize.z) * 0.5f;
Vector3 point3 = boxPosition + new Vector3(boxSize.x, -boxSize.y, boxSize.z) * 0.5f;
Vector3 point4 = boxPosition + new Vector3(boxSize.x, -boxSize.y, -boxSize.z) * 0.5f;
Vector3 point5 = boxPosition + new Vector3(-boxSize.x, boxSize.y, -boxSize.z) * 0.5f;
Vector3 point6 = boxPosition + new Vector3(-boxSize.x, boxSize.y, boxSize.z) * 0.5f;
Vector3 point7 = boxPosition + new Vector3(boxSize.x, boxSize.y, boxSize.z) * 0.5f;
Vector3 point8 = boxPosition + new Vector3(boxSize.x, boxSize.y, -boxSize.z) * 0.5f;
Debug.DrawLine(point1, point2, boxColor);
Debug.DrawLine(point2, point3, boxColor);
Debug.DrawLine(point3, point4, boxColor);
Debug.DrawLine(point4, point1, boxColor);
Debug.DrawLine(point5, point6, boxColor);
Debug.DrawLine(point6, point7, boxColor);
Debug.DrawLine(point7, point8, boxColor);
Debug.DrawLine(point8, point5, boxColor);
Debug.DrawLine(point1, point5, boxColor);
Debug.DrawLine(point2, point6, boxColor);
Debug.DrawLine(point3, point7, boxColor);
Debug.DrawLine(point4, point8, boxColor);
}
void Update()
{
DrawHitBox();
}
You can try to simply use the Physics Debug Visualization to show the colliders of your GameObjects in the Scene view. To open this, open the window: Window > Analysis > Physics Debugger
More information can be found here on the Unity manual: https://docs.unity3d.com/Manual/PhysicsDebugVisualization.html