因为项目中有很多类似三菱柱的物体,如果使用meshcollider的话,模型边的弧度会有很多碰撞线,所以就想着能不能自己绘制

一个三菱柱的碰撞体。思路是首先绘制出三菱柱,然后在添加meshcollidre,这样就能绘制出来了。

然后参考博客:http://www.cnblogs.com/JLZT1223/p/6089996.html

首先上效果图:

  

然后下面是代码:

/**************************************************************************  FileName: DrawTriangle.cs*  Author: LaiZhangYin(Eagle)   Version: 1.0   Date: 6/21/2017* if you have some question, please call *  QQ/Wechat : 782966734*************************************************************************/using UnityEngine;
using System.Collections;[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class DrawTriangle : BaseMonoBehaviour
{public float length = 6;public float width = 6;public float height = 6;private MeshFilter meshFilter;void Start(){meshFilter = GetComponent<MeshFilter>();meshFilter.mesh = CreateTriangle(length, width, height);if (this.gameObject.AddComponent<MeshCollider>() == null)this.gameObject.AddComponent<MeshCollider>();this.GetComponent<MeshRenderer>().enabled = false;}private Mesh CreateTriangle(float length, float width, float height){int verticesCount = 4 * 6;//3* 4;Vector3[] vertices = new Vector3[verticesCount];vertices[0] = Vector3.zero;                 //前面左下角的点vertices[1] = new Vector3(0, height, 0);    //前面左上角的点vertices[2] = new Vector3(length, 0, 0);   // 前面右下角的点    vertices[3] = new Vector3(length, height, 0);//前面右上角的点vertices[4] = Vector3.zero;//new Vector3(length, 0, width);        //后面右下角的点vertices[5] = Vector3.zero;//new Vector3(length, height, width);    //后面右上角的点vertices[6] = new Vector3(0, 0, width);             //后面左下角的点vertices[7] = new Vector3(0, height, width);        //后面左上角的点vertices[8] = vertices[6];                              //左vertices[9] = vertices[7];vertices[10] = vertices[0];vertices[11] = vertices[1];vertices[12] = vertices[2];                              //右vertices[13] = vertices[3];vertices[14] = vertices[6];vertices[15] = vertices[7];vertices[16] = vertices[1];                              //上vertices[17] = vertices[7];vertices[18] = vertices[3];vertices[19] = vertices[7];vertices[20] = vertices[2];                              //下vertices[21] = vertices[6];vertices[22] = vertices[0];vertices[23] = vertices[6];int triangleCount = 6 * 2 * 3;int[] triangles = new int[triangleCount];for (int i = 0, v = 0; i < triangleCount; i += 6, v += 4){triangles[i] = v;triangles[i + 1] = v + 1;triangles[i + 2] = v + 2;triangles[i + 3] = v + 3;triangles[i + 4] = v + 2;triangles[i + 5] = v + 1;}Mesh mesh = new Mesh();mesh.vertices = vertices;mesh.triangles = triangles;return mesh;}#if UNITY_EDITOR//用于在编辑状态设置大小void OnDrawGizmos(){meshFilter = GetComponent<MeshFilter>();meshFilter.mesh = CreateTriangle(length, width, height);}
#endif
}

对于绘制出来的下面有一个图便于理解:

然后就是根据这个图的示意来绘制三菱柱了。

完!

使用unity的mesh绘制三菱柱的碰撞体(3d三角形)相关推荐

  1. Unity自定义mesh绘制

    2019独角兽企业重金招聘Python工程师标准>>> 有些时候需要自定义mesh来绘制目标模型图形什么的,可以代码控制,也可以通过shader去控制,这里介绍代码控制的方法: 基本 ...

  2. 【Unity入门计划】制作RubyAdventure03-使用碰撞体触发器实现世界交互

    目录 8 添加生命系统 演示效果 9 使用触发器实现吃血包 9.1 添加一个可以被吃掉的血包 10 使用触发器添加伤害区域 10.1 设置伤害区域 10.2 设置无敌时间 关于无敌时间的讨论 10.3 ...

  3. 学习笔记:unity通过Mesh网格绘制图形:三角形正方体圆柱

    一,介绍 Mesh类:通过脚本创建或是获取网格的类,网格包含多个顶点和三角形数组.顶点信息包含坐标和所在面的法线. unity中3D的世界的所有图形全部都是由三角形构成的. 比如unity已经装配好的 ...

  4. [Unity]利用Mesh在Unity中绘制扇形图片

    背景 最近碰到个功能, 要画一个扇形图案, 如下图: 美术原图: 需求是这个图形跟随角色, 在角色背后, 并且每个角色的扇形角度可能不同. So, NGUI和UGUI很好用的FilledType是用不 ...

  5. 关于Unity中Mesh网格的详解

    3D模型 通过3D建模软件所建出来的点和面,如以三角形为主的点和面,比如人的脑袋一个球,就是由各种各样的三角形组成的点和面. 点和面以及纹理坐标都是通过3D建模软件建模出来的. Unity会帮我们把模 ...

  6. Unity使用bounds绘制不规则图形边框

    Unity使用bounds绘制不规则图形边框 for (int i = 0; i < screenList.Count; i++) { if (screenList[i].activeSelf ...

  7. Unity用Mesh画多边形、圆形

    首先贴上效果图 上面两个图就是使用Unity的Mesh画出的正五边形和正八变形. 无论是研究过Mesh还是只是有所听说,应该都听过网格,上面两个图形就是利用来网格的特性来实现的. 首先讲一下Mesh原 ...

  8. unity Text Mesh Pro Sprite Animation 支持动图,动态表情

    unity Text Mesh Pro Sprite Animation 支持动图,动态表情 使用格式 <sprite='assetName' anim='first frame, last f ...

  9. Unity的Mesh 应用---视野可视化Field of view

    csdn 这垃圾工具, 一保存就丢内容!!! https://github.com/SunGuangdong/Field-of-View https://www.youtube.com/watch?v ...

最新文章

  1. 【组队学习】一月微信图文索引
  2. [导入]Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
  3. 数据库数据满足树结构时,求一个结点的子结点有哪些
  4. cheat sheet for cs221 at stanford: good luck
  5. 第26讲:模拟登录爬取实战案例
  6. php实际开发过程,4.工作实际开发应用
  7. mysql 5.7.4 m14_win7 64位下如何安装配置mysql-5.7.4-m14-winx64(安装记录)
  8. Compass样式重置
  9. Linux-Ubuntu 远程登录服务器--ssh的安装和配置
  10. 视频编解码(十六):VE解码器解码demo解码流程
  11. git切换分支出现head is now at_git寻根——^和~的区别
  12. 通过迁移的方式解决Active Directory服务器问题之5-恢复服务器的应用
  13. mysql得花多少钱,php-我需要帮助来解决为什么mySQL查询花费比...
  14. L1- 017——024
  15. SAR图像:拟合杂波时常用的分布
  16. html5科技感logo,这些LOGO设计得太巧妙了!
  17. 计算机选购知识,计算机知识讲座(9):硬盘的选购
  18. android实现向右滑动返回功能
  19. 三国皇帝的寡妇秘史(1)
  20. 用yolov5做人脸检测

热门文章

  1. 风变编程python26_风变编程学习Python的切身体会
  2. python3刷新监控网页_python - 如何不刷新网页而监控网页变化?
  3. 太阳能电池板自动清洗机器人的制作分享
  4. 【转载】完全版线段树 by notonlysuccess大牛
  5. 数字证书认证机构(摘录自wiki百科)
  6. 理解单隐层ReLU神经网络的全局损失
  7. Vue+Element-Ui项目中使用qrcodejs2将链接生成二维码
  8. vscode格式化html代码失效了,VSCode格式化代码功能失效的bug解决方法
  9. 6代cpu装win7
  10. 腾讯云mysql服务cpu毛刺问题