Unity3d Introductory Article--transform class

Source: Internet
Author: User
Tags translate function

Transform component is one of the key points of Unity3d, which is mainly used to control the rotation, movement and scaling of objects. Then we'll take a detailed study of the member variables and member functions that are contained in the transform class.

One, member variable

Position: Position in the world space coordinate transform.
Localposition: The position of the transformation relative to the parent. If the transformation has no parent, it is equivalent to transform.position.
Eulerangles: Rotation in the World coordinate system (Euler angle).
Localeulerangles: The rotation angle of the transformation relative to the parent.
Right: The right-hand direction in the world coordinate system. (The red axis of the world Space Coordinate transformation.) Which is the X axis. )
Up: The top direction in the world coordinate system. (The green axis of transformation in the world space coordinates.) Which is the Y axis. )
Forward: The front direction in the world coordinate system. (The blue axis of the transformation in the world space coordinates.) Which is the z axis. )
Rotation: Rotation in the world coordinate system (four yuan number).
Localrotation: The rotation angle of the transformation relative to the parent.
Localscale: The scaling ratio relative to the parent.
Parent: Transform component.
Worldtolocalmatrix: The point of matrix transformation shifts from world coordinates to its own coordinates (read only).
Localtoworldmatrix: The point of the matrix transformation shifts from its own coordinates to world coordinates (read-only).
Root: The transform component of the root object in an object-level relationship.
ChildCount: Number of child objects.
Lossyscale: Global Scaling (read only).

second, the member function:

1. LookAt function

public void LookAt (Transform target) public
void LookAt (Vector3 worldposition);
public void LookAt (Vector3 worldposition, Vector3 worldup = vector3.up);
public void LookAt (Transform target, Vector3 worldup = vector3.up);

Rotate the object so that the z-axis of the object points to the target/worldposition, and for Worldup it is described as continuing to rotate itself after the above rotation so that the positive y-axis of the current object is aligned to the direction that the worldup points to.

The alignment here is that the new rotated Y axis is consistent with the projection vector of the worldup on the XY plane after the object's first rotation. The reason for the projection is that the first rotation causes the object's z-axis to point to the Target/worldposition, at which point the worldup vector may not be on the XY plane, and the Y axis is aligned to the Worldup if the z-axis points to the Target/worldposition , you can only take a projection of the worldup on the XY plane.

Note: The use of worldposition vector should pay attention to the direction, must be target-transform.position, the order of the reverse will make the object back to the target, if you use transform as a parameter, you do not have to pay attention to. By default, Worldup is Vector3.up (Y axis in the World coordinate system) 2, rotate function

public void Rotate (Vector3 eulerangles);
public void Rotate (Vector3 eulerangles, space relativeto = space.self);
public void Rotate (float xangle, float yangle, float zangle);
public void Rotate (float xangle, float yangle, float zangle, space relativeto = space.self);

Rotates a Euler angle, which rotates in the order of Zxy, by default space.self in the local coordinate system.

public void Rotate (Vector3 axis, float angle);            
public void Rotate (Vector3 axis, float angle,space relativeto = space.self);

Rotates the angle angle around the axis axis and space.self in the local coordinate system by default.

There is a difference between transform.rotation and rotate:

The Rotate () method is: how many degrees to rotate. On the basis of the original accumulation, that is, the number of rotation angle. And how many angles are rotated, on the basis of the original

The Rotation property is: Rotate to an angle, which is performed every frame in the update. But each rotation to the angle is 5, so it is rotated to 5 degrees. It's always been.

For example, you just want to let him rotate to how much, with rotation; If you want him to keep turning, you can use rotate.

Rotation directly changes the value to achieve the rotation effect

Rotate applies a rotational angle of 1 degrees per second, slowly rotating the object.

Of course: rotation () can also be interpolated by rotating 3, transformdirection function

Public Vector3 transformdirection (Vector3 direction);
Public Vector3 transformdirection (float x, float y, float z);

From its own coordinates to the World coordinate transformation direction (this emphasis is the direction) This operation is not affected by the zoom and position of the transformation. The returned vector has the same length as the direction

4. Transformpoint function

Public Vector3 Transformpoint (Vector3 position);
Public Vector3 Transformpoint (float x, float y, float z);

Transforms the position from its own coordinates to the world coordinates. Note that the return position is affected by scaling 5, transformvector function

Public Vector3 transformvector (Vector3 vector);
Public Vector3 Transformvector (float x, float y, float z);

Transforms a vector from the local coordinate space to the world coordinate space. This operation is not affected by the transformation position, but is affected by scaling 6, translate function

public void Translate (Vector3 translation);
public void Translate (Vector3 translation, space relativeto = space.self);

Moves the distance of the translation vector length along the translation direction, if RelativeTo is left blank or set to Space.self, the move is applied relative to its own coordinate system

public void Translate (float x, float y, float z);
public void Translate (float x, float y, float z, space relativeto = space.self);

Move the transform by x along the x axis, y along the Y-axis, z along the z-axis. If the relativeto is left blank or set to Space.self, the move is applied relative to its own coordinate system

public void Translate (Vector3 translation, Transform relativeto);   
public void Translate (float x, float y, float z, Transform relativeto);

The first argument is the same as the previous one, with emphasis on the mobile relativeto, which is interpreted as a coordinate system of the application relative to (Relativeto:transform). When daylight is relative to null, the move is applied to the world coordinate system

Example:
void Update ()
{
//Transform the object to the right of 1 units per second of the camera
. Translate (Vector3.right * time.deltatime, Camera.main.transform);
}

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.