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.

63 lines
1.5 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class gravityWell : MonoBehaviour {
  5. public float wellMass;
  6. public int gravConstPow;
  7. public GameObject[] targets;
  8. //public float minDistance;
  9. //public float bufferZone;
  10. //public float bufferDampner;
  11. public float clampVelo;
  12. public float maxVelo;
  13. public float moveSpeed;
  14. // Use this for initialization
  15. void Start () {
  16. }
  17. // Update is called once per frame
  18. void FixedUpdate () {
  19. foreach (GameObject target in targets) {
  20. /*target.rigidbody.AddForce( calcGrav (target));
  21. target.rigidbody.velocity = Vector3.ClampMagnitude(target.rigidbody.velocity, clampVelo);
  22. Debug.Log(target.rigidbody.velocity.magnitude);*/
  23. float step = moveSpeed * Time.deltaTime;
  24. target.transform.position = Vector3.MoveTowards(target.transform.position,transform.position, step);
  25. }
  26. }
  27. private Vector3 calcGrav(GameObject target){
  28. double GRAVCONSTANT = 6.673f * Mathf.Pow (10, gravConstPow);
  29. Vector3 distance = this.transform.position - target.transform.position;
  30. float mag = distance.magnitude;
  31. distance /= mag;
  32. float gravityForce = (target.GetComponent<Rigidbody>().mass * wellMass * (float)GRAVCONSTANT) / (mag * mag);
  33. //if (minDistance > mag)
  34. // return (Vector3.zero);
  35. //else if (bufferZone > mag)
  36. // return (-(distance * gravityForce)/bufferDampner);
  37. //else
  38. Debug.Log ((distance * gravityForce).magnitude);
  39. if ((distance * gravityForce).magnitude > maxVelo)
  40. return (Vector3.zero);
  41. else
  42. return (distance * gravityForce);
  43. }
  44. }