[Unity] 6.4 Transform -- move, rotate, and scale game objects, unitytransform --

Source: Internet
Author: User

[Unity] 6.4 Transform -- move, rotate, and scale game objects, unitytransform --

Classification: Unity, C #, VS2015

Created on: 1. Introduction

The Unity engine provides a wide range of components and class libraries, providing great convenience for game development. Mastering and using these Apis is very important for the efficiency of game development.

This section describes the basic usage of Transform. The running effect of this example is as follows:

Ii. Transform components

Each object in a scenario has a Transform.

The Transform component determines the location, direction, and scaling ratio of the game object. If you want to update the player position and set the camera viewing angle in the game, you must deal with the Transform component.

Each Transform can have a parent level that allows you to apply positions, rotate, and scale horizontally. You can view the Hierarchy on the Hierarchy panel. They also support counters (enumerator), so you can traverse sub-objects cyclically. For example:

using UnityEngine;using System.Collections;public class example : MonoBehaviour{    public void Awake()    {        foreach (Transform child in transform)        {             child.position += Vector3.up * 10.0F;        }    }}

1. member variables

2. Method

For example (C # script ):

Void Update () {// the object (1 unit/second) transform moves forward along the Z axis relative to its own coordinate system. translate (0, 0, Time. deltaTime); // In the world coordinate system, move the object up (1 unit/second) transform. translate (0, Time. deltaTime, 0, Space. world); // relative to the camera, move the object to the right (1 unit/second) transform. translate (Vector3.right * Time. deltaTime, Camera. main. transform); // relative to its own coordinate system, the system moves the object to the right (1 unit/second) transform. translate (Time. deltaTime, 0, 0, Camera. main. transform );}

Among them, the options of relativeTo are:

Space. Self -- default. Move relative to the self-axis of the transformation.

Space. World -- (x, y, and Z axes are displayed when an object is selected in the scenario view) moves relative to the World coordinate system. Iii. Example

1. Run Unity and open the ch06Demos project.

2. Add a subfolder named 6.4 under Assets and create a scenario named Scene6_4.unity under the subfolder:

3. Double-click to open the scenario.

4. Add a Cylinder to the scenario and change its Y axis to 5:

5. Add a lifangt (Cube) to the scenario, and change the zooming coefficient of X, Y, and Z to 2:

6. Adjust the camera position to make the cylinder and Cube show a proper size:

7. Create a C # script named Demo4_1.cs In the 6.4 Folder:

8. Double-click Demo4_1 to automatically start VS2015. In VS2015, change Demo4_1.cs to the following code and save it:

Using UnityEngine; using System. collections; public class Demo4_1: MonoBehaviour {public GameObject cube; public GameObject cylinder; void OnGUI () {if (GUILayout. button ("Move Cube to left") {cube. transform. translate (new Vector3 (-0.5f, 0f, 0f);} if (GUILayout. button ("Move Cube to the right") {cube. transform. position = cube. transform. position + new Vector3 (0.5f, 0f, 0f);} if (GUILayout. button ("zoom in Cube") {cube. transform. localScale * = 1.2f;} if (GUILayout. button ("zoom in Cube") {cube. transform. localScale * = 0.8f;} if (GUILayout. button ("rotate Cube") {cube. transform. rotate (new Vector3 (0, 10, 0);} if (GUILayout. button ("rotate Cube around the cylindrical") {cube. transform. rotateAround (cylinder. transform. position, Vector3.up, 10 );}}}

9. Switch to Unity, add an empty GameObject to the scenario, and drag and drop the script to the viewer view, drag the Cube and Cylinder to the corresponding attributes of the script respectively (assign the initial values ):

10. Click play to see the following preview:

Click different buttons or the same button multiple times to observe the changes respectively.

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.