英文:
How to display hitboxes in game?
问题
我想在游戏中使用调试模式显示我的角色的碰撞框。
是否有可能这样做?如何显示它们?
这是一个具有不同形状碰撞器的3D游戏。
谢谢,祝你有个愉快的一天。
英文:
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.
答案1
得分: 1
你可以尝试使用OnDrawGizmos()方法绘制不同的形状。
要创建碰撞框的可视化,还可以结合Gizmos.DrawCube来创建一个3D框,并定义它的位置(将是角色的中心)和大小(将与碰撞器大小相同)。
这是一个直接来自Unity文档的例子:
using UnityEngine;
using System.Collections;
public class CharacterClass : MonoBehaviour
{
void OnDrawGizmos()
{
// 在transform的位置画一个绿色的框
Gizmos.color = Color.green;
Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
}
}
如果你想使其更加动态,你可以获取到你的盒碰撞器的引用,并直接使用它的大小,而不是每次手动添加,就像这样:
using UnityEngine;
using System.Collections;
public class CharacterClass : MonoBehaviour
{
private BoxCollider collider;
void Awake()
{
collider = GetComponent<BoxCollider>();
}
void OnDrawGizmos()
{
// 在transform的位置画一个绿色的框
Gizmos.color = Color.green;
Gizmos.DrawCube(transform.position, collider.size);
}
}
更新的版本,在运行时使用Debug.DrawLine:
public Color boxColor = Color.green;
void DrawHitBox()
{
// 碰撞器的位置加上中心点
Vector3 boxPosition = boxCollider.transform.position + boxCollider.center;
Vector3 boxSize = boxCollider.size;
// 定义碰撞框的八个角的点
// ...
// 使用Debug.DrawLine绘制框的各个边
Debug.DrawLine(point1, point2, boxColor);
// ...
}
void Update()
{
DrawHitBox();
}
英文:
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();
}
答案2
得分: 0
你可以尝试简单地使用物理调试可视化来显示场景视图中的游戏对象的碰撞体。要打开它,打开窗口:窗口 > 分析 > 物理调试器。
关于此内容的更多信息可以在Unity手册中找到:https://docs.unity3d.com/Manual/PhysicsDebugVisualization.html
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论