// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0) || _has_just_fake_clicked) { Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (_has_just_fake_clicked) { mousePos = Camera.main.ScreenToWorldPoint(_fake_click_point); } mousePos.z = 0; _numHits = Physics2D.RaycastNonAlloc(transform.position, mousePos - transform.position, _tongueHits, float.PositiveInfinity, 1 << LayerMask.NameToLayer("Geometry")); if (_numHits > 0) { _swingPoints.Clear(); _swingPoints.Add(_tongueHits[0].point); gameObject.GetComponent <Logger>().registerMouseCoordinates(_tongueHits[0].point); // log mouse coords _updateTongue = true; if (RopeInsteadOfSpring) { // Cancel most velocity in direction away from swing point Vector2 forceDir = (_tongueHits[0].point - (Vector2)transform.position).normalized; Rigidbody2D rb = GetComponent <Rigidbody2D>(); float forceMag = Vector2.Dot(rb.velocity, -forceDir); rb.velocity += forceDir * forceMag; } } } else if ((Input.GetMouseButtonUp(0) || _has_just_fake_released) && _swingPoints.Count > 0) { _swingPoints.Clear(); _updateTongue = true; } // Check for collisions along tongue length if (_swingPoints.Count > 0) { _numHits = Physics2D.LinecastNonAlloc(transform.position, _swingPoints.Last(), _tongueHits, 1 << LayerMask.NameToLayer("Geometry")); if (_numHits > 0 && Vector2.Distance(_tongueHits[0].point, _swingPoints.Last()) > 0.1) { _swingPoints.Add(_tongueHits[0].point); _updateTongue = true; } } // Check if any (last) connections are now unnecessary. if (_swingPoints.Count > 1) { _numHits = Physics2D.LinecastNonAlloc(transform.position, _swingPoints[_swingPoints.Count - 2], _tongueHits, 1 << LayerMask.NameToLayer("Geometry")); if (_numHits == 0 || Vector2.Distance(_tongueHits[0].point, _swingPoints[_swingPoints.Count - 2]) < 0.1) { _swingPoints.Remove(_swingPoints.Last()); _updateTongue = true; } } // Recalculate lines and joints if necessary. if (_updateTongue) { tongue = null; if (_swingPoints.Count > 0) { tongue = new Rope(transform.position, _swingPoints.Last(), TongueStrength); } } // Decrease tongue length if the button is held down if (tongue != null && (Input.GetMouseButton(0) || _is_fake_clicking) && _swingPoints.Count > 0) { if (tongue.length > MinTongueLength) { tongue.length -= TongueRetractSpeed * Time.deltaTime; } } // Add force if (tongue != null) { GetComponent <Rigidbody2D>().AddForce(tongue.Force(transform.position, _swingPoints.Last())); } // Draw lines _lineRenderer.enabled = _swingPoints.Count > 0; if (_updateTongue) { _lineRenderer.positionCount = _swingPoints.Count; _lineRenderer.SetPositions(_swingPoints.ToArray()); _lineRenderer.positionCount += 1; } _lineRenderer.SetPosition(_lineRenderer.positionCount - 1, transform.position); if (_updateTongue) { _updateTongue = false; } if (_received_input_last_frame) { _has_just_fake_clicked = false; _has_just_fake_released = false; _received_input_last_frame = false; } else if (_has_just_fake_clicked || _has_just_fake_released) { _received_input_last_frame = true; } }