reprinted from Mo Rou Unity3d Learning Tour Original address: Unity3d Control object Movement, rotation, scaling
Transform basic move function:
1. Specify the direction to move:
float translatespeed = 10f; // Vector3.forward means "forward"transform. Translate (Vector3.forward *translatespeed);
2. Omni-Directional Movement:
float xspeed =-5f; // float zspeed = 10f; // transform. Translate (XSpeed,0, zspeed);
3. Reset Coordinates:
float xpostion =-5f; // float zpostion = 10f; // move the current object directly to the x-axis xpostion,y axis is the three-dimensional space position of the 0,z axis zpostion. transform.position = Vector3 (xpostion,0, zpostion);
Input control:
1. Enter the specified key:
// Press the keyboard "Up ARROW key" if (Input.getkey ("up")) print ("up! " ); // Press the keyboard "W key" if (Input.getkey (KEYCODE.W);) print ("w! ");
2. Mouse control
if ( Input.getmousebutton (0)) print ("Mouse down! " ); Input. Getaxis ("Mouse X"); // Input.getaxis ("Mouse Y"); // Mouse Vertical increment (move vertically)
3. Get the Axes:
// Horizontal axis/Vertical axis (this value ranges from 1 to 1 when the controller and keyboard input) Input.getaxis ("horizontal"); // Input.getaxis ("Vertical"); // Portrait
Hold down the mouse and drag the object to rotate and rotate the object at a custom angle:
floatSpeed =100.0f;floatx;floatZ;voidUpdate () {if(Input.getmousebutton (0)){//left mouse button to movey = Input.getaxis ("Mouse X") * Time.deltatime *Speed ; X= Input.getaxis ("Mouse Y") * Time.deltatime *Speed ; }Else{x= y =0 ; }//rotation angle (Increase)Transform. Rotate (NewVector3 (x, Y,0)); /**---------------Other modes of rotation----------------**/ //transform. Rotate (vector3.up *time.deltatime * speed);//rotate around the y-axis//for smooth rotation to custom targetsPinghuaxuanzhuan ();}//smooth rotation to a custom anglevoidOngui () {if(GUI. Button (Rect (Screen.width- the,Ten, -, -),"Set Rotation")) {//Custom Angletargetrotation= Quaternion.euler (45.0f,45.0f,45.0f); //set the rotation angle directly//transform.rotation = targetrotation; //smooth rotation to target angleIszhuan =true; }}BOOLiszhuan=false; Quaternion targetrotation;voidPinghuaxuanzhuan () {if(Iszhuan) {transform.rotation= Quaternion.slerp (Transform.rotation, targetrotation, Time.deltatime *3); }}
Keyboard Controls Object Scaling:
floatSpeed =5.0f;floatx;floatZ;voidUpdate () {x= Input.getaxis ("Horizontal") * Time.deltatime * speed;//levelz = Input.getaxis ("Vertical") * Time.deltatime * speed;//Vertical//"Fire1", "Fine2", "Fine3" maps to the Ctrl,alt,cmd key and the mouse's three-key or spine button. The new input axis can be added in Input manager. Transform.localscale + =NewVector3 (x,0, z); /**---------------re-set angle (one step)----------------**/ //Transform.localscale = new Vector3 (x, 0, z);}
Unity3d controls the movement, rotation, and scaling of objects