Transform.translate is moved by setting the vector direction and size of the next move.
There are two ways to move:
1.ADWS control Game object Move up and down, do not involve rotation;
The 2.AD is controlled by the rotation control direction, the WS control moves before and after, also can realize the controlled displacement.
Either way, you get the keyboard response first, you can get it through input.getkey, or you can get it through Input.getaxisraw.
The code in the first way is as follows:
floatInput_h = Input.getaxisraw ("Horizontal"); Gets the direction of movement in the x direction, if input a, output-1; If you enter D, Output 1. floatInput_v = Input.getaxisraw ("Vertical"); Gets the direction of movement of the z direction, if input w, output 1, if input s, output-1. Vector3 v=NewVector3 (Input_h,0, Input_v); New Move Vector v=v.normalized; If it is a diagonal direction, it needs to be standardized, the uniform length is 1v= v * Speed *Time.deltatime; Multiply speed to adjust movement speed, multiply deltatime to prevent transform.translate (v); Moving
The code in the second way is as follows:
float Input_h = Input.getaxisraw ("horizontal"); float input_v = Input.getaxisraw ("Vertical"); Transform. Rotate (New Vector3 (0, Input_h, 0)); Rotation around the y axis, a key clockwise; D key counterclockwise float curspeed = speed * Input_v * time.deltatime; Transform. Translate (Transform.forward * curspeed,space.world);//move around the object in front and back, because forward is used, so specify the moving coordinate system as the global coordinate
The last line of code can also be changed to:
Transform. Translate (New Vector3 (0,0,curspeed));//By default moves along the z axis of the object, which is the front and back direction
All of the above code needs to be implemented in the Update method.
Moving objects in Unity-transform.translate