EdgeCollider following LineRenderer with a weird Offset

Im trying to do a Curve Fever Clone but for some reason when I instantiate the Snake the EdgeCollider is following with a weird Offset. When I dont Instantiate and let it in the GameScene it doesnt do this.

My Line Code:

[RequireComponent(typeof(LineRenderer))]
[RequireComponent(typeof(EdgeCollider2D))]
public class Line : MonoBehaviour
{
    LineRenderer linerenderer = new LineRenderer();
    EdgeCollider2D edgecollider = new EdgeCollider2D();

    List<Vector2> pointsList;
    public float padding = .1f;
    public Transform headList;

    // Start is called before the first frame update
    void Start()
    {
        linerenderer = GetComponent<LineRenderer>();
        edgecollider = GetComponent<EdgeCollider2D>();
        pointsList = new List<Vector2>();
        spawnPoint();
    }

    // Update is called once per frame
    void Update()
    {
        if (Vector3.Distance(pointsList.Last(), headList.position) > padding)
        {
            spawnPoint();
        }
    }

    void spawnPoint()
    {
        if (pointsList.Count > 1)
        {
            edgecollider.points = pointsList.ToArray<Vector2>();
        }

        pointsList.Add(headList.position);
        linerenderer.positionCount = pointsList.Count;
        linerenderer.SetPosition(pointsList.Count - 1, headList.position);
    }
}

My PlayerSpawner which doenst Instantiate propely:

public class PlayerSpawner : MonoBehaviour
{
    [SerializeField] float padding = .5f;
    [SerializeField] List<GameObject> playerList = new List<GameObject>();


    private float xMin, xMax, yMin, yMax;
    private int Index = 0;

    // Start is called before the first frame update
    void Start()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;

        foreach (var player in playerList)
        {
            SpawnUpside();
            Index++;
        }
    }

    void SpawnUpside()
    {
        if (Random.Range(0, 2) == 1)
        {
            Vector3 randomPosition = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
            Instantiate(playerList[Index], randomPosition, Quaternion.identity);
        }

        else
        {
            Vector3 randomPosition = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
            Instantiate(playerList[Index], randomPosition, Quaternion.Euler(0, 0, 180));
        }
    }
}

4401820--400561--upload_2019-4-7_14-11-36.png

This is a very late reply but I just had the same problem. It appears as if the EdgeCollider uses the points in local space, so relative to the transform of the GameObject it is attached to, while the LineRenderer does not do that. That solved it for me. Hope this helps someone.

1 Like

You can make the LineRenderer use local space by setting “Use World Space” to false in the inspector or via code:

1 Like

FYI: All physics colliders do whether it be points on an EdgeCollider2D, the center point of a Circle, the points in a path on a Polygon, a 3D mesh on a 3D collider etc. It wouldn’t make sense to have them in world-space because then the Transform wouldn’t mean anything.