Basic Unity3D Tutorial: Unity tutorial (1): GameObject, Compoent, Time, Input, Physics

Source: Internet
Author: User

Author: Wang Xuan Yi, source: http://www.cnblogs.com/neverdie/Reprinted, Please keep this statement. If you like this article, clickRecommendation. Thank you! Unity3D class diagram of important modules

Recently completed a small project I personally satisfied: [Deep Cocos2d-x] using MVC Architecture to build the game Four, in this game, I used my own MVC Architecture to create a game and achieved a good SoC (separation of Focus ). But suffering from Cocos2d-x doesn't have a perfect editor, so I started to learn another very popular game engine-Unity3D.

Unity3D is a Component-Based game engine that provides many GamePlay Layer Support for GamePlay Progrmmer. For example, you can design an animation state transition animation or on the graphic interface. For example, you can directly adjust the Collider in the scenario editor. For example, the Animation curve Animation can be dynamically adjusted. In general, Unity is a game engine with a much more sophisticated architecture than Cocos2d-x.

Unfortunately, Unity itself is not open-source. Fortunately, Unity provides detailed documentation support when it is not open-source. At the same time, the Unity community is also very friendly, and stackoverflow also has many issues worth looking.

By the way, we recommend several Unity learning websites:

Unity holy book

Official Unity documentation

Unity's topic

 

When I learned about Unity3D, I found that the important analogy within Unity is too much Cocos2d-x, a little bit unclear, so I made the following design class diagram to provide a preliminary impression of Unity for beginners like me.

 

GameObject and Component

Since Unity is a Component-Based game engine, all objects in the game are a GameObject. To attach various attributes to this GameObject, we introduced the Component concept.

GameObject is composed of Component. The Life Cycle of Component is closely related to that of GameObject. Once the Destroy method of GameObject is used, its sub-objects and all corresponding Component are destroyed. At the same time, we can only Destroy one independent Component at a time.

Component has the following types. I made a table to record their usage:

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

For more information about Input, see this article: Input

Unity support, keyboard, joystick, and game handle input.

InInput Manager)You can create virtual axes and buttons, and end users can configure keyboard input in the screen configuration dialog box.

To add a new Virtual Axis, choose Edit> Project Settings> Input menu. The setting of each axis can be changed here. You can enter the Input Manager configuration page.

Time

The Time class is a global variable in Unity. It records game-related Time, frames, and other data.

The Time class contains a very important variable called deltaTime. this variable contains the time from the last Update or FixedUpdate call to the present (depending on whether you are in the Update function or FixedUpdate function ). (Note: Update is called once every frame)

As shown in the preceding example, an object is rotated at a constant speed and does not depend on the frame rate, as shown below:

using UnityEngine;using System.Collections;public class example : MonoBehaviour {    void Update() {        transform.Rotate(0, 5 * Time.deltaTime, 0);    }}
Of course, when using the Time class, we should also remember to use a variety of Lerp functions to reduce our workload. In Unity3D, Vector3, Vector2, color and other classes provide corresponding Lerp functions for us to call.
 
Physics and Transform

Physics class is a tool function class of Unity. It mainly provides two ray projection methods: Linecast and Raycast.

Among them, Linecast is the starting position of projection andTermination locationIs a parameter to determine whether the projection has collided with a Collider.

In contrast, Raycast sets andProjection DirectionIs a parameter to determine whether the projection has collided with a Collider.

You can view the following program for the corresponding instance:

Using UnityEngine; using System. collections; public class Example: MonoBehaviour {void Update () {// use Raycast Vector3 fwd = transform. transformDirection (Vector3.forward); if (Physics. raycast (transform. position, fwd, 10) print ("There is something in front of the object! "); // Use Linecast Transform target; if (! Physics. Linecast (transform. position, target. position) ProcessData. AndDoSomeCalculations ();}}

In Physics, three components are crucial, namely RigidBody, Collision, and Joint. In the new version, RigidBody2D, Collision2D, and Joint2D components are introduced to process Physics events in 2D.

These three classes are used to handle physical-related events. What are their differences?

RgidBody is a Force object, so Force and Drag can be applied to a RigidBody ). At the same time, RigidBody also has velocity (speed), mass (quality), position (position), rotation (rotation) and so on.

Collider is a class used to handle physical collision events. As mentioned in the above table, if there is no Collider, there will be no collision between two rigidbodies. You can bind multiple Collider pairs to the same GameObject to build a more complex collision body structure. Another noteworthy thing about Collider is that we can set material for Collider, that is, the physical material of Collider. The physical material is used to adjust the rebound effect between the friction and the collision unit.

When a collision occurs, the destroy function OnCollisionEnter, OnCollisionStay, OnCollisionExit, and so on will be triggered. The differences between these functions and OnTriggerXXX will be mentioned in the following blog.

Joint is used to connect two rigidbodies. When Joint is disconnected, the OnJointBreak callback function is triggered.

Summary

I wanted to describe the usage of various types of class images, but I only wrote these classes today, so please look forward to my next blog.

Related Article

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.