PathDefinition.cs

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq; public class PathDefinition : MonoBehaviour
{
public Transform[] LinePoint;
List<string> test = new List<string>(); public IEnumerator<Transform> GetPathEnumeratror() {
if (LinePoint == null || LinePoint.Length < )
yield break; int direction = ;
int index = ; while (true) {
yield return LinePoint[index]; if (LinePoint.Length == ) continue; if (index <= ) direction = ;
else if (index >= (LinePoint.Length - )) direction = -; index = index + direction;
}
} void OnDrawGizmos()
{
if (LinePoint == null && LinePoint.Length < ) return; /// filter null
var Points = LinePoint.Where(t => t != null).ToList(); if (Points.Count < ) return; for (int i = ; i < LinePoint.Length; i++)
{
Vector3 start = LinePoint[i - ].position;
Vector3 end = LinePoint[i].position;
Gizmos.DrawLine(start, end);
} }
}

FollowPath.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class FollowPath : MonoBehaviour { public enum FollowType
{
MoveToward,
Lerp
} public FollowType Type = FollowType.MoveToward; public PathDefinition Path; public float Speed = ; public float MaxDistanceToGoal = .1f; IEnumerator<Transform> _currentPoint; public void Start() {
if (Path == null) {
return;
} _currentPoint = Path.GetPathEnumeratror(); _currentPoint.MoveNext(); //set position at start point
if (_currentPoint.Current == null) return; transform.position = _currentPoint.Current.position;
} void Update() {
if (_currentPoint == null || _currentPoint.Current == null) return; if(Type == FollowType.MoveToward)
transform.position = Vector3.MoveTowards(transform.position,_currentPoint.Current.position,Speed*Time.deltaTime);
else if(Type == FollowType.Lerp)
transform.position = Vector3.Lerp(transform.position, _currentPoint.Current.position, Speed * Time.deltaTime); var distanceSquared = (transform.position - _currentPoint.Current.position).sqrMagnitude;
if (distanceSquared < MaxDistanceToGoal * MaxDistanceToGoal) {
_currentPoint.MoveNext();
}
} }

代码 很简单 我就不注释了,比较适用于 路径 编辑 跟踪

Follow Path -》 Unity3d通用脚本的更多相关文章

  1. mMathf -》 Unity3d通用脚本

    public class mMathf { /// <summary> /// 辗转 相除法 求 最大公约数 /// a / b = k /// a % b = r /// 原理 gcd( ...

  2. Linux上java程序的jar包启动通用脚本(稳定用过)

    Linux上java程序的jar包启动通用脚本如下: #! /bin/sh export LANG="zh_CN.GBK" SERVICE_NAME=` .sh` SCRIPT_N ...

  3. unity3d进行脚本资源打包加载

    原地址:http://www.cnblogs.com/hisiqi/p/3204752.html 本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public c ...

  4. Unity3D批处理脚本

    Unity3D批处理脚本 本文属于转载,如有侵权,请留言,我会及时删除! Max09在模型到处的模型和U3D场景的尺寸不一致,Max09中的1m导到U3D中,只有0.01m,这时可以在U3D中将模型的 ...

  5. 【转】Unity3D中脚本的执行顺序和编译顺序

    支持原文,原文请戳: Unity3D中脚本的执行顺序和编译顺序 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与脚本有关的也就是编译和执行啦 ...

  6. sql server编写通用脚本自动检查两个不同服务器的新旧数据库的表结构差异

    问题:工作过程中,不管是什么项目,伴随着项目不断升级版本,对应的项目数据库业务版本也不断升级,数据库出现新增表.修改表.删除表.新增字段.修改字段.删除字段等变化,如果人工检查,数据库表和字段比较多的 ...

  7. sql server编写通用脚本自动统计各表数据量心得

    工作过程中,如果一个数据库的表比较多,手工编写统计脚本就会比较繁琐,于是摸索出自动生成各表统计数据量脚本的通用方法,直接上代码: /* 脚本来源:https://www.cnblogs.com/zha ...

  8. Linux下shell通用脚本启动jar(微服务)

    Linux下shell通用脚本启动jar(微服务) vim app_jar.sh #!/bin/bash #source /etc/profile # Auth:Liucx # Please chan ...

  9. Unity3D 之脚本架构,优雅地管理你的代码

    本文参考雨松MOMO大神的帖子: 图片全部来自他的帖子(请允许我偷懒下) --------------------------------------------------------------- ...

随机推荐

  1. Optimistic Offline Lock乐观离线锁

    通过冲突检测和(发生冲突时的)事务回滚,来防止并发业务事务中的冲突. 通常一个业务事务的执行,会跨越一系列的系统事务. 一旦超出了单个系统事务的范围,就不能仅依靠DB管理程序来保证数据一致性. 乐观离 ...

  2. JAVA(int...i)问题

    第一次看到这种写法,弄不明白到底会是神马结果,运行后才发现是这么的神奇. 无论你调用的方法中带几位参数,“hello”一直可以被输出.

  3. 杭电ACM2061--Treasure the new start, freshmen!

    http://acm.hdu.edu.cn/showproblem.php?pid=2061 这题很简单.注意换行. <span style="font-size:18px;" ...

  4. Codevs 1371 浴火银河跑运输

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold  题目描述 Description: 小 K 又在玩浴火银河了...不过这次他的目的真的是跑运输赚钱... 他想知 ...

  5. centos6.5下的mysql5.6.30安装

    1.解压mysql tar -xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz  -C /usr/local mv mysql-5.6.30-linux-gli ...

  6. NFS参数配置详细说明

    摘抄自:http://www.linuxidc.com/Linux/2012-05/61527p3.htm 1.NFS概述 NFS:Network file system,网络文件系统: 由sun公司 ...

  7. 安装ipvsadm时出现下面所示错误,MARK

    [root@localhost ipvsadm-1.26]# makemake -C libipvsmake[1]: Entering directory `/usr/local/soft/ipvsa ...

  8. WINDOWS下PHP 的pear DB的安装(本地环境:PHP5.4.15+Apache+mysql)

    因为需要安装phpunit,要先装pear,网上的教程大多数是以双击go-pear.bat开始,但是我安装的php文件夹里压根没有这个文件. 经过几次搜索之后终于找到了办法. 解决步骤如下: 1.下载 ...

  9. html css布局

    这几天有点急于求成了,原来每一门技术都像大海,只有深入其中才发现它比看到的更要深广的多. 虽然忙里偷闲的看了HTML5,NODE.JS,JAVASCRIPT核心等许多东西,但是真正掌握的不足十分之一, ...

  10. suse linux 操作系统下打BASH补丁

    1.检查当前版本信息: bash -version echo $BASH_VERSION   2.打4.3版本的补丁 在tmp目录下(保险起见,空间至少要100M以上)新建一个bash_upgrade ...