Detailed explanation of core classes and methods commonly used by Unity3D programmers

Unity3D is a powerful game engine that is widely used in the field of game development. As a Unity3D programmer, it is very important to master commonly used core classes and methods. This article will introduce in detail the core classes and methods commonly used by programmers in Unity3D, and give code implementations.

Yes, there is a . I hope everyone can click in to exchange development experiences together!

Commonly used methods:

  • Translate(Vector3 translation): Translate the game object.
  • Rotate(Vector3 eulerAngles): Rotates the game object around the axis of its own coordinate system.
  • RotateAround(Vector3 point, Vector3 axis, float angle): Rotate the game object around the specified point and axis.
  • Scale(Vector3 scale): Scale the game object.

Code example:

Transform playerTransform = playerObject.transform;
playerTransform.Translate(Vector3.forward * Time.deltaTime);  // 前进
playerTransform.Rotate(Vector3.up * Time.deltaTime);  // 旋转
playerTransform.Scale(Vector3.one * Time.deltaTime);  // 缩放

GameObject Class
The GameObject class is used to represent game objects and is one of the most basic classes in Unity3D. Through the GameObject class, programmers can create, destroy and find game objects and other operations.

Commonly used methods:

Code example:

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);  // 创建立方体
GameObject clone = Instantiate(cube);  // 复制立方体
Destroy(cube);  // 销毁立方体
GameObject player = GameObject.Find("Player");  // 查找名为"Player"的游戏对象

Rigidbody class
The Rigidbody class is used to add physical properties to game objects, such as gravity, collision, etc. Through the Rigidbody class, programmers can implement physical simulation of game objects.

Commonly used methods:

Code example:

Rigidbody rb = playerObject.GetComponent<Rigidbody>();
rb.AddForce(Vector3.forward * 10);  // 向前施加力
rb.AddTorque(Vector3.up * 5);  // 绕y轴施加扭矩
rb.MovePosition(Vector3.zero);  // 移动到原点
rb.MoveRotation(Quaternion.Euler(0, 90, 0));  // 旋转到指定角度

Input class
The Input class is used to obtain user input, such as keyboard, mouse and touch. Through the Input class, programmers can implement the interactive functions of the game.

Commonly used methods:

Code example:

if (Input.GetKey(KeyCode.Space))  // 按下空格键
{
    // 执行操作
}

if (Input.GetMouseButton(0))  // 按下鼠标左键
{
    // 执行操作
}

Collider class
The Collider class is used to represent the collider of game objects and is used to detect collisions between game objects. Through the Collider class, programmers can implement collision detection and response in the game.

Commonly used methods:

Code example:

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("Player"))  // 碰撞到名为"Player"的游戏对象
    {
        // 执行操作
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Enemy"))  // 进入碰撞器的游戏对象是名为"Enemy"的游戏对象
    {
        // 执行操作
    }
}

The above is a detailed explanation and code implementation of the core classes and methods commonly used by Unity3D programmers. Mastering these core classes and methods can help programmers better develop Unity3D games. Of course, Unity3D has more powerful classes and methods waiting to be explored and learned.

Guess you like

Origin blog.csdn.net/voidinit/article/details/133901107