Transform script controls the transformation of Game objects
A transform (Transform) component is attached to a game object during creation and cannot be deleted.
Objective: To dynamically modify the value of the model transformation in the code
Conclusion: obj. Transform. Position references to obtain the position of the OBJ game object in the 3D coordinate system. You can directly modify the position container value to modify the model position.
Change the position of a game object
In the 3D world, the 3D coordinates of any model are kept in the vector3 container, and the container will record the coordinates of the model in the X, Y, and Z axes.
Private float value_x = 0.0f;
Private float value_y = 0.0f;
Private float value_z = 0.0f;
Private gameobject OBJ; // cube object
// Use this for initialization
Void start ()
{
OBJ = gameobject. Find ("cube ");
}
Void ongui (){
Guilayout. Box ("moving cube X axis ");
Value_x = guilayout. horizontalslider (value_x,-10.0f, 10.0f, guilayout. Width (200 ));
Guilayout. Box ("moving cube Y axis ");
Value_y = guilayout. horizontalslider (value_y,-10.0f, 10.0f, guilayout. Width (200 ));
Guilayout. Box ("moving cube Z axis ");
Value_z = guilayout. horizontalslider (value_z,-10.0f, 10.0f, guilayout. Width (200 ));
// Set the cube position
OBJ. Transform. Position = new vector3 (value_x, value_y, value_z );
Guilayout. Label ("current position of the cube" + obj. Transform. position );
}
Rotate the game object rotate
There are two rotation methods:
The first type is self-rotation, which is rotated along the X, Y, and Z axes.
The second is to rotate around a point in the coordinate system or a game object.
Transform. Rotate (); used to set the model to rotate around itself. The parameter is speed and direction.
Transform. rotatearound (); used to set the model to rotate around a certain point
Time. deletatime: used to record the time consumed by the previous frame, which is used as the speed coefficient of model rotation.
Vector3.right X axis
Vector3.up Y axis
Vector3.forward Z axis direction
Private gameobject objcube;
Private gameobject objcylinder; // a cylindrical object
Private int speed = 500;
// Use this for initialization
Void start (){
Objcube = gameobject. Find ("cube ");
Objcylinder = gameobject. Find ("Cylinder ");
}
Void 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 ("the cube rotates along the Y axis", guilayout. Height (50 )))
{
Objcube. Transform. Rotate (vector3.up * time. deltatime * speed );
}
If (guilayout. Button ("the cube rotates along the Z axis", guilayout. Height (50 )))
{
Objcube. Transform. Rotate (vector3.forward * time. deltatime * speed );
}
If (guilayout. Button ("the cube rotates along the cylinder", guilayout. Height (50 )))
{
Objcube. Transform. rotatearound (objcylinder. Transform. Position, vector3.up, time. deltatime * speed );
}
// Cube Rotation Angle
Guilayout. Label ("cube Rotation Angle" + objcube. Transform. Rotation );
}
Translation object game object translate ()
Translation Means Translation Based on the original position. The unique parameter is the direction of the translation model.
Private gameobject objcube;
// Use this for initialization
Void start ()
{
Objcube = gameobject. Find ("cube ");
}
Void ongui (){
If (guilayout. Button ("moving forward", guilayout. Height (50 ))){
Objcube. Transform. Translate (vector3.forward * time. deltatime * 20 );
}
If (guilayout. Button ("move backward", guilayout. Height (50 )))
{
Objcube. Transform. Translate (-vector3.fwd * time. deltatime * 20 );
}
If (guilayout. Button ("move to left", guilayout. Height (50 )))
{
Objcube. Transform. Translate (vector3.left * time. deltatime * 20 );
}
If (guilayout. Button ("Move right", guilayout. Height (50 )))
{
Objcube. Transform. Translate (vector3.right * time. deltatime * 20 );
}
// Cube Rotation Angle
Guilayout. Label ("cube position" + objcube. Transform. position );
}
Scale the game object localscale ()
Private float scale_x = 0.0f;
Private float scale_y = 0.0f;
Private float scale_z = 0.0f;
Private gameobject OBJ; // cube object
// Use this for initialization
Void start ()
{
OBJ = gameobject. Find ("cube ");
}
Void ongui (){
Guilayout. Label ("X axis scaling ");
Scale_x = guilayout. horizontalslider (scale_x, 1.0f, 2.0f, guilayout. Width (100 ));
Guilayout. Label ("Y axis scaling ");
Scale_y = guilayout. horizontalslider (scale_y, 1.0f, 2.0f, guilayout. Width (100 ));
Guilayout. Label ("Z axis scaling ");
Scale_z = guilayout. horizontalslider (scale_z, 1.0f, 2.0f, guilayout. Width (100 ));
// Recalculate the scaling ratio
OBJ. Transform. localscale = new vector3 (scale_x, scale_y, scale_z );
}
Each axis has its own scaling coefficient. The default scaling coefficient of the model is 1, which is the original size of the model.
Object transform (change attribute), Rigidbody (rigid body attribute)
VaR gameobject: gameobject; // declares a game object
Function Update (){
Gameobject. Transform. Rotate (20, 0, 0); // Rotate 20 ° around the X axis
Gameobject. Transform. Translate (); // one unit length to move forward each frame
}
Valueto () in itween is also the value method.
Valueto: returns an interpolation between "from" and "to" to call the "onupdate" Callback Function and onupdate callback, it receives a parameter of the same type as "from" and "".
Supported value types: float, double, vector3, vector2, color, rect
Method of calling value in update (): floatupdate, vector3update, vector2update, rectupdate
These methods return an interpolation between the provided two values, with the root of the Size Variation providing the speed.
Tip1:
Gameobject. Transform. Rotate (vector3.forward *-100); // The rotation angle instead of the Target angle.
Rotation of Odo... gameobject