У меня есть объект с игроком и точка, за которую должен цепляьться игрок. Все происходит как надо, но почему-то connected anchor равен не 0, а цифрам кривым. Выходит, что он смещен. Почему так происходит и как избавиться?
private DistanceJoint2D joint;
private Vector3 target;
public float distance = 10f;
public LayerMask mask;
public LineRenderer line;
public List<Transform> ListPoint = new List<Transform>();
public float swingForce = 5f;
public float swingSpeed = 2f;
private bool isSwinging = false;
private float swingDirection = 1f;
private float swingTimer = 0f;
RaycastHit2D rayCast;
private void Start()
{
joint = GetComponent<DistanceJoint2D>();
joint.enabled = false;
line.enabled = false;
GameObject[] blockObjects = GameObject.FindGameObjectsWithTag("Block");
foreach (GameObject blockObject in blockObjects)
{
ListPoint.Add(blockObject.transform);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Transform closestPoint = ListPoint.OrderBy(p => Vector2.Distance(p.position, transform.position)).FirstOrDefault();
if (closestPoint != null)
{
closestPoint.GetChild(0).transform.Rotate(0, 0, 1);
Debug.Log("Closest Point: " + closestPoint.position);
target = closestPoint.position;
target.z = 0;
Debug.Log("Target: " + target);
rayCast = Physics2D.Raycast(transform.position, target - transform.position, distance, mask);
if (rayCast.collider != null && rayCast.collider.gameObject != gameObject)
{
joint.enabled = true;
joint.connectedAnchor = rayCast.collider.gameObject.transform.position;
joint.distance = Vector2.Distance(transform.position, rayCast.point);
line.enabled = true;
line.SetPosition(0, transform.position);
line.SetPosition(1, rayCast.collider.gameObject.transform.position);
joint.connectedBody = rayCast.collider.gameObject.GetComponent<Rigidbody2D>();
Debug.Log("Target: " + target);
/* isSwinging = true;
swingDirection = 1f;
swingTimer = 0f;*/
}
}
}
if (Input.GetKey(KeyCode.Space))
{
line.SetPosition(0, transform.position);
}
if (Input.GetKeyUp(KeyCode.Space))
{
joint.enabled = false;
line.enabled = false;
isSwinging = false;
}