| 正多边形墙体生成 所使用编辑器版本:Online_v0.22.0.2 咱们开发游戏的时候,会遇到要搭建一个包围起来的关卡场景,如果在场景上拖物体去慢慢拼凑的话,会比较费时间,同时如何让墙体之间严丝合缝也是一个比较头疼的事情。那么!快使用正多半形墙体生成代码吧!能够帮助你快速的在场景中创建一个极具美感的正多边形!
 
/*** 创建一个正多边形
 * @param count 边数
 * @param pos 位置
 * @param length 边长
 * @param height 高度
 * @param thickness 厚度
 */
 private async creatPolygon(count: number, pos: Type.Vector, length: number, height: number = 200, thickness: number = 20) {
 
 console.log("这是一个正" + count + "边形")
 
 const angle = 360 / count
 
 // 半径
 const Radius = Math.abs(length / (2 * Math.sin(180 * (Math.PI / 180) / count) * (180 / Math.PI)))
 
 // 边心距
 const Apothem = Math.abs(Radius * Math.cos(180 * (Math.PI / 180) / count) * (180 / Math.PI))
 
 for (let i = 0; i < count; i++) {
 
 // 创建一个正方体来模拟墙体
 let oneSide = await Core.GameObject.asyncSpawn({ "guid": "7669" })
 // 改变墙体的长宽高
 oneSide.worldScale = new Type.Vector(thickness / 100, length / 100, height / 100)
 // 墙体旋转
 oneSide.worldRotation = new Type.Rotation(0, 0, angle * i)
 // 墙体朝旋转后的方向移动边心距个单位
 oneSide.worldLocation = pos.clone().add(oneSide.forwardVector.multiply(Apothem))
 
 console.log("边" + i + "的Transform:" + oneSide.transform)
 }
 
 }
 |