I’m developing a project at the university where study, I’m having a problem running my code.
Basically, I’m doing a gravity simulator, where each star is affected by the movement of each other. I made a code where it runs, however, when I run the project the unity breaks down, and I have to close the file editor.
Here the code, and sorry for my bad English.
using UnityEngine;
using System.Collections;
public class movimento : MonoBehaviour
{
//variaveis para encontrar a instancia das demais estrelas
private GameObject outraEstrela;
private int contprox = 0;
//variaveis auxiliares para realização de calculos
private float deltaX, deltaY, distancia, aceleracaoX, aceleracaoY;
//propriedades da estrela
private float velocidadeY, velocidadeX, massa, raio;
// Use this for initialization
void Start()
{
velocidadeY = 0;
velocidadeX = 0;
}
// Update is called once per frame
void Update () {
outraEstrela = GameObject.Find("estrela"+contprox);
while(outraEstrela!=null){
if(this.GetInstanceID() == outraEstrela.GetInstanceID()){
contprox++;
}else {
contprox++;
deltaX = outraEstrela.transform.position.x - this.transform.position.x;
deltaY = outraEstrela.transform.position.y - this.transform.position.y;
distancia = Mathf.Sqrt(Mathf.Pow(deltaX, 2f)+Mathf.Pow(deltaY, 2f));
aceleracaoX=(deltaX)/Mathf.Pow(distancia, 3f);
aceleracaoY=(deltaY)/Mathf.Pow(distancia, 3f);
}
}
velocidadeX += aceleracaoX*Time.deltaTime;
velocidadeY += aceleracaoY*Time.deltaTime;
this.transform.Translate(new Vector3(velocidadeX, velocidadeY, 0)*Time.deltaTime,Space.Self);
contprox=0;
}
}