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.
 
 
 

43 lines
891 B

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);
}
}