05 Unity3D horizontal motion control-programmer learning Unity3d

Source: Internet
Author: User

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 );}}}


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.