public GrappleObject NextGrappleObject(Vector3 position, float minDistance) { float min = minDistance; GrappleObject closestTarget = null; for (int i = 0; i < grappleObjects.Count; i++) { float distance = Vector3.Distance(position, grappleObjects[i].transform.position); if (grappleObjects[i].canGrapple && distance < min) { min = distance; closestTarget = grappleObjects[i]; } } return(closestTarget); }
IEnumerator Swing() { Physics2D.SyncTransforms(); GrappleObject vineGrapplePoint = LevelManager.singleton.NextGrappleObject(transform.position, grappleDistance); if (vineGrapplePoint != null) { canGrapple = false; float distance = Vector3.Distance(vineGrapplePoint.transform.position, transform.position); vineGrapplePointObject = vineGrapplePoint.gameObject; AudioManager.singleton.PlaySoundEffect(vineSFX, 1); if (distance < grappleDistance) { vine.SetPosition(0, transform.position); vine.SetPosition(1, vineGrapplePointObject.transform.position); vine.enabled = true; Vector3 midPoint = (vineGrapplePointObject.transform.position + transform.position) / 2f; vineParticles.transform.position = midPoint; var particleShape = vineGlow.shape; particleShape.scale = new Vector3(distance, particleShape.scale.y, particleShape.scale.z); vineParticles.transform.LookAt(transform.position); Vector3 targetRot = vineParticles.transform.eulerAngles; targetRot.z = (vineGrapplePointObject.transform.position.x - transform.position.x >= 0) ? targetRot.x : -targetRot.x; targetRot.x = targetRot.y = 0; vineParticles.transform.eulerAngles = targetRot; vineParticles.SetActive(true); controller.freeze = true; Vector3 hit3d = vineGrapplePointObject.transform.position; Vector3 dir = hit3d - transform.position; if (dir.normalized.x < 0) { sprite.flipX = true; } else { sprite.flipX = false; } yield return(new WaitForSeconds(.1f)); } controller.freeze = false; while (vineGrapplePointObject != null) { controller.collisions.below = false; distance = Vector3.Distance(vineGrapplePointObject.transform.position, transform.position); if (distance < 1f) { canGrapple = true; //velocity = Vector3.zero; vineGrapplePoint.canGrapple = false; vineGrapplePoint.timer = .75f; Debug.Log("Break grapple"); vineGrapplePointObject = null; vine.enabled = false; vineParticles.SetActive(false); } else if (distance < grappleDistance) { vine.SetPosition(0, transform.position); vine.SetPosition(1, vineGrapplePointObject.transform.position); vine.enabled = true; Vector3 hit3d = vineGrapplePointObject.transform.position; Vector3 dir = hit3d - transform.position; velocity = dir.normalized * 15f; } if (vineGrapplePointObject != null && ((controller.collisions.above || controller.collisions.right || controller.collisions.left) || distance > grappleDistance || (controller.collisions.below && distance < 3))) { vineGrapplePointObject = null; vine.enabled = false; canGrapple = true; vineGrapplePoint.canGrapple = false; vineGrapplePoint.timer = .75f; vineParticles.SetActive(false); //velocity = Vector3.zero; } yield return(null); } } else { vineTimer = .3f; AudioManager.singleton.PlaySoundEffect(vineSFX, 1); while (vineTimer > 0) { canGrapple = false; vine.enabled = true; vine.SetPosition(0, transform.position); Vector3 targetPos; if (sprite.flipX) { targetPos = transform.position - Vector3.right * 3; } else { targetPos = transform.position + Vector3.right * 3; } vine.SetPosition(1, targetPos); float distance = Vector3.Distance(targetPos, transform.position); Vector3 midPoint = (targetPos + transform.position) / 2f; vineParticles.transform.position = midPoint; var particleShape = vineGlow.shape; particleShape.scale = new Vector3(distance, particleShape.scale.y, particleShape.scale.z); vineParticles.transform.LookAt(transform.position); Vector3 targetRot = vineParticles.transform.eulerAngles; targetRot.z = (targetPos.x - transform.position.x >= 0) ? targetRot.x : -targetRot.x; targetRot.x = targetRot.y = 0; vineParticles.transform.eulerAngles = targetRot; vineParticles.SetActive(true); vineTimer -= Time.deltaTime; yield return(null); } if (vine.enabled) { canGrapple = true; vineParticles.SetActive(false); vine.enabled = false; } } }