Created
October 14, 2018 21:45
-
-
Save micrub/7f930d6e79ce79e0a5c42623911a2716 to your computer and use it in GitHub Desktop.
Unity 2017 On ubuntu - Orbital Motion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The sourcce | |
``` | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class OrbitalMoton : MonoBehaviour { | |
public Transform oentity; | |
public Ellipse opath; | |
[Range(0f,1f)] | |
public float oprogress = 0f; | |
float ominimum = 0.1f; | |
public float operiod = 3f; // the time around in seconds | |
public bool oactive = true; | |
// Use this for initialization | |
void Start () { | |
if (operiod < ominimum) | |
{ | |
operiod = ominimum; | |
} | |
if (oentity == null) | |
{ | |
oactive = false; | |
return; | |
} | |
// start orbit animation | |
SetPosition(); | |
StartCoroutine(AnimateOrbit()); | |
} | |
void SetPosition(){ | |
// set orbitng object position | |
Vector2 position = opath.Evaluate (oprogress); | |
oentity.localPosition = new Vector3(position.x, 0, position.y); | |
} | |
IEnumerator AnimateOrbit() { | |
float speed = 1f / operiod; | |
while (oactive) | |
{ | |
oprogress += Time.deltaTime * speed; | |
oprogress %= 1f; | |
SetPosition(); | |
// yield trigger next FRAME processing | |
yield return null; | |
} | |
} | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment