[Unity 3D] study notes 26: Unity game script (6)

Source: Internet
Author: User

In the 3D game world, any game object is created with a transform component, which cannot be deleted or deleted. In unity, the transform panel has three attributes:

Position)

Rotation)

Scale)

These three values are used to adjust the location, status, and other parameters of the game object on the game interface.


Position)

The 3D coordinates of any game object are stored in the vector3 container, which records the coordinates of the object on the X, Y, and Z axes. Once the coordinates in the vector33 container change, the location of the game object in the scene view also changes.

In this summary example, you can use the GUI to add three drag bars to control the coordinates of the X axis, Y axis, and Z axis respectively.

// Cube X axis position private var value_x: Float = 0.0f; // cube Y axis position private var value_y: Float = 0.0f; // cube Z axis position private var value_z: Float = 0.0f; // cube object private var OBJ: gameobject; function start () {// obtain the cube object OBJ = gameobject. find ("cube");} function ongui () {guilayout. box ("Move cube X axis"); value_x = guilayout. horizontalslider (value_x,-10.0f, 10.0f, guilayout. width (200); guilayout. box ("Move cube Y axis"); value_y = guilayout. horizontalslider (value_y,-10.0f, 10.0f, guilayout. width (200); guilayout. box ("Move cube Z axis"); value_z = guilayout. horizontalslider (value_z,-10.0f, 10.0f, guilayout. width (200); // set the position of the cube obj. transform. position = vector3 (value_x, value_y, value_z); guilayout. label ("Current cube position:" + obj. transform. position );}

Run:


After dragging each axis:


In the above Code, obj. Transform. Position = vector3 (value_x, value_y, value_z) is used to reference and obtain that the OBJ game object is heavy in 3D coordinates and stored in the vector3 container.



Rotation)

Game objects can be rotated in two ways:

It rotates along the X axis, Y axis, and Z axis.

It rotates around a coordinate or an object.


// Cube object private var objcube: gameobject; // cylindrical object private var objcylinder: gameobject; // rotation speed private var speed: Int = 500; function start () {// obtain object objcube = gameobject. find ("cube"); objcylinder = gameobject. find ("Cylinder");} function ongui () {If (guilayout. button ("the cube rotates along the X axis", guilayout. height (50) {objcube. transform. rotate (vector3.right * time. deltatime * speed);} If (guilayout. button ("cube rotating along Y axis", guilayout. height (50) {objcube. transform. rotate (vector3.up * time. deltatime * speed);} If (guilayout. button ("cube rotating along the Z axis", guilayout. height (50) {objcube. transform. rotate (vector3.forward * time. deltatime * speed);} If (guilayout. button ("the cube rotates around the cylinder", guilayout. height (50) {objcube. transform. rotatearound (objcylinder. transform. position, vector3.up, time. deltatime * speed);} guilayout. label ("cube Rotation Angle" + objcube. transform. rotation );}


Run:



Click the button:



The transform. Rotate () method is called for its own rotation. Select to call the transform. rotatearound () method for this object.




Translation game objects

It is generally used to translate game objects.Transform. Translate () method.

// Cube object var OBJ: gameobject; // translation speed var speed: Int = 1; function start () {// obtain the cube object OBJ = gameobject. find ("cube");} function Update () {} function ongui () {If (guilayout. button ("moving forward", guilayout. height (50) {obj. transform. translate (vector3.forward * time. deltatime * speed);} If (guilayout. button ("move backward", guilayout. height (50) {obj. transform. translate (-vector3.fwd * time. deltatime * speed);} If (guilayout. button ("move to left", guilayout. height (50) {obj. transform. translate (vector3.left * time. deltatime * speed);} If (guilayout. button ("Move to the right", guilayout. height (50) {obj. transform. translate (vector3.right * time. deltatime * speed);} guilayout. label ("cube position:" + obj. transform. position );}

Run:


After Translation:




Scale)

In unity, there are three main scaling methods:

Scale along the X axis

Scale along Y axis

Zooming along Z axis

Each axis has its own zoom factor. The default zoom factor of the system is 1, which is the original size. Therefore, the zoom factor of Game objects is actually dynamically modified.

The following code changes the location of Game objects:

// Cube object private var OBJ: gameobject; // initialize the scaling ratio private var scalex: Float = 1.0; private var scaley: Float = 1.0; private var scalez: Float = 1.0; function start () {// obtain the cube object OBJ = gameobject. find ("cube");} function ongui () {guilayout. box ("X axis scaling"); scalex = guilayout. horizontalslider (scalex, 1.0, 2.0, guilayout. width (100); guilayout. box ("Y-axis scaling"); scaley = guilayout. horizontalslider (scaley, 1.0, 2.0, guilayout. width (100); guilayout. box ("Z axis scaling"); scalez = guilayout. horizontalslider (scalez, 1.0, 2.0, guilayout. width (100); // recalculate the zoom ratio obj. transform. localscale = vector3 (scalex, scaley, scalez );}

Run:


After Scaling:


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.