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

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class Gravitypull : MonoBehaviour {
  5. //public GameObject gravityWell;
  6. //public float wellMass;
  7. public int gravConstPow;
  8. public
  9. GameObject[] planets;
  10. //public double gravConstant = 6.673f * Mathf.Pow(10,11);
  11. // Use this for initialization
  12. void Start () {
  13. planets = GameObject.FindGameObjectsWithTag ("ground");
  14. }
  15. // Update is called once per frame
  16. void Update () {
  17. }
  18. void FixedUpdate() {
  19. foreach (GameObject planet in planets) {
  20. rigidbody2D.AddForce( calcGrav (planet));
  21. }
  22. }
  23. private Vector2 calcGrav(GameObject planet){
  24. double gravConstant = 6.673f * Mathf.Pow(10,gravConstPow);
  25. float gravityForce;
  26. Vector3 distance = planet.transform.position - transform.position;
  27. float r12 = distance.magnitude;
  28. distance /= r12;
  29. //Debug.Log ("distance: " + distance);
  30. gravityForce = (rigidbody2D.mass * planet.rigidbody2D.mass * (float)gravConstant) / (r12 * r12);
  31. //Debug.Log ("player mass: " + rigidbody.mass);
  32. //Debug.Log ("well mass: " + wellMass);
  33. //Debug.Log ("grav Const: " + (float)gravConstant);
  34. //Debug.Log ("r12: " + r12);
  35. //Debug.Log ("gravityForce: " + gravityForce);
  36. Vector2 distance2D = new Vector2 (distance.x, distance.y);
  37. Debug.Log ("force added: " + (distance2D * gravityForce));
  38. return (distance2D * gravityForce);
  39. }
  40. }