《最新unity学习资料大全.doc》由会员分享,可在线阅读,更多相关《最新unity学习资料大全.doc(137页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateunity学习资料大全unity学习资料大全关于路径跟随设置点形成路径控制物体运动:用过Torque3D的人都知道,在这个引擎中可以在编辑模式下设定路径 然后AI就会按照这个路径运动,现在unity3D也可以啦 分享给大家 希望能有点帮助总共3个文件把第一个个文件放在新建的gameobject上 例如cube,然后creat empty创建空object作为splin
2、eparent,再创建4个或者多个sphere1,2,3,4作为它的子物体,然后将splineparent拖到这个代码var SplineParent : GameObject中,最后会看到cube按照sphere1到4的顺序运动JavaScript - SplineController.jsenum eOrientationMode NODE = 0, TANGENT var SplineParent : GameObject;var Duration : float = 10.0;var OrientationMode : eOrientationMode = eOrientationMo
3、de.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 O
4、nDrawGizmos() var trans : Array = GetTransforms(); if (trans.length 2) return; var interp = new SplineInterpolator(); SetupSplineInterpolator(interp, trans); interp.StartInterpolation(null, false, WrapMode); var prevPos : Vector3 = trans0.position; for (c=1; c = 100; c+) var currTime:float = c * Dur
5、ation / 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
6、 = 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
7、 = 0; c 0) SetupSplineInterpolator(mSplineInterp, mTransforms); mSplineInterp.StartInterpolation(null, true, WrapMode); JavaScript - SplineInterpolator.jsenum eEndPointsMode AUTO = 0, AUTOCLOSED, EXPLICIT enum eWrapMode ONCE = 0, LOOP private var mEndPointsMode = eEndPointsMode.AUTO;class SplineNode
8、 var Point : Vector3; var Rot : Quaternion; var Time : float; var EaseIO : Vector2; function SplineNode(p:Vector3, quaternion, t:float, io:Vector2) Point=p; Rot=q; Time=t; EaseIO=io; function SplineNode(o : SplineNode) Point=o.Point; Rot=o.Rot; Time=o.Time; EaseIO=o.EaseIO; private var mNodes : Arra
9、y = null;private var mState : String = ;private var mRotations : boolean = false;private var mOnEndCallback:Object;function Awake() Reset();function StartInterpolation(endCallback : Object, bRotations : boolean, mode : eWrapMode) if (mState != Reset) throw First reset, add points and then call here;
10、 mState = mode = eWrapMode.ONCE? Once : Loop; mRotations = bRotations; mOnEndCallback = endCallback; SetInput();function Reset() mNodes = new Array(); mState = Reset; mCurrentIdx = 1; mCurrentTime = 0.0; mRotations = false; mEndPointsMode = eEndPointsMode.AUTO;function AddPoint(pos : Vector3, quat :
11、 Quaternion, timeInSeconds : float, easeInOut : Vector2) if (mState != Reset) throw Cannot add points after start; mNodes.push(SplineNode(pos, quat, timeInSeconds, easeInOut);function SetInput() if (mNodes.length 2) throw Invalid number of points; if (mRotations) for (var c:int = 1; c Selective nega
12、tion if (Quaternion.Dot(mNodesc.Rot, mNodesc-1.Rot) 0) mNodesc.Rot.x = -mNodesc.Rot.x; mNodesc.Rot.y = -mNodesc.Rot.y; mNodesc.Rot.z = -mNodesc.Rot.z; mNodesc.Rot.w = -mNodesc.Rot.w; if (mEndPointsMode = eEndPointsMode.AUTO) mNodes.Unshift(mNodes0); mNodes.push(mNodesmNodes.length-1); else if (mEndP
13、ointsMode = eEndPointsMode.EXPLICIT & (mNodes.length 4) throw Invalid number of points;function SetExplicitMode() : void if (mState != Reset) throw Cannot change mode after start; mEndPointsMode = eEndPointsMode.EXPLICIT;function SetAutoCloseMode(joiningPointTime : float) : void if (mState != Reset)
14、 throw Cannot change mode after start; mEndPointsMode = eEndPointsMode.AUTOCLOSED; mNodes.push(new SplineNode(mNodes0 as SplineNode); mNodesmNodes.length-1.Time = joiningPointTime; var vInitDir : Vector3 = (mNodes1.Point - mNodes0.Point).normalized; var vEndDir : Vector3 = (mNodesmNodes.length-2.Poi
15、nt - mNodesmNodes.length-1.Point).normalized; var firstLength : float = (mNodes1.Point - mNodes0.Point).magnitude; var lastLength : float = (mNodesmNodes.length-2.Point - mNodesmNodes.length-1.Point).magnitude; var firstNode : SplineNode = new SplineNode(mNodes0 as SplineNode); firstNode.Point = mNo
16、des0.Point + vEndDir*firstLength; var lastNode : SplineNode = new SplineNode(mNodesmNodes.length-1 as SplineNode); lastNode.Point = mNodes0.Point + vInitDir*lastLength; mNodes.Unshift(firstNode); mNodes.push(lastNode);private var mCurrentTime = 0.0;private var mCurrentIdx = 1;function Update () if (
17、mState = Reset | mState = Stopped | mNodes.length = mNodesmCurrentIdx+1.Time) if (mCurrentIdx = mNodesmNodes.length-2.Time) return mNodesmNodes.length-2.Point; for (var c:int = 1; c timeParam) break; var idx:int = c-1; var param : float = (timeParam - mNodesidx.Time) / (mNodesidx+1.Time - mNodesidx.
18、Time); param = MathUtils.Ease(param, mNodesidx.EaseIO.x, mNodesidx.EaseIO.y); return GetHermiteInternal(idx, param);JavaScript - MathUtils.jsclass MathUtils / / / static function GetQuatLength(q : Quaternion) : float return Mathf.Sqrt(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w); / / / static function Get
19、QuatConjugate(q : Quaternion) : Quaternion return Quaternion(-q.x, -q.y, -q.z, q.w); / / Logarithm of a unit quaternion. The result is not necessary a unit quaternion. / static function GetQuatLog(q : Quaternion) : Quaternion var res : Quaternion = q; res.w = 0; if (Mathf.Abs(q.w) 0.0001) var coef:f
20、loat = theta/sin_theta; res.x = q.x*coef; res.y = q.y*coef; res.z = q.z*coef; return res; / / Exp / static function GetQuatExp(q : Quaternion) : Quaternion var res : Quaternion = q; var fAngle:float = Mathf.Sqrt(q.x*q.x + q.y*q.y + q.z*q.z); var fSin:float = Mathf.Sin(fAngle); res.w = Mathf.Cos(fAng
21、le); if (Mathf.Abs(fSin) 0.0001) var coef:float = fSin/fAngle; res.x = coef*q.x; res.y = coef*q.y; res.z = coef*q.z; return res; / / SQUAD Spherical Quadrangle interpolation Shoe87 / static function GetQuatSquad (t : float, q0 : Quaternion, q1 : Quaternion, a0 : Quaternion, a1 : Quaternion) var sler
22、pT:float = 2.0*t*(1.0-t); var slerpP = Slerp(q0, q1, t); var slerpQ = Slerp(a0, a1, t); return Slerp(slerpP, slerpQ, slerpT); static function GetSquadIntermediate (q0uaternion, q1uaternion, q2uaternion) var q1Inv : Quaternion = GetQuatConjugate(q1); var p0 = GetQuatLog(q1Inv*q0); var p2 = GetQuatLog
23、(q1Inv*q2); var sum : Quaternion = Quaternion(-0.25*(p0.x+p2.x), -0.25*(p0.y+p2.y), -0.25*(p0.z+p2.z), -0.25*(p0.w+p2.w); return q1*GetQuatExp(sum); / / Smooths the input parameter t. If less than k1 ir greater than k2, it uses a sin. Between k1 and k2 it uses / linear interp. / static function Ease(t : float, k1 :