关于路径跟随设置点形成路径控制物体运动
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
用过Torque3D的人都知道,在这个引擎中可以在编辑模式下设定路径然后AI就会按照这个路径运动,现在unity3D也可以啦分享给大家希望能有点帮助~总共3个文件
把第一个个文件放在新建的gameobject上例如cube,然后creat empty创建空object作为splineparent,再创建4个或者多个sphere1,2,3,4作为它的子物体,然后将splineparent拖到这个代码var SplineParent : GameObject中,最后会看到cube按照sphere1到4的顺序运动~
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;
private var mSplineInterp : SplineInterpolator = null;
private var mTransforms : Array = null;
@script AddComponentMenu("Splines/Spline Controller")
function OnDrawGizmos()
{
var trans : Array = GetTransforms();
if(trans.length < 2)
return;
var interp = new SplineInterpolator();
SetupSplineInterpolator(interp, trans);
interp.StartInterpolation(null, false, WrapMode);
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;
}
}
function Start()
{
mSplineInterp = gameObject.AddComponent(SplineInterpolator);
mTransforms = GetTransforms();
if(HideOnExecute)
DisableTransforms();
if(AutoStart)
FollowSpline();
}
function SetupSplineInterpolator(interp:SplineInterpolator, trans:Array) : void {
interp.Reset();
if(AutoClose)
var step : float = Duration / trans.length;
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));
}
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 -
trans[c].position, trans[c].up);
else
rot = trans[c].rotation;
interp.AddPoint(trans[c].position, rot, step*c, Vector2(0.0, 1.0));
}
}
if(AutoClose)
interp.SetAutoCloseMode(step*c);
}
// We need this to sort GameObjects by name
class NameComparer extends IComparer
{
function Compare(trA : Object, trB : Object) : int {
return pareTo();
}
}