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.
 
 
 

62 lines
1.8 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoriolisEffect : MonoBehaviour
{
public Vector3 downDir => Vector3.ProjectOnPlane((transform.position - center.transform.position), Vector3.forward);
// Start is called before the first frame update
public GameObject center;
private Rigidbody rb;
private const float MAX_GRAVITY = 9.8f;
public float maxCoriolisForce;
public float coriolisForceMultiplier = 2f;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
}
// Update is called once per frame
void FixedUpdate()
{
if (IsGrounded())
return;
Vector3 direction = downDir.normalized;
Vector3 coriolisVector = -Vector3.Cross(direction, new Vector3(0,0,1)).normalized;
Debug.Log(LerpCoriolisForce());
float force = LerpCoriolisForce();
rb.AddForce(coriolisVector * force * coriolisForceMultiplier * Time.deltaTime, ForceMode.Acceleration);
//rb.AddForce(direction * LerpGravity(), ForceMode.Acceleration);
Debug.DrawRay(transform.position, coriolisVector * force, Color.red);
}
float LerpGravity()
{
return Mathf.Lerp(0, MAX_GRAVITY, Vector3.Distance(gameObject.transform.position, center.transform.position) / 12.75f);
}
float LerpCoriolisForce()
{
Debug.Log(Vector3.Distance(gameObject.transform.position, center.transform.position));
return Mathf.Lerp(maxCoriolisForce, 0, downDir.magnitude / 12.75f);
}
private bool IsGrounded()
{
Ray ray = new Ray(transform.position, downDir.normalized);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 0.25f))
{
return true;
}
return false;
}
}