using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class WayPoints : MonoBehaviour
{
public static Transform[] wayPoints;
public string _dataPath;
private List<float> dataList = new List<float>();
private StreamReader reader;
private void Awake()
{
GetTXTList();
//int count = transform.childCount;
//wayPoints = new Transform[count];
//for (int i = 0; i < count; i++)
//{
// wayPoints[i] = transform.GetChild(i);
//}
}
public List<float> GetTXTList()
{
//逐行读取返回的为数组数据
string path = Path.Combine(Application.streamingAssetsPath + "/Path.txt", "");
print(path);
string[] strs = File.ReadAllLines(Path.Combine(Application.streamingAssetsPath + "/Path.txt", ""));
wayPoints = new Transform[strs.Length];
for (int i = 0; i < strs.Length; i++)
{
GameObject child = new GameObject();
child.transform.SetParent(transform);
string[] pos = strs[i].Split(',');
child.transform.localPosition = new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
child.transform.localEulerAngles = Vector3.zero;
child.transform.localScale = Vector3.one;
wayPoints[i] = child.transform;
}
return dataList;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneAutomaticFly : MonoBehaviour {
public float speed = 1f;
private Transform[] ways;
public static int index;
float timer = 0.0f;
bool flag = true;
public Transform plane;
public GameObject image, button;
public AudioSource flysound;
//转向参数
private Quaternion raw_rotation; // 保存转身前的角度
private Quaternion lookat_rotation; // 准备面向的角度
private float per_second_rotate = 60.0f; // 转身速度(每秒能转多少度)
float lerp_speed = 0.0f; // 旋转角度越大, lerp变化速度就应该越慢
float lerp_tm = 0.0f; // lerp的动态参数
// Use this for initialization
void Start()
{
ways = WayPoints.wayPoints;
index = 0;
Init_LookAtRotate(plane, ways[0]);
flysound = GetComponent<AudioSource>();
flysound.volume = 0;
}
// Update is called once per frame
void Update()
{
//if (timer >= 2)
//{
if(flag)
{
Time.timeScale = 0;
}
else
{
MoveTo();
}
//}
}
void MoveTo()
{
if (index > ways.Length - 1)
{
timer += Time.deltaTime;
if (timer >= 1.5f)
{
image.SetActive(true);
button.SetActive(true);
}
return;
}
//transform.LookAt(ways[index].position);
if(index <= 0)
{
speed += 5f * Time.deltaTime;
flysound.volume += 0.1f * Time.deltaTime;
}
if (index > 0 && index <=1)
{
speed += 20f * Time.deltaTime;
flysound.volume += 0.1f * Time.deltaTime;
}
if (index >= ways.Length-2)
{
if(speed > 30)
{
speed -= 15f * Time.deltaTime;
}
speed -= 5f * Time.deltaTime;
if(speed <= 30 && index != ways.Length-1)
{
speed = 30;
}
if(speed < 1)
{
speed = 1;
}
if (flysound.volume <= 0.5f && index != ways.Length - 1)
flysound.volume = 0.5f;
else flysound.volume -= 0.1f * Time.deltaTime;
}
// Debug.Log(flysound.volume);
Init_LookAtRotate(plane, ways[index]);
transform.position = Vector3.MoveTowards(this.transform.position, ways[index].transform.position, Time.deltaTime * speed);
//transform.Translate((ways[index].position - transform.position).normalized * Time.deltaTime * speed);
//transform.position = Vector3.Lerp(transform.position, ways[index].position, Time.deltaTime);
//Quaternion rotate = Quaternion.LookRotation(ways[index].transform.position - transform.position);
//transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * speed);
Rotate_Func(plane);
if(index > 2 && index < ways.Length - 3)
{
if(lookat_rotation.y - raw_rotation.y > 0)
{
transform.Rotate(-new Vector3(0, 0, 1) * 40 * Time.deltaTime);
}
if (lookat_rotation.y - raw_rotation.y < 0)
{
transform.Rotate(new Vector3(0, 0, 1) * 40 * Time.deltaTime);
}
}
if (Vector3.Distance(ways[index].position, transform.position) < 0.2f)
{
index++;
if (index == ways.Length)
{
transform.position = ways[index - 1].position;
flysound.volume = 0;
}
}
}
void Init_LookAtRotate(Transform raw_Transform, Transform target_Transform)
{
// 保存转身前的角度
raw_rotation = raw_Transform.rotation;
// 获得并保存目标角度
raw_Transform.LookAt(target_Transform.position);
lookat_rotation = raw_Transform.rotation;
// 恢复到原来的角度
raw_Transform.rotation = raw_rotation;
// 计算出需要的旋转角度
float rotate_angle = Quaternion.Angle(raw_rotation, lookat_rotation);
// 获得lerp速度
lerp_speed = per_second_rotate / rotate_angle;
lerp_tm = 0.0f;
}
void Rotate_Func(Transform raw_Transform)
{
// 旋转完成
if (lerp_tm >= 1)
{
raw_Transform.rotation = lookat_rotation;
return;
}
// 使用 Quaternion.Lerp 进行旋转
lerp_tm += Time.deltaTime * lerp_speed;
raw_Transform.rotation = Quaternion.Slerp(raw_rotation, lookat_rotation, lerp_tm );
// raw_Transform.Rotate(0, 0, Time.deltaTime * 1.0f);
}
public void StartSimulationg()
{
// image.SetActive(false);
Time.timeScale = 1;
flag = false;
}
}