关于路径跟随设置点形成路径控制物体运动

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
trans[c].position, trans[c].up); else rot = trans[c].rotation;
1.0)); }
interp.AddPoint(trans[c].position, rot, step*c, Vector2(0.0, }
if (AutoClose) interp.SetAutoCloseMode(step*c);
var trans : Array = GetTransforms();
if (trans.length < 2) return;
var interp = new SplineInterpolator(); SetupSplineInterpolator(interp, trans);
interp.StartInterpolation(null, false, WrapMode);
}
// We need this to sort GameObjects by name class NameComparer extends IComparer {
function Compare(trA : Object, trB : Object) : int { return trA.gameObject.name.CompareTo(trB.gameObject.name);
if (AutoStart) FollowSpline();
}
function SetupSplineInterpolator(interp:SplineInterpolator, trans:Array) : void {
interp.Reset();
if (AutoClose) var step : flwenku.baidu.comat = Duration / trans.length;
private var mOnEndCallback:Object;
function Awake() {
Reset(); }
function StartInterpolation(endCallback : Object, bRotations : boolean, mode : eWrapMode) {
JavaScript - SplineController.js
enum eOrientationMode { NODE = 0, TANGENT }
var SplineParent : GameObject; var Duration : float = 10.0; var OrientationMode : eOrientationMode = eOrientationMode.NODE; var WrapMode : eWrapMode = eWrapMode.ONCE; var AutoStart : boolean = true; var AutoClose : boolean = true; var HideOnExecute : boolean = true;
// We need to get rid of the parent, which is also returned by GetComponentsInChildren...
for (var tr : Transform in tempTransforms) {
if (tr != SplineParent.transform) tempTransformsArray.Add(tr);
} else if (OrientationMode == OrientationMode.TANGENT) {
if (c != trans.length-1) var rot : Quaternion =
Quaternion.LookRotation(trans[c+1].position - trans[c].position, trans[c].up); else if (AutoClose) rot = Quaternion.LookRotation(trans[0].position -
SetInput(); }
function Reset()
{ mNodes = new Array(); mState = "Reset"; mCurrentIdx = 1; mCurrentTime = 0.0; mRotations = false; mEndPointsMode = eEndPointsMode.AUTO;
// We need to use an ArrayList because there&acute;s not Sort method in Array...
var tempTransformsArray = new ArrayList(); var tempTransforms =
SplineParent.GetComponentsInChildren(Transform);
mNodes.push(SplineNode(pos, quat, timeInSeconds, easeInOut)); }
function SetInput() {
if (mNodes.length < 2) throw "Invalid number of points";
if (mRotations) {
} }
function Start() {
mSplineInterp = gameObject.AddComponent(SplineInterpolator);
mTransforms = GetTransforms();
if (HideOnExecute) DisableTransforms();
} }
// // Returns children transforms already sorted by name // function GetTransforms() : Array {
var ret : Array = new Array();
if (SplineParent != null) {
private var mSplineInterp : SplineInterpolator = null; private var mTransforms : Array = null;
@script AddComponentMenu("Splines/Spline Controller")
function OnDrawGizmos() {
if (mTransforms.length > 0) {
SetupSplineInterpolator(mSplineInterp, mTransforms); mSplineInterp.StartInterpolation(null, true, WrapMode);
} }
JavaScript - SplineInterpolator.js
if (mState != "Reset") throw "First reset, add points and then call here";
mState = mode == eWrapMode.ONCE? "Once" : "Loop"; mRotations = bRotations; mOnEndCallback = endCallback;
}
function AddPoint(pos : Vector3, quat : Quaternion, timeInSeconds : float, easeInOut : Vector2) {
if (mState != "Reset") throw "Cannot add points after start";
function SplineNode(o : SplineNode) { Point=o.Point; Rot=o.Rot; Time=o.Time; EaseIO=o.EaseIO; } }
private var mNodes : Array = null; private var mState : String = ""; private var mRotations : boolean = false;
}
tempTransformsArray.Sort(new NameComparer()); ret = Array(tempTransformsArray); }
return ret; }
// // Disables the spline objects, we generally don't need them because they are just
enum eEndPointsMode { AUTO = 0, AUTOCLOSED, EXPLICIT } enum eWrapMode { ONCE = 0, LOOP }
private var mEndPointsMode = eEndPointsMode.AUTO;
class SplineNode
var prevPos : Vector3 = trans[0].position; for (c=1; c <= 100; c++) {
var currTime:float = c * Duration / 100.0; var currPos = interp.GetHermiteAtTime(currTime); var mag:float = (currPos-prevPos).magnitude * 2.0; Gizmos.color = Color(mag, 0.0, 0.0, 1.0); Gizmos.DrawLine(prevPos, currPos); prevPos = currPos;
auxiliary // function DisableTransforms() : void {
if (SplineParent != null) {
SplineParent.SetActiveRecursively(false); } }
// // Starts the interpolation // function FollowSpline() {
for (var c:int = 1; c < mNodes.length; c++) {
// Always interpolate using the shortest path -> Selective negation
{
var Point : Vector3;
var Rot
: Quaternion;
var Time var EaseIO
: float; : Vector2;
function SplineNode(p:Vector3, quaternion, t:float, io:Vector2) { Point=p; Rot=q; Time=t; EaseIO=io; }
用过 Torque3D 的人都知道,在这个引擎中可以在编辑模式下设定路径 然后 AI 就会按照这 个路径运动,现在 unity3D 也可以啦 分享给大家 希望能有点帮助~总共 3 个文件 把第一个个文件放在新建的 gameobject 上 例如 cube,然后 creat empty 创建空 object 作 为 splineparent,再创建 4 个或者多个 sphere1,2,3,4 作为它的子物体,然后将 splineparent 拖到这个代码 var SplineParent : GameObject 中,最后会看到 cube 按照 sphere1 到 4 的顺序运动~
else step = Duration / (trans.length-1);
for (var c:int = 0; c < trans.length; c++) {
if (OrientationMode == OrientationMode.NODE) {
interp.AddPoint(trans[c].position, trans[c].rotation, step*c, Vector2(0.0, 1.0));
相关文档
最新文档