【Unity3D】动态读取txt数据后寻路

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发财猫MoneyCat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值