养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

Unity 物体运动 之 物体按规定路线运动的优化

时间:2024-10-21 20:41:59

Unity物体运动之物体按规定路线运动的优化。本节针对之前已有的《Unity物体运动之物体按规定路线运动的使用》(链接如下)的不足,物体运动朝向不对的问题,进行优化的简单案例,具体如下

工具/原料

Unity

一、知识要点

1、Vector3.MoveTowards:1)功能简述publicstatic外狒芙梨Vector3MoveTowards(Vector3current,Vector3target,惺绅寨瞀floatmaxDistanceDelta);Movesapointcurrentinastraightlinetowardsatargetpoint.ThevaluereturnedbythisfunctionisapointmaxDistanceDeltaunitsclosertoatarget/pointalongalinebetweencurrentandtarget.IfthetargetiscloserthanmaxDistanceDelta/thenthereturnedvaluewillbeequaltotarget(ie,themovementwillnotovershootthetarget).NegativevaluesofmaxDistanceDeltacanbeusedtopushthepointawayfromthetarget.2)使用举例usingUnityEngine;usingSystem.Collections;publicclassExampleClass:MonoBehaviour{publicTransformtarget;publicfloatspeed;voidUpdate(){floatstep=speed*Time.deltaTime;transform.position=Vector3.MoveTowards(transform.position,target.position,step);}}

2、Transform.LookAt:1)功能简述publicvoidLoo氯短赤亻kAt(Transformtarget,Vec墉掠载牿tor3worldUp=Vector3.up);Parameterstarget:Objecttopointtowards.worldUp:Vectorspecifyingtheupwarddirection.Rotatesthetransformsotheforwardvectorpointsat/target/'scurrentposition.ThenitrotatesthetransformtopointitsupdirectionvectorinthedirectionhintedatbytheworldUpvector.IfyouleaveouttheworldUpparameter,thefunctionwillusetheworldyaxis.worldUpisonlyahintvector.TheupvectoroftherotationwillonlymatchtheworldUpvectoriftheforwarddirectionisperpendiculartoworldUp.2)使用举例//Thiscompletescriptcanbeattachedtoacameratomakeit//continuouslypointatanotherobject.//Thetargetvariableshowsupasapropertyintheinspector.//Draganotherobjectontoittomakethecameralookatit.usingUnityEngine;usingSystem.Collections;publicclassExampleClass:MonoBehaviour{publicTransformtarget;voidUpdate() {//Rotatethecameraeveryframesoitkeepslookingatthetargettransform.LookAt(target);}}

二、物体运动之物体按规定路线运动的优化

1、在Unity场景中新建一个“Plane”和“Cube”是低昂调整大小与布局,“MaainCamera”的视角调整好,具体如下图

Unity 物体运动 之 物体按规定路线运动的优化

2、在场景中添加路线的关键点,演示需要大致随意摆放几个点,具体如下图

Unity 物体运动 之 物体按规定路线运动的优化

Unity 物体运动 之 物体按规定路线运动的优化

3、在工程中,仙剑两个脚本“Move”和“WayPoints”,代开脚本进行编辑,具体如下图

Unity 物体运动 之 物体按规定路线运动的优化

4、在“WayPoints”脚本上编辑代码,具体代码和代码说明如下图

Unity 物体运动 之 物体按规定路线运动的优化

5、“WayPoints”脚本具体内容如下:usingUnityEngine;publicclassWayPoints:MonoBehaviour{publicstaticTransform[]wayPoints;voidAwake(){intcount=transform.childCount;wayPoints=newTransform[count];for(inti=0;i<count;i++){wayPoints[i]=transform.GetChild(i);}}}

6、在“Move”脚本上编辑代码,本脚本关键使用LookAt朝向目标,然后MoveTowards在移动到目标点,具体代码和代码说明如下图

Unity 物体运动 之 物体按规定路线运动的优化

7、“Move”脚本具体内容如蛴蛩钔淞下:usingUnityEngine;publicclassMove:MonoBeh锾攒揉敫aviour{publicfloatspeed=5;privateTransform[]ways;privateintindex;//UsethisforinitializationvoidStart(){ways=WayPoints.wayPoints;index=0;}//UpdateiscalledonceperframevoidUpdate(){MoveTo();}voidMoveTo(){if(index>ways.Length-1)return;//保证正面朝向物体运动transform.LookAt(ways[index].position);transform.position=Vector3.MoveTowards(transform.position,ways[index].position,Time.deltaTime*speed);if(Vector3.Distance(ways[index].position,transform.position)<0.2f){index++;if(index==ways.Length){transform.position=ways[index-1].position;}}}}

8、脚本编译正确,回到Unity界面,“WayPoints”脚本赋给场景中的“WayPoints”,“Move”脚本赋给“Cube”

9、运行场景,可以看到,物体首先朝向目标点,然后在向目标点运动,弥补朝向不变的不足,具体如下图

Unity 物体运动 之 物体按规定路线运动的优化

10、到此,《Unity物体运动之物体按规定路线运动的优化》讲解结束,谢谢

© 一点知识