unity按路径移动

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPathMove : MonoBehaviour
{
    public Transform[] wayPointArray;
    [SerializeField] private Transform PathA;//路径点的父物体
    [SerializeField]private Transform playerObj;//移动的物体
    private int index;
    private Vector3 _currentDirection;//当前角色的前进方向
    void Start()
    {
        if(playerObj==null)
        {
            Debug.LogError("请将移动的角 色放入参数栏");
        }
        //print(PathA.childCount);//获取子物体的数量
        //开辟空间
        wayPointArray = new Transform[PathA.childCount];
        for(int i=0;i<wayPointArray.Length;i++)
        {
            wayPointArray[i]=PathA.GetChild(i);//将路径点加入数组中
            //print($"wayPointArrayz{i} ={ wayPointArray[i].position }");//输出世界坐标
        }
       
    }
    


    //在Updata里面最好不要放太多东西
    void Update()
    {
        //Distance需要开方
       /* if (Vector3.Distance(wayPointArray[index].position,playerObj.position)<0.3f)
        {
            //检查是否到达最后一个目标点
            if (index < wayPointArray.Length - 1)
            { index++; }
            else
            {
                index = 0;
            }*/
           /* if (index++ == wayPointArray.Length - 1)
            {
                index = 0;
            }*/
       // }
        //返回距离的平方值
       // Vector3 _currentDirection = wayPointArray[index].position - playerObj.position;

        //开启协程,每隔1秒钟算10次0.11.5
        StartCoroutine(RotationDirectionByTime(1.5f));

        //角色一直前进
        playerObj.Translate(Vector3.forward * 8f * Time.deltaTime);
    }

    IEnumerator RotationDirectionByTime(float time)
    {
        while (true)
        {

            //写算法来改变角色前进的方向
            //计算角色与目标点之间的向量
           Vector3 _currentDirection = wayPointArray[index].position - playerObj.position;
            if (_currentDirection.sqrMagnitude <= 0.2f)
            {
                index += 1;
                if (index == wayPointArray.Length )
                {
                    index = 0;
                }
            }
            Debug.Log("输出数组坐标:" + index);
            //计算旋转角度(没有旋转的过程)
            // playerObj.LookAt(direction);
            //差值运算能够看到他的过程,球形差值
            //出现抖动是因为这里每一帧出来的值都不一样,所以需要用协程控制一下
            playerObj.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_currentDirection), 0.9f);
            //计算旋转角度(没有旋转的过程)
            
            //print(_currentDirection.sqrMagnitude);
            yield return new WaitForSeconds(time);
        }
    }
}

相关推荐

  1. unity中 鼠标移动端与pc端的位置

    2024-04-10 06:16:03       37 阅读
  2. Qt 鼠标移动释放事件

    2024-04-10 06:16:03       47 阅读
  3. Unity几种移动方式

    2024-04-10 06:16:03       60 阅读
  4. Unity相机跟随角色移动

    2024-04-10 06:16:03       52 阅读
  5. 了解Unity文件夹和路径

    2024-04-10 06:16:03       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 06:16:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 06:16:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 06:16:03       82 阅读
  4. Python语言-面向对象

    2024-04-10 06:16:03       91 阅读

热门阅读

  1. HTML5本地存储(localStorage和sessionStorage)

    2024-04-10 06:16:03       38 阅读
  2. ubuntu 18.04 安装 OpenSSL libssl.so.1.1

    2024-04-10 06:16:03       34 阅读
  3. Android Camera API 1打开相机失败

    2024-04-10 06:16:03       33 阅读
  4. Hadoop简介

    2024-04-10 06:16:03       35 阅读
  5. 数据仓库理论与实战

    2024-04-10 06:16:03       30 阅读
  6. 高并发环境下的实现与优化策略

    2024-04-10 06:16:03       42 阅读
  7. 百度机器学习算法春招一二三面面经

    2024-04-10 06:16:03       33 阅读
  8. 基于Flask测试深度学习模型预测

    2024-04-10 06:16:03       40 阅读
  9. Vscode使用教程

    2024-04-10 06:16:03       34 阅读
  10. 【hive】单节点搭建hadoop和hive

    2024-04-10 06:16:03       32 阅读
  11. Hadoop 源码中使用ServiceLoader

    2024-04-10 06:16:03       39 阅读
  12. vscode 关键字记录

    2024-04-10 06:16:03       31 阅读