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大神的帖子: 图片全部来自他的帖子(请允许我偷懒下) --------------------------------------------------------------- ...
随机推荐
- .NET清除Session 的几个方法[clear/removeAll/remove/Abandon]
1.clear() 清空所有session对象的值,但保留会话 2.removeAll() 调用clear()方法 3.remove("SessionName") 删除某个 ...
- MFC控件随窗口大小变化原理及实现
本文主要针对MFC的dialog,实现控件随窗口大小变化. 原理:首先获取dialog的初始大小,当窗口发送变动时,调用OnSize事件和方法,计算缩放比例,然后对界面中的所有控件进行缩放和布局. 实 ...
- unity 多线程
对于客户端来说,好的用户体验,需要保持一个快速响应的用户界面.于是便要求:网络请求.io操作等 开销比较大的操作必须在后台线程进行,从而避免主线程的ui卡顿.(注:协程也是主线程的一部分,进行大量的i ...
- 《Usermod:user lee is currently logged in 家目录不能改变解决方法》
前面短时间自己玩samba服务时,上面的所有服务都做好了,家目录死活就是不能访问,删掉自己的smb.conf文件,自己到别的服务上用rsync同步过来的文件,启动服务家目录还是不能访问,排了一下午,终 ...
- cadence16.3破解方法
今天安装cadence16.3,安装了两遍都失败了,百思不得其解,结果总是出现在license上,如下图所示: 后面当然就只能启动demo版了,于是网上找,参照以前古老的法子,终于解决问题! 首先,开 ...
- WordPress 主题开发 - (二) 理解主题 待翻译
What is “Theme Sense”? What is “Theme Sense”? Theme Sense is an intuitive understanding of WordPress ...
- 忘记linux root密码怎么办?
摘自:<鸟哥的Linux私房菜> 常常有些朋友在配置好了Linux之后,结果root密码给他忘记去!要重新安装吗?不需要的, 你只要以单人维护模式登陆即可更改你的root密码喔!由于lil ...
- Python中处理时间 —— time模块
time模块 time 模块可用来处理时间,详细的说明参考 time模块说明. 逝去的秒数 逝去的秒数表示从某个时间(Python中是"Thu Jan 1 07:00:00 1970&quo ...
- Antelope 和Barracuda区别
Antelope是innodb-base的文件格式, Barracude是innodb-plugin后引入的文件格式,同时Barracude也支持Antelope文件格式.两者区别在于: 文件格式 支 ...
- orcale 修改字段属性
有些时候,因为没能预料到一些情况的变化,需要修改字段的类型.如果是varchar型,直接增加长度是可以的,但是如果需要修改成其他类型就不能这么做了. 思路:1.增加一个临时列,把需要修改的那个字段的数 ...