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.

70 lines
1.6 KiB

  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. [SerializeField]
  10. private float m_correlusMultiplier = 1;
  11. private Rigidbody rb;
  12. private void OnEnable()
  13. {
  14. rb = GetComponent<Rigidbody>();
  15. rb.useGravity = false;
  16. if (Ship == null)
  17. Ship = FindObjectOfType<RotationController>();
  18. }
  19. private void FixedUpdate()
  20. {
  21. ApplyGravity();
  22. }
  23. private void ApplyGravity()
  24. {
  25. if (Ship.RotationPeriod <= 0)
  26. return;
  27. Vector3 direction = Ship.getDownDirection(transform.position, false);
  28. float force = direction.magnitude * Mathf.Pow(2 * Mathf.PI / Ship.RotationPeriod, 2);
  29. Debug.DrawRay(transform.position, transform.position + direction);
  30. rb.AddForce(direction.normalized * force * Time.fixedDeltaTime, ForceMode.Acceleration);
  31. if (!IsGrounded())
  32. {
  33. direction = Ship.getPerpendicularDirection(transform.position);
  34. rb.AddForce(direction * Ship.CorrelusStrength * m_correlusMultiplier * Time.fixedDeltaTime, ForceMode.Acceleration);
  35. Debug.DrawRay(transform.position, direction, Color.blue);
  36. }
  37. }
  38. private bool IsGrounded()
  39. {
  40. Ray ray = new Ray(transform.position, Ship.getDownDirection(transform.position));
  41. RaycastHit hit;
  42. if (Physics.Raycast(ray, out hit, 0.25f)){
  43. return true;
  44. }
  45. return false;
  46. }
  47. }