Unity3d 如何用按钮控制物体前后左右移动?JS的

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/19 16:34:27
Unity3d 如何用按钮控制物体前后左右移动?JS的
xMo0ǿSRR7k5UpD7N\M N^` ^.|m\ ,$qY&8=۔ggN/Lvqt1~=?Oﳓ_ӻ//;o}fz)i%1@ cqg#:0X ~gʵAD]AVha6t\+:dHjP(`E_閲 ( = vr$ ~a7rpEQlAn!468"YdiOgokakB$L%eնLD= ~X>4WCi)CL[>=΢P{Di}cLa&zKo|^46|y'gy*WKJ$ҍL6F-I.^M/ FX,.AD"Zcպ̅<PnN)WI<_iVYՋc}o+'gɢ.#jߩ(D4{[07D

Unity3d 如何用按钮控制物体前后左右移动?JS的
Unity3d 如何用按钮控制物体前后左右移动?
JS的

Unity3d 如何用按钮控制物体前后左右移动?JS的
public var target:Transform;
public var moveSpeed=1;
function Start(){
\x05
\x05if(!target){
\x05\x05print("not set target!");
\x05\x05
\x05\x05var go=GameObject.CreatePrimitive( PrimitiveType.Cube);
\x05\x05target=go.transform;
\x05\x05target.position=Camera.main.transform.TransformPoint(Vector3(0,0,5));
\x05\x05target.rotation=Camera.main.transform.rotation;
\x05\x05
\x05}
\x05
}
function OnGUI(){
\x05var width=60;
\x05var height=20;
\x05GUI.BeginGroup(Rect((Screen.width-width*2)/2,Screen.height-height*3,width*2,height*3));
\x05var moveDirection=Vector3.zero;
\x05if(GUI.Button(Rect(width/2,0,width,height),"forward")){
\x05 \x05moveDirection.z=1;
\x05}
\x05if(GUI.Button(Rect(width/2,height*2,width,height),"back")){
\x05\x05moveDirection.z=-1;
\x05}
\x05if(GUI.Button(Rect(0,height,width,height),"left")){
\x05\x05moveDirection.x=-1;
\x05}
\x05if(GUI.Button(Rect(width,height,width,height),"right")){
\x05\x05moveDirection.x=1;
\x05}
\x05if(target){
\x05\x05moveDirection=moveDirection*moveSpeed;
\x05\x05target.position=target.position+ target.rotation*moveDirection;
\x05
\x05}
\x05GUI.EndGroup();
\x05
\x05
\x05
}