Unity3d 如何用按钮控制物体前后左右移动?JS的
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/19 16:34:27
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
}