void Attract(Attractor objToAttract) { Rigidbody rbToAttract = objToAttract.rb; Vector3 direction = rb.position - rbToAttract.position; float distance = direction.magnitude; if (distance == 0f) { return; } la = GameObject.Find("La"); if (la != null) { paresCargas = GameObject.Find("ParesCargas"); active_counter_script active_script = paresCargas.GetComponent <active_counter_script> (); eletrons = active_script.num_eletrons; } else { paresCargasAnti = GameObject.Find("ParesCargasAnti"); active_counter_script active_script = paresCargasAnti.GetComponent <active_counter_script>(); eletrons = active_script.num_eletrons; } multiplier = eletrons * CONSTANTE; float forceMagnitude = multiplier * ((rb.mass * rbToAttract.mass) / Mathf.Pow(distance, 2)); Vector3 force = direction.normalized * forceMagnitude; rbToAttract.AddForce(force); }
// Update is called once per frame void Update() { //total=0; la = GameObject.Find("La"); if (la != null) { paresCargas = GameObject.Find("ParesCargas"); active_counter_script active_script = paresCargas.GetComponent <active_counter_script> (); eletrons = active_script.num_eletrons; } else { paresCargasAnti = GameObject.Find("ParesCargasAnti"); active_counter_script active_script = paresCargasAnti.GetComponent <active_counter_script>(); eletrons = active_script.num_eletrons; } eletrons++; //correção do bug do primeiro eletron não ativar no balão foreach (Transform child in transform) { if (eletrons > 0) { if (child.gameObject.activeSelf == false) { child.gameObject.SetActive(true); } eletrons--; } if (eletrons == 0) { if (child.gameObject.activeSelf == true) { child.gameObject.SetActive(false); } } } eletrons--; //correção do bug do primeiro eletron não ativar no balão //Debug.Log(la.activeSelf); }