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. Eclipse 快捷键 快捷输入

    快捷键: 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如appli ...

  2. Integer类型值相等或不等分析

    看到博客园一位博友写的面试问题,其中一题是 Integer a = 1; Integer b = 1 ; (a == b)?true :false; 当时我一看,这不是明显的true 嘛,  看到评论 ...

  3. 洛谷 P3374 【模板】树状数组 1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  4. 暑假集训(4)第五弹——— 数论(hdu1222)

    题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A ...

  5. debian终端菱形乱码修复

    最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok

  6. Linux 静态库和动态库 使用说明

        Linux下程序运行中,有两种库,静态库和动态库.     静态库:名字一般为libxxx.a,编译时会整合到可执行程序中,优点是运行时不需要外部函数库支持,缺点是编译后程序较大,一旦静态库改 ...

  7. 《Usermod:user lee is currently logged in 家目录不能改变解决方法》

    前面短时间自己玩samba服务时,上面的所有服务都做好了,家目录死活就是不能访问,删掉自己的smb.conf文件,自己到别的服务上用rsync同步过来的文件,启动服务家目录还是不能访问,排了一下午,终 ...

  8. JSON结构

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

  9. MATLAB light material lighting

    clf;[X,Y,Z]=sphere(40);colormap(jet)subplot(1,2,1),surf(X,Y,Z),axis off square,shading interplight(' ...

  10. 【Web学习日记】——C#引用WebService,从配置文件改变引用地址

    开发环境:Win7 32位,开发工具:VS2013,.Net:4.0 初用WebService,很多地方都搞不清楚怎么回事,但稍作研究之后,也就用上了,根本就没有考虑后续事情. 但是,随着项目的进行, ...