GameObject of unity introduction

  • Input and inputmanager
  • MonoBehaviour
  • summary

GameObject and component

GameObject is an object that exists in the game scene and has a location

Component is attached to GameObject and controls various properties of GameObject

GameObject is composed of components. The life cycle of components is closely related to GameObject. When you call the destroy method of this GameObject, its child objects and all corresponding components will be destroyed, but you can also destroy only one component at a time

Common components:

Component effect
Rigidbody rigid body Enable an object to move under physical control
Collider Collider The rigid body and rigidbody collide together. Without Collider, the two colliding rigid bodies will penetrate each other
Renderer renderer Make objects appear on the screen
Audiosource audio source Make the object play audio in the scence scene
Animation animation
Animator animation controller

At the same time, all scripts are components, so they can be attached to the game object

Common components can be obtained through simple member variables

Components or scripts attached to game objects can be obtained through getcomponent

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Awake() {
        transform.Translate(0, 1, 0);
        GetComponent<Transform>().Translate(0, 1, 0);

Input and inputmanager

In inputmanager, you can create virtual axes and buttons, and end users can configure keyboard input in the screen configuration dialog box.

If you want to add a new virtual axis, select Edit – > project settings – > Input menu. The setting of each axis can be changed here. You can enter the configuration interface of input manager.

In the script, all virtual axes are accessed by their names

After each project is created, there are the following default input axes:

  • Horizontal and Vertical are mapped to w, a, s, d and the arrow keys. Horizontal and vertical are mapped to W, a, s, D keys and direction keys
  • Fire1, Fire2, Fire3 are mapped to Control, Option (Alt), and Command, respectively. Fire1, fire2, and fire3 are mapped to Ctrl, option (ALT), and command keys, respectively
  • Mouse X and Mouse Y are mapped to the delta of mouse movement. Mouse X and mouse Y are mapped to mouse movement increments
  • Window Shake X and Window Shake Y is mapped to the movement of the window. Window shake X and window shake y are mapped to the movement of the window


Time class is a global variable in unity, which records the time, frame number and other data related to the game

The time class contains a very important variable called deltatime This variable contains the time since the last call to update or fixedupdate (depending on whether you put it in the update function or fixedupdate function) (update is called once per frame)

Example: make the object rotate at a uniform speed, independent of the frame rate

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Update() {
        transform.Rotate(0, 5 * Time.deltaTime, 0);

Physics and transform

Physics class is a tool function class, which mainly provides two ray projection methods: linecast and raycast.

  • Linecast takes the start position and end position of projection as parameters
  • Raycast takes the starting position and direction of projection as parameters

To determine whether the projection collided with a collider.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void Update() {
    //Using raycast
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        if (Physics.Raycast(transform.position, fwd, 10))
            print("There is something in front of the object!");
        //Using linecast
    Transform target;
    if (!Physics.Linecast(transform.position, target.position))

The physics module contains three most important components: rigidbody, collision and joint

  • Rgidbody exists as a force object, so it can apply force and drag to a rigidbody. At the same time, rigidbody also has velocity, mass, position, rotation and other attributes
  • Collider is a class that appears to handle collision events in physics. Without Collider, two rigidbodies cannot collide. The same GameObject can bind multiple colliders to build a more complex collider structure. Collider can also set material, that is, the physical material of collider. Lets you adjust the bounce effect between friction and collision units. (in case of collision, the destruction functions oncollisionenter, oncollisionstay, oncollisionexit, etc. will be triggered
  • Joint is used to connect two rigidbodies. When the joint is broken, the callback function of onjointbreak will be triggered.


GameObject is an object that exists in the game scene and has a location

Controlling GameObject requires script components

Monobehavior is the base class for all scripts in unity

MonoBehaviour is the base class from which every Unity script derives.

Monobehavior life cycle

It is often necessary to detect the distance between the enemy and us in the game. At this time, if you want to find all the enemies, it obviously consumes too much computation, so the best way is to use Collider to represent the attack range, and then set collider’s istrigger to true. Finally, ontriggerenter is used to detect the distance within the attack range, which will greatly improve the program performance.

Basic structure of script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;  // 3 reference namespace
public class player : MonoBehaviour {
       // Use this for initialization
       void Start () {
        GameData data;  // 4 can be used
       // Update is called once per frame
       void Update () {
Namespace mygame {// 1 defines the namespace
    Class GameData {// 2 belongs to the class under mygame


Time, input and physics are global variables in unity

GameObject is a basic object in the game and is composed of components. GameObject itself must have a transform component

GameObject is an object that exists in the game scene and has a location

The above is the detailed content of GameObject for unity introduction. For more information about GameObject for unity introduction, please pay attention to other relevant articles of developeppaer!