Gizmos讲解

本文详细介绍了Unity中的Gizmos组件,包括colorGizmos、DrawCube、DrawFrustum、DrawGUITexture和DrawWireSphere的使用,重点展示了如何用DrawWireSphere进行物体碰撞范围检测。通过实例演示了如何设置边界颜色、绘制立方体和视锥体,以及在GUI上显示纹理和检测指定范围内的玩家角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gizmos.color

这里可以设置可视化边界的颜色。如:
Gizmos.color= Color.red;
可设置边界为红色。

Gizmos.DrawCube

public static void DrawCube(Vector3 center, Vector3 size);
第一个参数需要给一个坐标,第二个参数需要设置描绘的长方体的长、宽、高的大小及比例。如:

// private void OnDrawGizmosSelected() { 
        Gizmos.color = Color.red;
        Gizmos.DrawCube(transform.position,new Vector3(5,1,5));
    }

效果:
在这里插入图片描述

Gizmos.DrawFrustum

public static void DrawFrustum(Vector3 center, float fov, float maxRange, float minRange, float aspect);
float fov:垂直视野
float maxRange:截头远平面的距离
float minRange:截头近平面的距离
float aspect:宽高比
这主要用于绘制相机可视区域。

Gizmos.DrawGUITexture

public static void DrawGUITexture(Rect screenRect, Texture texture, int leftBorder, int rightBorder, int topBorder, int bottomBorder, Material mat = null);

//     public Texture myTexture;
    private void OnDrawGizmosSelected() { 
        Gizmos.color = Color.red;
        Gizmos.DrawGUITexture(new Rect(5,5,10,10),myTexture);
    }

在这里插入图片描述

Gizmos.DrawWireSphere

public static void DrawWireSphere(Vector3 center, float radius);
这里可以描绘一个圆,第一个参数是设置圆中心的位置,第二个参数是设置圆的半径。如:
在这里插入图片描述

通过Gizmos.DrawWieeSphere检测范围中的物体

//  //设置一个碰撞器的数组
        Collider[] cols;
        //数组的成员为Center范围内的碰撞器
        cols=Physics.OverlapSphere(Center.transform.position, audRange);
        //遍历数组成员,寻找是否含有玩家
        foreach (Collider col in cols) { if (col.tag == "Player") { return true; } }

Center.transform.position应该和DrawWireSphere设置的中心相等, audRange应该与DrawWieeSphere设置的半径相等,这样就可以检测范围中的物体。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值