You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

65 lines
1.4 KiB

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gravitypull : MonoBehaviour {
//public GameObject gravityWell;
//public float wellMass;
public int gravConstPow;
public
GameObject[] planets;
//public double gravConstant = 6.673f * Mathf.Pow(10,11);
// Use this for initialization
void Start () {
planets = GameObject.FindGameObjectsWithTag ("ground");
}
// Update is called once per frame
void Update () {
}
void FixedUpdate() {
foreach (GameObject planet in planets) {
rigidbody2D.AddForce( calcGrav (planet));
}
}
private Vector2 calcGrav(GameObject planet){
double gravConstant = 6.673f * Mathf.Pow(10,gravConstPow);
float gravityForce;
Vector3 distance = planet.transform.position - transform.position;
float r12 = distance.magnitude;
distance /= r12;
//Debug.Log ("distance: " + distance);
gravityForce = (rigidbody2D.mass * planet.rigidbody2D.mass * (float)gravConstant) / (r12 * r12);
//Debug.Log ("player mass: " + rigidbody.mass);
//Debug.Log ("well mass: " + wellMass);
//Debug.Log ("grav Const: " + (float)gravConstant);
//Debug.Log ("r12: " + r12);
//Debug.Log ("gravityForce: " + gravityForce);
Vector2 distance2D = new Vector2 (distance.x, distance.y);
Debug.Log ("force added: " + (distance2D * gravityForce));
return (distance2D * gravityForce);
}
}