- Home /
Visualize a trajectory of a drone flight?
Hello,
I am currently working on a real life drone project. As an example of a drone flight I am doing a visualization in Unity. What I want to do is to have a trajectory defined in an array (my guess is using Vector3), read the coordinates from the array and send the drone object to fly a certain path and follow the points from the trajectory array.
I am very new to the Unity environment and would be happy if you could give me some tips on how and where to start.
Thanks!
Answer by Happeloy · May 19, 2018 at 01:09 PM
There are a few ways to do this. Here's one way, using a coroutine. They can be seen as separate threads (although they are not, in most cases they behave like one).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour {
public List<Vector3> positions; //Your positions, set ininspector;
public float speed; //Set in inspector to change speed.
public Transform drone; //The object you want to move
void Start () {
StartCoroutine("MoveAlongPath");
}
private IEnumerator MoveAlongPath(){
int index = 0;
Vector3 currentGoal = positions[index];
drone.transform.position = currentGoal; //Set drone to start position.
while (index < positions.Count)
{
currentGoal = positions[index];
while(drone.transform.position != currentGoal){
drone.transform.position = Vector3.MoveTowards(drone.transform.position, currentGoal, Time.deltaTime * speed);
drone.LookAt(currentGoal); //make drone face towards goal.
yield return null; //Wait for next frame.
}
index++;
}
}
}
And here's a image showing how this would be used in the inspector:
Answer by Slastraf · May 19, 2018 at 11:38 AM
you can probably use a camera cutszene script from the workshop and jsut use the drone model instead of the camera
Answer by unity_KXxxNHSz_BKpQg · May 19, 2018 at 08:42 PM
Thank you so much, that is exactly what I need!
Your answer
![](https://koobas.hobune.stream/wayback/20220612163003im_/https://answers.unity.com/themes/thub/images/avi.jpg)