using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
public class ArtificialGravity : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private RotationController Ship;
|
|
|
|
private Rigidbody rb;
|
|
|
|
private void OnEnable()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
rb.useGravity = false;
|
|
|
|
if (Ship == null)
|
|
Ship = FindObjectOfType<RotationController>();
|
|
}
|
|
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
ApplyGravity();
|
|
}
|
|
|
|
private void ApplyGravity()
|
|
{
|
|
if (Ship.RotationPeriod <= 0)
|
|
return;
|
|
|
|
Vector3 direction = (transform.position - Ship.Position);
|
|
float force = direction.magnitude * Mathf.Pow(2 * Mathf.PI / Ship.RotationPeriod, 2);
|
|
|
|
rb.AddForce(direction.normalized * force * Time.fixedDeltaTime, ForceMode.Acceleration);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|