image1 image2 image3

HELLO I'M VIET|WELCOME TO MY PERSONAL BLOG|I LOVE TO DO CREATIVE THINGS

Unity bài 2: Biến, Hàm và Điều kiện


Nội dung bài viết
  1. Các biến (variables)
  2. Ramdom số trong khoảng
  3. Hàm Mathf
  4. Biểu thức điều kiện (Conditions)

I. Các biến (Variables)

  • Khai báo biến trong C#
<kiểu dữ liệu> <tên biến> = <giá trị>
  • Các kiểu dữ liệu
    string nameC = "NameCharacter";
    float speed = 2.5f;
    double speed2 = 2.5;
    bool isDead = true;

II. Random

  • Để random 1 số ngẫu nhiên trong khoảng 1-100 ta dùng Random.Range()
  void Start()
    {
        int ran = Random.Range(1100);
        Debug.Log(ran);
    }

III. Mathf

  • Mathf.PI
int ran = Random.Range(1100);
float sum = ran + Mathf.PI;
Debug.Log(sum);
  • Mathf.Abs, Mathf.Acos,...

IV. Điều kiện (Conditions)

  • Boolean
        bool sky = false;
        if(sky) {
            Debug.Log("I will go out and play");
        }
        else {
            Debug.Log("I'm staying in bed");
        }
  • Switch-case
            int ran = Random.Range(13);
            switch (ran) {
                case 1print("You rolled a 1");
                break;
                case 2print("You rolled a 2");
                break;
                case 3print("You rolled a 3");
                break;
            }  

Share this:

CONVERSATION

0 nhận xét:

Đăng nhận xét

Thank you!