无法在 Unity 中更改边缘碰撞器的点

Cannot change edge colliders' points in Unity

我正在制作一个学习Unity的小游戏。

在游戏中,角色可以滑动。他通常有一个 EdgeCollider2D。当他滑动时,我希望 EdgeCollider2D 在他滑动时缩小以适应角色模型。

我目前正在尝试执行此操作,如下所示:

public void ChangeColliderForSlide()
{
    //used in animator behavior script
    edge2d.points.SetValue(new Vector2(-0.35f, -0.15f), 0);
    edge2d.points.SetValue(new Vector2(-0.35f, -0.55f), 1);
    edge2d.points.SetValue(new Vector2(0.4f, -0.55f), 2);
    edge2d.points.SetValue(new Vector2(0.4f, -0.15f), 3);
    edge2d.points.SetValue(new Vector2(-0.35f, -0.15f), 4);
}

这会缩小碰撞器,应该在玩家进入幻灯片动画时调用,但是,当我暂停游戏并检查时,角色的碰撞器根本没有改变,并且具有原始值。

如果有人知道这里可能存在的问题,我们将不胜感激。

谢谢。

请注意,您需要先在检查器中设置正确的点数,或者像 points = new Vector2[5]

一样重新初始化数组
    public void ChangeColliderForSlide()
    {
        //used in animator behavior script
        Vector2[] points = edge2d.points;

        points.SetValue(new Vector2(-0.35f, -0.15f), 0);
        points.SetValue(new Vector2(-0.35f, -0.55f), 1);
        points.SetValue(new Vector2(0.4f, -0.55f), 2);
        points.SetValue(new Vector2(0.4f, -0.15f), 3);
        points.SetValue(new Vector2(-0.35f, -0.15f), 4);

        edge2d.points = points;
    }

edge is ok

but the EdgeCollider2D is not

我只是尝试了几个选项来解决这个问题,但 edgeCollider 没有像我期望的那样工作

 Vector2[] rr = new Vector2[2];   
           rr[0] = line.GetPosition(0);
           rr[1] = line.GetPosition(1);
 GetComponent<EdgeCollider2D>().points = rr;

tempFingerPos = Camera.main.ScreenToWorldPoint(new Vector3( Input.mousePosition.x,Input.mousePosition.y,10f));

        line.SetPosition(line.positionCount - 1, tempFingerPos);
        pos.Clear();

        pos.Add(startPos);

      
        pos.Add ( tempFingerPos);

GetComponent().points = pos.ToArray();