Assignment for RMIT Mixed Reality in 2020
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.

43 lines
891 B

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class ArtificialGravity : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private RotationController Ship;
  9. private Rigidbody rb;
  10. private void OnEnable()
  11. {
  12. rb = GetComponent<Rigidbody>();
  13. rb.useGravity = false;
  14. if (Ship == null)
  15. Ship = FindObjectOfType<RotationController>();
  16. }
  17. private void FixedUpdate()
  18. {
  19. ApplyGravity();
  20. }
  21. private void ApplyGravity()
  22. {
  23. if (Ship.RotationPeriod <= 0)
  24. return;
  25. Vector3 direction = (transform.position - Ship.Position);
  26. float force = direction.magnitude * Mathf.Pow(2 * Mathf.PI / Ship.RotationPeriod, 2);
  27. rb.AddForce(direction.normalized * force * Time.fixedDeltaTime, ForceMode.Acceleration);
  28. }
  29. }