arskrald/AR-3/Assets/GPSScript.cs

61 lines
1.8 KiB
C#
Raw Permalink Normal View History

2019-03-04 17:07:53 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GPSScript : MonoBehaviour
{
private Transform earth;
private Transform nose;
private Transform shuttle;
private Transform positionTest;
private Matrix4x4 earthLocalToWorldInverse;
private string printString;
private Vector3 posOnEarth = new Vector3();
// Start is called before the first frame update
void Start()
{
earth = GameObject.Find("EarthTarget").transform;
nose = GameObject.Find("NoseQuad").transform;
shuttle = GameObject.Find("ShuttleTarget").transform;
positionTest = GameObject.Find("PositionTest").transform;
}
// Update is called once per frame
void Update()
{
printString = "";
var shuttleLocalToWorld = shuttle.localToWorldMatrix;
var earthWorldToLocal = Matrix4x4.Inverse(Matrix4x4.TRS(earth.position, earth.rotation, Vector3.one));
var transMatrix = earthWorldToLocal * shuttleLocalToWorld;
posOnEarth = transMatrix.MultiplyPoint3x4(nose.localPosition);
printString += posOnEarth.ToString("F4") + "\n";
compareToNose(posOnEarth);
}
void compareToNose(Vector3 nosePosition) {
var radius = earth.lossyScale.magnitude / 4;
var d = new Vector2(nosePosition.x, nosePosition.z).magnitude;
if (d <= radius && nosePosition.y <= 4) {
if (nosePosition.z >= 0) {
printString += "North";
} else {
printString += "South";
}
} else {
printString += "Outside";
}
}
void OnGUI()
{
GUI.color = Color.white;
GUI.Box(new Rect(10, 30, 200, 40), printString);
}
}