Space Gravity

I have been working on this script for some time…

#pragma strict

var g : GameObject[];
var x = 0.0;
var y = 0.0;
var z = 0.0;
var p = 0;

var xr = 0.0;
var yr = 0.0;
var zr = 0.0;

//Newtons
var fx = 0.0;
var fy = 0.0;
var fz = 0.0;

//6.674×10−11 N m2 kg−2
var G = 1.0;

//Killograms
var m1 = 1000.0;
var m2 = 1500.0;
var r = 10.0 ;

function Start () {
	G = m1 * 0.00000000570345;
}

function Update () {
	g = GameObject.FindGameObjectsWithTag ("Planet");
	
	Calc();
	xr = (transform.position.x - x); 
	fx = (G * (m1 * m1)) * ((m1 * m2) / (xr * xr));
	yr = (transform.position.y - y); 
	fy = (G * (m1 * m1)) * ((m1 * m2) / (yr * yr));
	zr = (transform.position.z - z); 
	fz = (G * (m1 * m1)) * ((m1 * m2) / (zr * zr));
}

function FixedUpdate () {
    rigidbody.AddRelativeForce (Vector3.right * fx);
    rigidbody.AddRelativeForce (Vector3.up * fy);
    rigidbody.AddRelativeForce (Vector3.forward * fz);
}

function Calc () {
	if (p > g.Length - 1){
		p = 0;
	}
	x = (x + (g[p].transform.position.x)) / g.length;
	y = (y + (g[p].transform.position.y)) / g.length;
	z = (z + (g[p].transform.position.z)) / g.length;
	p++;
}

When I run this I have some issues with running into infinity. Can someone fix it or give me a better script.

Not sure I understand your problem… I assume this is trying to calculate gravitational pull for the planets?

On a side note you should move

g = GameObject.FindGameObjectsWithTag (“Planet”);

to the Start function (performance improvement)

I am trying to calculate the gravitational pull, I’ll also move the g thing. thx

You’ve told us what you think it should do. You haven’t told us what the problem is.

Well I have gotten all the math to workout now but the script…

#pragma strict

var g : GameObject[];
var p = 0;

//Newtons
var fx = 0.0;
var fy = 0.0;
var fz = 0.0;

//6.674×10−11 N m2 kg−2
var G = 1.0;

//kg
var mass = 1000;
var mas : int;

var xf : float;
var yf : float;
var zf : float;

var x : float;
var y : float;
var z : float;

function Start () {
	G = mass * 0.00000000570345;
	g = GameObject.FindGameObjectsWithTag ("Planet");
}

function FixedUpdate () {
    rigidbody.AddRelativeForce (Vector3.right * fx);
    rigidbody.AddRelativeForce (Vector3.up * fy);
    rigidbody.AddRelativeForce (Vector3.forward * fz);
}

function Update () {
	if (g[p] == this.gameObject) {
		if (p < g.Length - 1){
			p ++;
		}else{
			p = 0;
		}
	}else{
		
		x = (transform.position.x - g[p].transform.position.x);
		y = (transform.position.y - g[p].transform.position.y);
		z = (transform.position.z - g[p].transform.position.z);
		
		xf = (G * ((mass * mass) * (mass * mas)) / (x * x));
		yf = (G * ((mass * mass) * (mass * mas)) / (y * y));
		zf = (G * ((mass * mass) * (mass * mas)) / (z * z));
		
		if (x > 0) {
			fx = fx + xf;
		}else{
			fx = fx - xf;
		}
		
		if (y > 0) {
			fy = fy + yf;
		}else{
			fy = fy - yf;
		}
		
		if (z > 0) {
			fz = fz + zf;
		}else{
			fz = fz - zf;
		}
		
		if (p < g.Length - 1){
			p ++;
		}else{
			p = 0;
		}
	}
}

keeps giving me this error…

OverflowException: Number overflow.
CelestialGravity.Update () (at Assets/Physics/CelestialGravity.js:50)

so this is the only issue that remains with this script.

Your calculations are exceeding the maximum number of decimals Unity’s float values can have. Try using System.Double for your variables instead of float.

Could you give me a piece of code that uses this as an example?

I belive all you need to do is declair the non working float as follows

var {whatever} : system.double;

That should declair it as a system double, and if Diviner is correct, solve the problem

var G : System.Double = 1.0;

Note the casing.

thx, it works wonderfully now.