snap logic
Never
private void OnTriggerEnter(Collider other) { if (_isAlreadySnapped) return; if (other.CompareTag(GameObjectTags.Character)) { _canFall = false; BodyPartComponents bodyPartComponents = other.GetComponent<BodyPartComponents>(); BodyPart leftHand = null, rightHand = null, leftLeg = null, rightLeg = null; List<BodyPart> bodyParts; if (bodyPartComponents) { leftHand = bodyPartComponents.Part.Body.LeftHand; rightHand = bodyPartComponents.Part.Body.RightHand; leftLeg = bodyPartComponents.Part.Body.LeftLag; rightLeg = bodyPartComponents.Part.Body.RightLag; } bodyParts = new List<BodyPart> { leftHand, rightHand, leftLeg, rightLeg }; foreach (var part in bodyParts) if (part) SnapToClosestPoint(part.transform); } } private void SnapToClosestPoint(Transform objectToSnap) { _isAlreadySnapped = true; Transform closestSnapPoint = GetClosestSnapPoint(objectToSnap); Rigidbody rigidbody = objectToSnap.GetComponent<Rigidbody>(); if (rigidbody) rigidbody.isKinematic = true; objectToSnap.SetParent(closestSnapPoint); objectToSnap.position = closestSnapPoint.position; _onVictory.Invoke(); Services.Game.Victory(); } private Transform GetClosestSnapPoint(Transform fromTransform) { KeyValuePair<Transform, bool> closestSnapPoint = _snapPoints.ElementAt(0); float minimalDistance = Mathf.Infinity; foreach (KeyValuePair<Transform, bool> snapPoint in _snapPoints) { if (_snapPoints[snapPoint.Key] == false) { float distance = (fromTransform.position - snapPoint.Key.position).magnitude; if (distance < minimalDistance) { minimalDistance = distance; closestSnapPoint = snapPoint; } } } _snapPoints[closestSnapPoint.Key] = true; return closestSnapPoint.Key; }