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.

82 lines
1.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class ArtificialGravity : MonoBehaviour
  6. {
  7. [Header("References")]
  8. [SerializeField]
  9. private RotationController Ship;
  10. [Header("Settings")]
  11. [SerializeField]
  12. private float m_gravityMultiplier = 1;
  13. [SerializeField]
  14. private float m_correlusMultiplier = 1;
  15. private Rigidbody rb;
  16. private void OnEnable()
  17. {
  18. rb = GetComponent<Rigidbody>();
  19. rb.useGravity = false;
  20. if (Ship == null)
  21. Ship = FindObjectOfType<RotationController>();
  22. }
  23. private void FixedUpdate()
  24. {
  25. bool isGrounded = IsGrounded();
  26. ApplyGravity();
  27. if (!isGrounded)
  28. ApplyCorrelus();
  29. }
  30. private void ApplyGravity()
  31. {
  32. if (Ship.RotationPeriod <= 0)
  33. return;
  34. Vector3 direction = Ship.GetGravityAtPoint(transform.position);
  35. Debug.DrawRay(transform.position, direction,Color.red);
  36. //Debug.Log($"Direction: {direction.magnitude}");
  37. rb.AddForce(direction * m_gravityMultiplier, ForceMode.Acceleration);
  38. }
  39. private void ApplyCorrelus()
  40. {
  41. if (Ship.RotationPeriod <= 0)
  42. return;
  43. Vector3 direction = Ship.getPerpendicularDirection(transform.position);
  44. rb.AddForce(Ship.GetCoriolisAtPoint(transform.position) * m_correlusMultiplier, ForceMode.Acceleration);
  45. }
  46. private bool IsGrounded()
  47. {
  48. Ray ray = new Ray(transform.position, Ship.getDownDirection(transform.position));
  49. RaycastHit hit;
  50. if (Physics.Raycast(ray, out hit, 0.25f))
  51. {
  52. return true;
  53. }
  54. return false;
  55. }
  56. }