Previously, the Unity3d collision was simulated. This time, the motion of the object will be controlled through the script. Simply use the "W", "S", "A", and "D" buttons to control the object's "forward", "backward", "Left", and "right ".
Because the project file for the previous exercise has disappeared and Images cannot be provided for the time being. If necessary, add it later.
----------------------
Concept: use scripts to get control of components
Add a cube and add a C # script.
The Code is as follows:
Transform tCube; // declare a Transform (transformation) Type Variable void Start () {// obtain control of corresponding components (other component methods are similar) tCube = (Transform) gameObject. getComponent ("Transform");} void Update () {tCube. rotate (0, 15, 5 );}
-----------------------
(UI control usage)
Create a GUIText control and attach a C # script. The Code is as follows:
GUIText txt1; txt1 = (GUIText) gameObject. getComponent ("GUIText"); static bool anyKey; // receives whether the keyboard has been tapped static Vector3 mousePosition; // receives the mouse coordinate anyKey = Input. anyKey; // determines whether the key action has occurred. anyKey = Input. getKey (KeyCode. a); // determines whether 'A' txt1 is pressed. text = anyKey. toString (); anyKey = Input. getKey (KeyCode. a); // if A is released, it is FalseInput. getKeyDown (KeyCode. a); // loose A. It is still AmousePosition = Input before the key is not. mousePosition; // mousePosition. x is the X coordinate value txt1.text = mo UsePosition. ToString (); bool I = Input. GetMouseButton (1); // right-click in the left of the table 1, 2, and 3 respectively. // Cardinar // Terrain to create the earth surface. Create a cube and add the rigid body attribute. // Translate (1 * Time. deltaTime, 0, 0, Space. Self); // Space. Self coordinate system. World coordinate system. 1 * Time. deltaTime: Move a unit (control speed) in one second. // After the camera adjusts its vision, it can be used as a subclass of the car. The camera will move with the car. Public class car: MonoBehaviour {Transform tCar; // Use this for initialization void Start () {tCar = (Transform) gameObject. getComponent ("Transform");} // Update is called once per frame void Update () {// forward if (Input. getKey (KeyCode. w) {tCar. translate (1 * Time. deltaTime, 0, 0, Space. self);} // returns if (Input. getKey (KeyCode. s) {tCar. translate (-1 * Time. deltaTime, 0, 0, Space. self);} // left turn if (Input. getKey (KeyCode. a) {tCar. rotate (0,-15 * Time. deltaTime, 0, Space. self);} // turn right if (Input. getKey (KeyCode. d) {tCar. rotate (0, 15 * Time. deltaTime, 0, Space. self );}}}