Advertisement
Guest User

Physics_orbit.cs

a guest
Nov 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Physics_orbit : MonoBehaviour
  6. {
  7. public float Nudge = 100;
  8. public Vector3 center;
  9. public int mass;
  10.  
  11.  
  12. //G = 6.67 * 10^-11 N.m².kg^-2
  13. private double G = 6.674f * (10 ^ 11);
  14.  
  15. Rigidbody rb;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. mass = 100;
  20. center = new Vector3(0, 0, 0);
  21. rb = GetComponent<Rigidbody>();
  22. rb.AddForce(transform.right * Nudge * 10);
  23. }
  24.  
  25. void FixedUpdate()
  26. {
  27. Vector3 location = rb.position;
  28. Vector3 velocity = rb.velocity;
  29. Vector3 dist = center - location;
  30. float r = dist.magnitude;
  31. dist /= r;
  32.  
  33. float parentMass = mass;
  34. float force = ((float)G * mass * parentMass) / (r * r);
  35.  
  36. rb.velocity += dist * force;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement