private void ApplyMagneticForce(MovingChargedItem mci) { Vector3 newForce = Vector3.zero; foreach (ChargedItem ci in chargedItems) { if (mci == ci) { continue; } float distance = Vector3.Distance(mci.transform.position, ci.gameObject.transform.position); float force = 1000 * mci.charge * ci.charge / Mathf.Pow(distance, 2); Vector3 direction = mci.transform.position - ci.transform.position; direction.Normalize(); newForce += force * direction * cycleInterval; if (float.IsNaN(newForce.x)) { newForce = Vector3.zero; } mci.rb.AddForce(newForce); } }
public IEnumerator Cycle(MovingChargedItem mci) { while (true) { ApplyMagneticForce(mci); yield return(new WaitForSeconds(cycleInterval)); } }