如何在游戏中显示碰撞框?

huangapple go评论98阅读模式
英文:

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&#39;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&lt;BoxCollider&gt;();
    }
    
    void OnDrawGizmos()
    {
        // Draw a green box at the transform&#39;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

huangapple
  • 本文由 发表于 2023年5月25日 16:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定