Hello everybody!
I am currently trying to do a visualisation of flightpaths in cesium for unity for a school project. I have successfully installed unity and integrated cesium. Then i managed to create the waypoints of the flight path that have been given to me by my teacher. At the moment i have four flight paths in my project, each consisting of several hundreds of waypoints which are in the coordinate format latitude, longitude and height. So far so good. Now i downloaded the 3D-Modell of a Plane and implemented it in the Game. It is supposed to move from waypoint to waypoint (which it does already) and the tip of the plane is supposed to point to the next waypoint. And here is the problem. I seemingly don‘t understand how to properly rotate objects which are positioned through a globe anchor. The plane is clipping through different angles wildly and i dont know how to do it properly. The rotation in cesium is defined through a East-Up-North-System but i just can not get i right. Has somebody else had this Problem and can help me out here?
Hi @Meliodas255, welcome to the community!
Can you share with us what you have so far? It will probably be easier to help you fix what you have than to start from scratch.
This is the script i am currently using for placing the plane at the start of the program. The part where it is moving along the waypoints which are stored in CoordinateData is already working fine. But the way i calculated the Rotation did not work, no matter what i tried. Thank you for the quick reply! If i get how to to the rotation here i surely will understand how to apply the logic to my waypoint arrows as well :D.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using CesiumForUnity;
public class FlyingTaxiController : MonoBehaviour
{
public string jsonFilePath;
public Transform globeTransform;
public GameObject flyingTaxiPrefab;
public float flightSpeed = 10f;
private CoordinateData[] coordinateData;
private int currentWaypointIndex = 0;
private GameObject flyingTaxi;
private CesiumGlobeAnchor globeAnchor;
void Start()
{
string jsonText = System.IO.File.ReadAllText(jsonFilePath);
coordinateData = JsonConvert.DeserializeObject<CoordinateData[]>(jsonText);
flyingTaxi = Instantiate(flyingTaxiPrefab);
flyingTaxi.transform.SetParent(globeTransform, false);
globeAnchor = flyingTaxi.AddComponent<CesiumGlobeAnchor>();
SetFlyingTaxiPosition(coordinateData[0].latitude, coordinateData[0].longitude, coordinateData[0].altitude);
}
void Update()
{
if (currentWaypointIndex < coordinateData.Length - 1)
{
SetFlyingTaxiPosition(coordinateData[currentWaypointIndex].latitude,
coordinateData[currentWaypointIndex].longitude,
coordinateData[currentWaypointIndex].altitude);
RotateTowardsNextWaypoint(
coordinateData[currentWaypointIndex].latitude,
coordinateData[currentWaypointIndex].longitude,
coordinateData[currentWaypointIndex].altitude,
coordinateData[currentWaypointIndex + 1].latitude,
coordinateData[currentWaypointIndex + 1].longitude,
coordinateData[currentWaypointIndex + 1].altitude
);
if (Vector3.Distance(flyingTaxi.transform.position, globeAnchor.transform.position) < 0.0021f)
{
currentWaypointIndex++;
}
}
else
{
currentWaypointIndex = 0;
}
}
void RotateTowardsNextWaypoint(float currentLatitude, float currentLongitude, float currentAltitude,
float nextLatitude, float nextLongitude, float nextAltitude)
{
Vector3 currentPosition = new Vector3(currentLongitude, currentLatitude, currentAltitude);
Vector3 nextPosition = new Vector3(nextLongitude, nextLatitude, nextAltitude);
Vector3 direction = (nextPosition - currentPosition).normalized;
Vector3 east = Vector3.Cross(Vector3.up, up).normalized;
Vector3 up = globeAnchor.transform.up;
Vector3 north = Vector3.Cross(up, east).normalized;
Vector3 eastNorthUp = new Vector3(east.x, north.y, up.z);
Quaternion targetRotation = Quaternion.LookRotation(direction, eastNorthUp);
globeAnchor.transform.rotation = targetRotation;
}
void SetFlyingTaxiPosition(float latitude, float longitude, float altitude)
{
globeAnchor.latitude = latitude;
globeAnchor.longitude = longitude;
globeAnchor.height = altitude;
}
[System.Serializable]
public class CoordinateData
{
public float longitude;
public float latitude;
public float altitude;
}
}