void GetClosestObjects() { RingActor[] Rings = FindObjectsOfType <RingActor>(); RingActor tMin = null; float Distance = Mathf.Pow(AttractionRadius, 2); foreach (RingActor r in Rings) { if (!r.gameObject.activeSelf || r.IsLostRing) { return; } float currentDistance = (transform.position - r.transform.position).sqrMagnitude; if (currentDistance <= Distance) { tMin = r; if (!closeRings.Contains(tMin)) { closeRings.Add(tMin); } } } }
private void OnTriggerEnter(Collider other) { if (stageProgress == null) { Debug.LogError("Stage Progress object not found. Please add a Stage Progress object."); return; } #region Items if (other.GetComponent <RingActor>()) { RingActor ring = other.GetComponent <RingActor>(); pool.SpawnFromPool("RingParticles", ring.transform.position, Quaternion.identity); anim.PlayRingSound(false); ring.gameObject.SetActive(false); PlayerHealth.RingCount++; _health.RingsToLife--; } if (other.GetComponent <ItemCapsuleActor>()) { ItemCapsuleActor cap = other.GetComponent <ItemCapsuleActor>(); if (cap.IsActive) { anim.PlayCapsuleSound(); //Add items switch (cap.capItem) { case ItemCapsuleActor.Item.TenRings: StartCoroutine(AddRings(10, 0.5f)); break; case ItemCapsuleActor.Item.FiveRings: StartCoroutine(AddRings(5, 0.5f)); break; case ItemCapsuleActor.Item.Shield: StartCoroutine(AddShield(0, 0.5f)); break; case ItemCapsuleActor.Item.Invincible: StartCoroutine(AddShield(1, 0.5f)); break; case ItemCapsuleActor.Item.OneUp: StartCoroutine(AddLife(0.5f)); break; } cap.IsActive = false; if (PlayerActions.currentState is ActionJump) { if (Player.rigidBody.velocity.y < 0) { Player.rigidBody.velocity = Vector3.Reflect(Player.rigidBody.velocity, cap.transform.up); } } else if (PlayerActions.currentState is ActionHoming) { Player.rigidBody.velocity = Vector3.ProjectOnPlane(Player.rigidBody.velocity, Player.GroundNormal) * actions.homingState.HomingReleaseVelocity; Player.rigidBody.velocity += transform.up * actions.homingState.HomingBouncePower; actions.ChangeState(1); } actions.UpdateTargets(); } } #endregion #region Boosters if (other.GetComponent <SpringActor>()) { SpringActor spring = other.GetComponent <SpringActor>(); transform.position = spring.transform.position + spring.transform.up * spring.PositionOffset; actions.ChangeState(0); Player.TransformNormal = spring.transform.up; Player.rigidBody.velocity = spring.transform.up * spring.SpringForce; actions.defaultState.DidDash = false; if (Player.Crouching) { Player.Crouching = false; } Player.StartCoroutine(Player.LockInput(spring.LockDuration)); } if (other.GetComponent <DashPanelActor>()) { DashPanelActor dashPanel = other.GetComponent <DashPanelActor>(); if (dashPanel.SnapToPosition) { Player.rigidBody.position = dashPanel.transform.position + dashPanel.transform.up * 0.5f; } if (dashPanel.IsAdditive) { Player.rigidBody.velocity = dashPanel.transform.forward * (Player.rigidBody.velocity.magnitude + dashPanel.Force); } else { Player.rigidBody.velocity = dashPanel.transform.forward * dashPanel.Force; } Player.StartCoroutine(Player.LockInput(dashPanel.LockDuration)); } #endregion #region Stage Objects if (other.gameObject.CompareTag("PulleyHandle")) { currentPulley = other.gameObject.GetComponentInParent <PulleyActor>(); actions.ChangeState(5); currentPulley.RetractPulley(); } #endregion #region Hazards and Enemies if (other.gameObject.CompareTag("Enemy")) { if (PlayerActions.currentState is ActionHoming || PlayerActions.currentState is ActionJump) { pool.SpawnFromPool("HitExplosion", other.transform.position, Quaternion.identity); other.gameObject.SetActive(false); if (PlayerActions.currentState is ActionHoming) { Player.rigidBody.velocity = Vector3.ProjectOnPlane(Player.rigidBody.velocity, Player.GroundNormal) * actions.homingState.HomingReleaseVelocity; Player.rigidBody.velocity += transform.up * actions.homingState.HomingBouncePower; actions.ChangeState(1); } else if (PlayerActions.currentState is ActionJump) { if (Player.rigidBody.velocity.y < 0) { Player.rigidBody.velocity = Vector3.Reflect(Player.rigidBody.velocity, other.transform.up); } } anim.PlayEnemyHit(); score.AddScore(EnemyScore); } else if (PlayerActions.currentState is ActionDefault) { ///If we are grounded and rolling or have an invincible shield, destroy the enemy. Otherwise, take damage. if (Player.rigidBody.velocity.magnitude >= actions.defaultState.RollingStartSpeed && Player.Crouching && Player.Grounded || _health.HasShield && _health.IsInvincible) { pool.SpawnFromPool("HitExplosion", other.transform.position, Quaternion.identity); other.gameObject.SetActive(false); anim.PlayEnemyHit(); score.AddScore(EnemyScore); } else { if (!_health.IsInvincible) { _health.TakeDamage(); Vector3 Direction = transform.position - other.gameObject.transform.position; Player.Grounded = false; transform.forward = -Vector3.ProjectOnPlane(Direction, Vector3.up).normalized; actions.ChangeState(3); } } } } if (other.gameObject.CompareTag("Hazard")) { if (!_health.IsInvincible) { _health.TakeDamage(); Vector3 Direction = transform.position - other.gameObject.transform.position; Player.Grounded = false; transform.forward = -Vector3.ProjectOnPlane(Direction, Vector3.up).normalized; actions.ChangeState(3); } } if (other.gameObject.CompareTag("KillPlane")) { camera.UpdateCameraOrientation = false; camera.camState = PlayerCamera.CameraState.Fixed; camera.TargetPosition = camera.transform.position; anim.PlayDeathVoice(); _health.RemoveShield(); if (_health.LifeCount > 0) { StageProgress.instance.BeginRespawn(); } else { StageProgress.instance.BeginGameOver(); } } #endregion #region Stage Progress if (other.GetComponent <CheckpointActor>()) { CheckpointActor check = other.GetComponent <CheckpointActor>(); if (!check.IsActive) { check.IsActive = true; stageProgress.SpawnPosition = check.transform.position; stageProgress.SpawnRotation = check.transform.rotation; check.GetComponent <AudioSource>().Play(); } } #endregion }