I am participating in the nuggets Community game creative submission Contest. For details, please see: Game Creative Submission Contest

Functional requirements of physics mini-games:

After the LineRenderer is drawn in real time, PolygonCollider2D is added dynamically. Read the LineRenderer’s point position (a polyline) and offset the left and right sides by a certain distance to form a closed wireframe. Use the point position of the wireframe to modify the PolygonCollider2D shape.

Workflow:

  • Listen for mouse events through Input
  • Press the mouse to start drawing LineRenderer graphics
  • Refresh graphics in real time during mouse movement
  • Lift the mouse to finish drawing and add a collision box

TouchPosList is a line of finger movement track points, just a broken line, can be used to draw LineRenderer, and can be used to draw LineRenderer display width; It can’t be used directly to generate a PolygonCollider, so you need to convert the point line to a wide face. Here, a point is offset by a certain distance along the normal direction and the opposite direction of the normal to generate two new points, which are connected in sequence to form a closed contour.

Green is the captured point and purple is the offset point:

LineRenderer. SetPositions (Vector3 [] positions) :

  • Sets the graph drawing path for LineRenderer.

LineRenderer.SetWidth(float start,float end):

  • Set the LineRenderer start and end widths.

PolygonCollider.SetPath(int index, Vector2[] points):

  • Index: A polygon may have holes and discontinuous parts, so its shape is not necessarily defined by a single path. For example, a polygon might actually have three separate paths. In this case SetPath(0), SetPath(1), SetPath(2) will be called.
  • Points: A cyclic sequence of line segments that define the points of a polygon outline. Points can be enclosed into a closed and uncrossed polyline.

Complete test code, key code functions are written in the comments:

using System.Collections.Generic; using UnityEngine; Public class LineCollider: MonoBehaviour {// LineRenderer [SerializeField] LineRenderer drawingLine; // PolygonCollider2D, LineRenderer, [SerializeField] GameObject colliderLinePrefab; // sensitivity [SerializeField] float minTouchDistance = 0.1f; / / the Camera Settings for orthogonal mode, can be converted to the world coordinate tmpTouchPos = mainCamera. ScreenToWorldPoint (Input. MousePosition) Camera mainCamera; void Start() { mainCamera = Camera.main; // drawingLine.enabled = false; drawingLine.positionCount = 0; } Vector3 tmpTouchPos; bool tmpIsMouseDown; void Update() { if (Input.GetMouseButtonDown(0)) { tmpIsMouseDown = true; tmpTouchPos = mainCamera.ScreenToWorldPoint(Input.mousePosition); tmpTouchPos.z = 0; OnPressScreen(tmpTouchPos); } else if (Input.GetMouseButton(0) && tmpIsMouseDown) { tmpTouchPos = mainCamera.ScreenToWorldPoint(Input.mousePosition); tmpTouchPos.z = 0; OnTouchScreen(tmpTouchPos); } else if (Input.GetMouseButtonUp(0) && tmpIsMouseDown) { tmpTouchPos = mainCamera.ScreenToWorldPoint(Input.mousePosition); tmpTouchPos.z = 0; OnReleaseScreen(tmpTouchPos); tmpIsMouseDown = false; } // Click Vector3 lastPos; List<Vector3> touchPosList = new List<Vector3>(); public void OnPressScreen(Vector3 pressPos) { touchPosList.Clear(); lastPos = pressPos; drawingLine.enabled = true; drawingLine.positionCount = 0; } // Drag and drop the Vector3 tmpStartPoint; Vector3 tmpEndPoint; public void OnTouchScreen(Vector3 touchPos) { tmpStartPoint = lastPos; tmpEndPoint = touchPos; Vector3.Distance(tmpStartPoint, tmpEndPoint) > minTouchDistance) {touchposList.add (lastPos); drawingLine.positionCount = touchPosList.Count; drawingLine.SetPosition(touchPosList.Count - 1, touchPosList[touchPosList.Count - 1]); lastPos = touchPos; }} / / mouse raise public void OnReleaseScreen (Vector3 releasePos) {drawingLine. PositionCount = 0; drawingLine.enabled = false; Line touchposList. Add(releasePos); if (touchPosList.Count > 3) CreateColliderLine(touchPosList); } line void CreateColliderLine(List<Vector3> pointList) {GameObject prefab = Instantiate(colliderLinePrefab, transform); LineRenderer lineRenderer = prefab.GetComponent<LineRenderer>(); PolygonCollider2D polygonCollider = prefab.GetComponent<PolygonCollider2D>(); lineRenderer.positionCount = pointList.Count; lineRenderer.SetPositions(pointList.ToArray()); List<Vector2> colliderPath = GetColliderPath(pointList); polygonCollider.SetPath(0, colliderPath.ToArray()); } // Calculate the collider outline float colliderWidth; List<Vector2> pointList2 = new List<Vector2>(); List<Vector2> GetColliderPath(List<Vector3> pointList3) {colliderWidth = drawingLine. / / Vector3 Vector2 pointList2. The Clear (); for (int i = 0; i < pointList3.Count; i++) { pointList2.Add(pointList3[i]); } // List<Vector2> edgePointList = new List<Vector2>(); Int j = 1; int j = 1; int j = 1; int j = 1; j < pointList2.Count; Vector2 distanceVector = pointList2[j-1] -pointList2 [j]; Cross(distanceVector, vector3. forward); // normalized, unit vector Vector2 offectVector = crossVector.normalized; Vector2 up = pointList2[J-1] + 0.5f * colliderWidth * offectVector; Vector2 down = pointList2[J-1] -0.5f * colliderWidth * offectVector; Vector2 down = pointList2[J-1] -0.5f * colliderWidth * offectVector; Edgepointlist. Insert(0, down); edgePointList.Insert(0, down); edgePointList.Add(up); If (j == pointList2.count-1) {up = pointList2[j] + 0.5f * colliderWidth * offectVector; Down = pointList2[j] -0.5f * colliderWidth * offectVector; edgePointList.Insert(0, down); edgePointList.Add(up); }} return edgePointList; }}Copy the code