Follow Path -》 Unity3d通用脚本
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通用脚本的更多相关文章
- mMathf -》 Unity3d通用脚本
public class mMathf { /// <summary> /// 辗转 相除法 求 最大公约数 /// a / b = k /// a % b = r /// 原理 gcd( ...
- Linux上java程序的jar包启动通用脚本(稳定用过)
Linux上java程序的jar包启动通用脚本如下: #! /bin/sh export LANG="zh_CN.GBK" SERVICE_NAME=` .sh` SCRIPT_N ...
- unity3d进行脚本资源打包加载
原地址:http://www.cnblogs.com/hisiqi/p/3204752.html 本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public c ...
- Unity3D批处理脚本
Unity3D批处理脚本 本文属于转载,如有侵权,请留言,我会及时删除! Max09在模型到处的模型和U3D场景的尺寸不一致,Max09中的1m导到U3D中,只有0.01m,这时可以在U3D中将模型的 ...
- 【转】Unity3D中脚本的执行顺序和编译顺序
支持原文,原文请戳: Unity3D中脚本的执行顺序和编译顺序 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与脚本有关的也就是编译和执行啦 ...
- sql server编写通用脚本自动检查两个不同服务器的新旧数据库的表结构差异
问题:工作过程中,不管是什么项目,伴随着项目不断升级版本,对应的项目数据库业务版本也不断升级,数据库出现新增表.修改表.删除表.新增字段.修改字段.删除字段等变化,如果人工检查,数据库表和字段比较多的 ...
- sql server编写通用脚本自动统计各表数据量心得
工作过程中,如果一个数据库的表比较多,手工编写统计脚本就会比较繁琐,于是摸索出自动生成各表统计数据量脚本的通用方法,直接上代码: /* 脚本来源:https://www.cnblogs.com/zha ...
- Linux下shell通用脚本启动jar(微服务)
Linux下shell通用脚本启动jar(微服务) vim app_jar.sh #!/bin/bash #source /etc/profile # Auth:Liucx # Please chan ...
- Unity3D 之脚本架构,优雅地管理你的代码
本文参考雨松MOMO大神的帖子: 图片全部来自他的帖子(请允许我偷懒下) --------------------------------------------------------------- ...
随机推荐
- 7款超酷HTML5 3D动画精选应用及源码
对以前来讲,3D动画拿到网页上展示是一件非常奢侈的事情,第一是浏览器不够先进,第二是大部分只能用flash实现伪3D.HTML5的出现,让实现网页3D动画变得非常简单,当然前提是你不要再使用像IE67 ...
- 《HTML5与CSS3基础教程》学习笔记 ——One Day
第一章 1. 邮箱地址的URL地址包括:mailto:+邮箱地址 2. ../表示向上走一级,开头直接使用/表示根目录 第三章 1. <header>: role = “ ...
- 《C++Primer中文版》读书笔记——第1章 开始
istream对象:cin(标准输入对象); ostream对象:cout(标准输出对象) cerr(输出错误和警告) clog(输出一般性信息) 读取数量不定的输入数据,eg , sum=; whi ...
- 探索VS中C++多态实现原理
引言 最近把<深度探索c++对象模型>读了几遍,收获甚大.明白了很多以前知其然却不知其所以然的姿势.比如构造函数与拷贝构造函数什么时候被编译器合成,虚函数.实例函数.类函数的区别等等.在此 ...
- Ubuntu系统下允许Apache的mod_rewrite功能
首先,使能apache的rewirte模块,在shell里输入下边的命令: sudo a2enmod rewrite 然后重启一下webserver使更改生效 sudo service apache2 ...
- u-boot ctr0.S详解 包含_main函数
/** ****************************************************************************** * @author Maox ...
- 【Qt】Qt之自定义界面(实现无边框、可移动)【转】
简述 UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性.有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位和特点. 爱美之心人皆有之.其实软件 ...
- 解决Win7下运行php Composer出现SSL报错的问题
以前都在linux环境使用php composer.今天尝试在win7下运行composer却出现SSL报错: D:\data\www\mmoyu\symapp>php -f %phprc%\c ...
- allegro生成光绘文件时,通过cam打开,*.drl钻孔文件不识别,为Unknow类型
生成钻孔文件时,NC_Parameters中,应该选Absolute
- 数据分析≠Hadoop+NoSQL,不妨先看完善现有技术的10条捷径(分享)
Hadoop让大数据分析走向了大众化,然而它的部署仍需耗费大量的人力和物力.在直奔Hadoop之前,是否已经将现有技术推向极限?这里总结了对Hadoop投资前可以尝试的10个替代方案, ...