Copy Aetheria Code
Added a bunch of code from the old Aetheria project
This commit is contained in:
parent
ab392cd3d6
commit
c58ba0b1da
1704 changed files with 175081 additions and 313 deletions
76
Assets/Scripts/Orbit.cs
Normal file
76
Assets/Scripts/Orbit.cs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
// TODO: USE THIS EVERYWHERE
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class Orbit : MonoBehaviour
|
||||
{
|
||||
public Transform Target;
|
||||
public float Period;
|
||||
public bool ApplyOrbitRotation = false;
|
||||
public float Phase;
|
||||
public float Distance;
|
||||
public bool CalculatePosition;
|
||||
|
||||
//private float _period;
|
||||
|
||||
// Use this for initialization
|
||||
void Start ()
|
||||
{
|
||||
if (Target == null)
|
||||
{
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var diff = Target.position - transform.position;
|
||||
|
||||
Distance = !CalculatePosition ? Distance : diff.magnitude;
|
||||
|
||||
Phase = !CalculatePosition ? Phase : Mathf.Atan2(-diff.x, -diff.z);
|
||||
|
||||
if (Mathf.Approximately(Period,0))
|
||||
{
|
||||
Period = diff.sqrMagnitude / 100;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update ()
|
||||
{
|
||||
if (Target == null)
|
||||
{
|
||||
enabled = false;
|
||||
Debug.Log("Orbit script updating without target. HOW DOES THIS HAPPEN?!");
|
||||
return;
|
||||
}
|
||||
|
||||
var time = Time.time;//(GameManager.Instance!=null ? GameManager.Instance.ServerTime : Time.time);
|
||||
|
||||
// var p = _phase + time / (Period / (Mathf.PI * 2));
|
||||
|
||||
var pos = Evaluate(Phase + time / Period);
|
||||
|
||||
transform.position = new Vector3(Target.position.x + Distance * pos.x,
|
||||
transform.position.y,
|
||||
Target.position.z + Distance * pos.y);
|
||||
var rot = transform.localRotation.eulerAngles;
|
||||
|
||||
if(ApplyOrbitRotation)
|
||||
transform.rotation = Quaternion.Euler(rot.x,(rot.y*Mathf.Deg2Rad-Time.fixedDeltaTime * Period) * Mathf.Rad2Deg,rot.z);
|
||||
}
|
||||
|
||||
void Parent(Transform parent)
|
||||
{
|
||||
Target = parent;
|
||||
}
|
||||
|
||||
public static float2 Evaluate(float phase)
|
||||
{
|
||||
phase *= (Mathf.PI * 2);
|
||||
return new float2(sin(phase), cos(phase));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue