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.
 
 
 

80 lines
1.7 KiB

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<Rigidbody>();
rb.useGravity = false;
if (Ship == null)
Ship = FindObjectOfType<RotationController>();
}
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;
}
}