Guest User

Untitled

a guest
Jul 19th, 2021
2,736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Line : MonoBehaviour {
  6. [SerializeField] private LineRenderer _renderer;
  7. [SerializeField] private EdgeCollider2D _collider;
  8.  
  9. private readonly List<Vector2> _points = new List<Vector2>();
  10. void Start() {
  11. _collider.transform.position -= transform.position;
  12. }
  13.  
  14.  
  15. void Update()
  16. {
  17.  
  18. }
  19.  
  20. public void SetPosition(Vector2 pos) {
  21. if(!CanAppend(pos)) return;
  22.  
  23. _points.Add(pos);
  24.  
  25. _renderer.positionCount++;
  26. _renderer.SetPosition(_renderer.positionCount-1,pos);
  27.  
  28. _collider.points = _points.ToArray();
  29. }
  30.  
  31. private bool CanAppend(Vector2 pos) {
  32. if (_renderer.positionCount == 0) return true;
  33.  
  34. return Vector2.Distance(_renderer.GetPosition(_renderer.positionCount - 1), pos) > DrawManager.RESOLUTION;
  35. }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment