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

JS的

第1个回答  推荐于2016-12-02
public var target:Transform;
public var moveSpeed=1;

function Start(){

if(!target){
print("not set target!");

var go=GameObject.CreatePrimitive( PrimitiveType.Cube);
target=go.transform;
target.position=Camera.main.transform.TransformPoint(Vector3(0,0,5));
target.rotation=Camera.main.transform.rotation;

}

}

function OnGUI(){

var width=60;
var height=20;
GUI.BeginGroup(Rect((Screen.width-width*2)/2,Screen.height-height*3,width*2,height*3));
var moveDirection=Vector3.zero;
if(GUI.Button(Rect(width/2,0,width,height),"forward")){
moveDirection.z=1;
}
if(GUI.Button(Rect(width/2,height*2,width,height),"back")){
moveDirection.z=-1;
}
if(GUI.Button(Rect(0,height,width,height),"left")){
moveDirection.x=-1;
}
if(GUI.Button(Rect(width,height,width,height),"right")){
moveDirection.x=1;
}

if(target){
moveDirection=moveDirection*moveSpeed;
target.position=target.position+ target.rotation*moveDirection;

}
GUI.EndGroup();

}本回答被提问者采纳