using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class ArtificialGravity : MonoBehaviour { [SerializeField] private RotationController Ship; [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() { ApplyGravity(); } private void ApplyGravity() { if (Ship.RotationPeriod <= 0) return; Vector3 direction = Ship.getDownDirection(transform.position, false); float force = direction.magnitude * Mathf.Pow(2 * Mathf.PI / Ship.RotationPeriod, 2); Debug.DrawRay(transform.position, transform.position + direction); rb.AddForce(direction.normalized * force * Time.fixedDeltaTime, ForceMode.Acceleration); if (!IsGrounded()) { direction = Ship.getPerpendicularDirection(transform.position); rb.AddForce(direction * Ship.CorrelusStrength * m_correlusMultiplier * Time.fixedDeltaTime, ForceMode.Acceleration); Debug.DrawRay(transform.position, direction, Color.blue); } } 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; } }