Example #1
0
    void UpdateReadFingerCreatePoints()
    {
        Vector2 fingerPos = Input.mousePosition;

        // NOTE: this works because we're using an orthographic camera.
        // Per Unity's camera setup, orthoSize is half the screen height.

        // If you are using a perspective camera, cast a ray into the scene
        // to where your zero plane is and get the position that way.

        fingerPos           -= ScreenCenter;
        worldFingerPosition  = (fingerPos * CameraOrthoSize) / (Screen.height / 2);
        worldFingerPosition += CameraCenterAxis;

        if (Input.GetMouseButtonDown(0))
        {
            fingerDown        = true;
            lastWorldPosition = worldFingerPosition;

            Points = new List <Vector2>();

            Points.Add(worldFingerPosition);
        }

        if (fingerDown)
        {
            if (Input.GetMouseButton(0))
            {
                float distance = Vector2.Distance(fingerPos, lastWorldPosition);

                if (distance > MinDistanceToConsiderAnEdge)
                {
                    Points.Add(worldFingerPosition);
                    lastWorldPosition = fingerPos;
                }
            }
            else
            {
                if (Input.GetMouseButtonUp(0))
                {
                    if (Points.Count > 2)
                    {
                        Points.Add(worldFingerPosition);

                        var go = MakeCollider2D.Create(Points.ToArray(), DoubleSided: true);

                        go.GetComponent <Renderer>().material = mtlPolygon;

                        go.AddComponent <Rigidbody2D>();
                    }

                    fingerDown = false;
                    Points     = null;
                }
            }
        }
    }
    void Start()
    {
        Vector2[] v1 = new Vector2[] {
            new Vector2(-1, 0),
            new Vector2(0, 1),
            new Vector2(2, 0),
            new Vector2(0, -1),
        };

        var go = MakeCollider2D.Create(v1);

        go.GetComponent <Renderer>().material = mtl1;

        go.AddComponent <Rigidbody2D>();
    }