Unity PolygonCollider2D代码调整其形状

  面板上看上去是靠Points来规定形状和范围的。

 但是自己用代码测试过:

   weaponPolygonCollider2D[i].points[l].x *= computerEffect;
   weaponPolygonCollider2D[i].points[l].y *= computerEffect;

当computerEffect =1.2的时候,最终的Points并没有因此改变,就像上面说的不可编辑信息。

 这时候就要采用weaponPolygonCollider2D[i].SetPath

PolygonCollider2D.SetPath(int index,Vector2[] points)

路径是定义多边形轮廓线的点之间的线段连续循环。由于多边形可以有孔和不连续的部分,那它形状不一定是由单一路径定义的。[1]所以上面的Vector2是数组。

采用weaponPolygonCollider2D[i].SetPath的代码:

private void ComputerEffect()
    {
        weaponTransform = this.transform.parent.parent.Find("Weapon");
        weaponPolygonCollider2D = weaponTransform.GetComponentsInChildren<PolygonCollider2D>();
        for (int i = 0; i < weaponPolygonCollider2D.Length; i++)
        {
            (x, y) = scriptShip.GetPolygonCollider2DPoints(weaponPolygonCollider2D[i]);
            newPoint = new Vector2[weaponPolygonCollider2D[i].points.Length];
            for (int l = 0; l < weaponPolygonCollider2D[i].points.Length; l++)
            {
                float x1= weaponPolygonCollider2D[i].points[l].x *computerEffect;
                float y1= weaponPolygonCollider2D[i].points[l].y *computerEffect;
                newPoint[l] = new Vector2(x1, y1);
            }
            weaponPolygonCollider2D[i].SetPath(0, newPoint);
        } 
    }

 面板已经发生变化,数值从30变成了36。

[1] Unity5 中文 API 手册.PolygonCollider2D.SetPath 设置路径

2019-12-18/2023-1-5

https://www.wenjiangs.com/doc/unity5-polygoncollider2d-setpath

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值