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

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CoriolisEffect : MonoBehaviour
  5. {
  6. public Vector3 downDir => Vector3.ProjectOnPlane((transform.position - center.transform.position), Vector3.forward);
  7. // Start is called before the first frame update
  8. public GameObject center;
  9. private Rigidbody rb;
  10. private const float MAX_GRAVITY = 9.8f;
  11. public float maxCoriolisForce;
  12. public float coriolisForceMultiplier = 2f;
  13. void Start()
  14. {
  15. rb = GetComponent<Rigidbody>();
  16. rb.useGravity = false;
  17. }
  18. // Update is called once per frame
  19. void FixedUpdate()
  20. {
  21. if (IsGrounded())
  22. return;
  23. Vector3 direction = downDir.normalized;
  24. Vector3 coriolisVector = -Vector3.Cross(direction, new Vector3(0,0,1)).normalized;
  25. Debug.Log(LerpCoriolisForce());
  26. float force = LerpCoriolisForce();
  27. rb.AddForce(coriolisVector * force * coriolisForceMultiplier * Time.deltaTime, ForceMode.Acceleration);
  28. //rb.AddForce(direction * LerpGravity(), ForceMode.Acceleration);
  29. Debug.DrawRay(transform.position, coriolisVector * force, Color.red);
  30. }
  31. float LerpGravity()
  32. {
  33. return Mathf.Lerp(0, MAX_GRAVITY, Vector3.Distance(gameObject.transform.position, center.transform.position) / 12.75f);
  34. }
  35. float LerpCoriolisForce()
  36. {
  37. Debug.Log(Vector3.Distance(gameObject.transform.position, center.transform.position));
  38. return Mathf.Lerp(maxCoriolisForce, 0, downDir.magnitude / 12.75f);
  39. }
  40. private bool IsGrounded()
  41. {
  42. Ray ray = new Ray(transform.position, downDir.normalized);
  43. RaycastHit hit;
  44. if (Physics.Raycast(ray, out hit, 0.25f))
  45. {
  46. return true;
  47. }
  48. return false;
  49. }
  50. }