С физикой то бишь с "начинкой" крюка справился. теперь проблема визуальная часть. У меня есть спрайт с звенью цепи и я пытаюсь с помощью
LineRenderer-а при каждом "выстреле " крюка рисовать от героя до нажатой точки цепь. Но проблема в том что я не знаю как для
LineRenderer-а посчитать позиции чтоб нарисовать крюк.
[RequireComponent(typeof(DistanceJoint2D), typeof(LineRenderer))]
public class HookComponent : MonoBehaviour
{
[SerializeField] private LayerMask _layer;
[SerializeField] private float _distance;
[SerializeField] private float _minDistance;
[SerializeField] private float _hookSpeed;
private RaycastHit2D _raycast;
private Vector3 _mousePosition;
private DistanceJoint2D _joint;
private LineRenderer _lineRenderer;
#region MonoBehaviorMethods
void Start()
{
_joint = GetComponent<DistanceJoint2D>();
_lineRenderer = GetComponent<LineRenderer>();
_joint.autoConfigureDistance = false;
_joint.enabled = false;
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
//Click Prossesing
_mousePosition = this.GetMousePositionInWorld();
bool isHittedInRange = _mousePosition.x - transform.position.x <= _distance;
if (!isHittedInRange) return;
_raycast = Physics2D.Linecast(transform.position, _mousePosition, _layer);
//Gizmos Block
bla = _mousePosition;
gizEnab = true;
if (_raycast.collider != null)
{
// Joint prossesing. prossesing of distantJoint2D.ConnectedBody(rigitbody) and
// distantJoint2D.ConnectedBodyAnchor(float)
var connectableGameObject = _raycast.collider.gameObject; // Получение геймОбдж из рейкаста
_joint.enabled = true;
_joint.connectedBody = connectableGameObject.GetComponent<Rigidbody2D>();
var position = connectableGameObject.transform.position;
var xAnchorPoss = _mousePosition.x - position.x;
var yAnchorPoss = _mousePosition.y - position.y;
_joint.connectedAnchor = new Vector2(xAnchorPoss, yAnchorPoss);
var mousePositionInWordWithoutZ = new Vector3(_mousePosition.x, _mousePosition.y, 0);
_joint.distance = (transform.position - mousePositionInWordWithoutZ).magnitude;
_lineRenderer.enabled = true;
}
}
}
private void FixedUpdate()
{
if (_joint.enabled)
{
if (_joint.distance <= _minDistance)
{
_joint.connectedBody = null;
_joint.enabled = false;
_lineRenderer.enabled = false;
return;
}
_joint.distance -= _hookSpeed;
}
}
#endregion
#region MyMethods
private Vector3 GetMousePositionInWorld()
{
var inputMousePosition = Input.mousePosition;
return Camera.main.ScreenToWorldPoint(inputMousePosition);
}
#endregion
private bool gizEnab = false;
private Vector3 bla;
private void OnDrawGizmosSelected()
{
if (gizEnab)
Gizmos.DrawLine(transform.position, bla);
}
}