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. UI3_UIViewController生命周期

    // // SecondViewController.h // UI3_UIViewController生命周期 // // Created by zhangxueming on 15/7/2. // ...

  2. 正确的安装和使用nvm

    前言 目前主流的node版本管理工具有两种,nvm和n.两者差异挺大的,具体分析可以参考一下淘宝FED团队的一篇文章: 管理 node 版本,选择 nvm 还是 n? 总的来说,nvm有点类似于 Py ...

  3. 如何给xml应用样式

    引子:可扩展标记语言xml(Extensible Markup Language)自己平常也用到的不多,除了在ajax处理服务器返回的数据可能会用到外(不过一般用json处理数据的比较常见)还真没怎么 ...

  4. daily news新闻阅读客户端应用源码(兼容iPhone和iPad)

    daily news新闻阅读客户端应用源码(兼容iPhone和iPad),也是一款兼容性较好的应用,可以支iphone和ipad的阅读阅读器源码,设计风格和排列效果很不错,现在做新闻资讯客户端的朋友可 ...

  5. 《gpg文件加密的使用》RHEL6

    甲端: 首先是要生成一对密钥: 提示是否要生成2048个字节的密钥对:   下面都是生成密钥对时的步骤: 按“o”键开始生成密钥对: 提示要我给密钥对加个密码: 输入2次 之后密钥对的字符需要我按键盘 ...

  6. ISE 中使用system generate

    本文讲解简单的ISE中使用system generate,system generate基本使用规则在此不详细说明可以见博客http://blog.csdn.net/xiabodan/article/ ...

  7. yaf运行错误:Class 'Yaf_Application' not found

    提示:致命错误 Yaf_Application 基类没有加载进去 一检查:phpinfo() 里yaf 扩展有没有安装上 扩展也安装进去了 这时在分布式配置文件的重写 也是正确 这时百思不得其解,没办 ...

  8. 完美解决fixed 水平居中问题

    群里的朋友问的,发现自己没写过:就写了下,原理和网上的fixed上下左右四个角的原理一样! 1.防止页面振动: body{ _background-image: url(about:blank); _ ...

  9. Android:自定义控件样式(Selector)

    前言 在开发一个应用程序过程中不可避免的要去修改组件的样式,比如按钮.输入框等.现在就看下如何通过Seletor实现样式的自定义.先看下简单的效果对比

  10. 关于查看Android系统源码【Written By KillerLegend】

    可能你会想下载Android系统源码,但是我不知道你会看多少系统的源码,如果你对源码只是偶尔看一次的话,推荐你在线看Android的系统源码,下面提供几种查看android系统源码的方法. 1:打开这 ...