Unity 2D Мотузка

 

 

DistanceJoint2D rope;
LineRenderer lr;
Vector3 linePos;
bool checker = true;

void Start()
{
  gameObject.AddComponent<Rigidbody2D>();
  lr = gameObject.GetComponent<LineRenderer>();
  lr.enabled = false;
}

void Update()
{
  Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

  if (Input.GetMouseButtonDown(0) && checker == true)
  {
    rope = gameObject.AddComponent<DistanceJoint2D>();
    rope.connectedAnchor = mousePos;
    linePos = mousePos;
    checker = false;
  }
  else if (Input.GetMouseButtonDown(0))
  {
    DestroyImmediate(rope);
    checker = true;
  }

  if (checker == false)
  {
    lr.enabled = true;
    lr.SetPosition(0, linePos);
    lr.SetPosition(1, gameObject.transform.position);
  }
  else
  {
    lr.enabled = false;
  }
}