using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class ArtificialGravity : MonoBehaviour { [Header("References")] [SerializeField] private RotationController Ship; [Header("Settings")] [SerializeField] private float m_gravityMultiplier = 1; [SerializeField] private float m_correlusMultiplier = 1; private Rigidbody rb; private void OnEnable() { rb = GetComponent(); rb.useGravity = false; if (Ship == null) Ship = FindObjectOfType(); } private void FixedUpdate() { bool isGrounded = IsGrounded(); ApplyGravity(); if (!isGrounded) ApplyCorrelus(); } private void ApplyGravity() { if (Ship.RotationPeriod <= 0) return; Vector3 direction = Ship.GetGravityAtPoint(transform.position); Debug.DrawRay(transform.position, direction,Color.red); //Debug.Log($"Direction: {direction.magnitude}"); rb.AddForce(direction * m_gravityMultiplier, ForceMode.Acceleration); } private void ApplyCorrelus() { Vector3 direction = Ship.getPerpendicularDirection(transform.position); rb.AddForce(Ship.GetCoriolisAtPoint(transform.position) * m_correlusMultiplier, ForceMode.Acceleration); } private bool IsGrounded() { Ray ray = new Ray(transform.position, Ship.getDownDirection(transform.position)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 0.25f)) { return true; } return false; } }