[Unity learning] Getting started with Unity and the first game

[Unity learning] Getting started with Unity and the first game

A small game made by following the tutorial, basically, even if I have zero foundation like me, it can be realized in two hours.

The main thing is to get familiar with C# and Unity and make something that can run.

A simple demo is as follows:

Unity Learning Log 01 - Delivery Driver

Unity related

Unity theoretical knowledge

Unity implements component development, and everything is conceptually a component. The functions/components learned in this tutorial are:

  • Sprite

    Sprite is an object that "works" in Unity. The new method is as follows:

    crete-sprite

    Right-click on a blank page on the Hierarchy and select 2D Object > SpritesNew to create a new Sprite.

  • Main Camera

    where the point of view is.

  • Assets

    Where resources are stored, local resources are:

    assets

    assets2

    The cars, roads, and scenes on the interface are all pulled out from Assets, and then rotated, zoomed out, and zoomed in. The complete scene is as follows:

    After each resource is dragged onto the page, it becomes an independent component.

  • Components

    • Collider 2D

      This property adds collidability to the component. For example, in the video, when it hits a house or a tree, it will collide, but it will not pass through.

    • Rigid Body

      My understanding is to add "mass" to objects. If the Rigid Bodyproperty , then the car hits the house or tree and both the house/tree and the car pop out.

      My understanding is that if the component is not added Rigid Body, the component is equivalent to an object with a large mass. For example, if a person pushes a house, the house will not move, but if a person pushes a person, both people will move.

    • C# script

      C# scripts in Unity are also components that can be mounted on Sprites for execution.

Common Unity shortcut keys in this issue

  • q

    The cursor will look like a hand, at this time you can drag and drop the page to relocate

    Pressing the altkey also produces the same effect

  • w

    When the Sprite is selected, press the wkey to move the Sprite

  • e

    Rotate the Sprite, which can be rotated in three dimensions

  • r

    Change the Sprite Scale

  • ctrl + d

    Copy Sprite

C# related

The actual C# script to be written in this project is less than a hundred lines, and the rest are generated by Unity itself after using the drag-and-drop function on Unity.

Structure of a C# class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClassName : MonoBehaviour
{
    private void Start()
    {
    }

    private void Update()
    {
    }
}

This is basically the template given when creating a script in Unity.

MonoBehaviourIt is the base class provided by Unity, and all Unity scripts need to inherit.

original:

MonoBehaviouris the base class from which every Unity script derives.

Startis the script that fires when the game starts.

UpdateThe code inside the method is called every frame and recalculated.

full script

All scripts used in the project.

Delivery.cs

This script is mounted Caron , and the main control content is picking up and delivering items, and when the car picks up the item, the vehicle will change color, and dropping the item is the vehicle color restoration.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Delivery : MonoBehaviour
{
    bool hasPackage = false;

    // SerializeField 使得该变量可以在Unity引擎中直接被看到和修改
    [SerializeField] Color32 hasPackageColor = new Color32(255, 0, 0, 255);
    [SerializeField] Color32 noPackageColor = new Color32(255, 255, 255, 255);

    [SerializeField] float destroyDelay = 0.5f;

    SpriteRenderer spriteRenderer;

    private void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        Debug.Log("collided");
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        // if (the thing we triggered is package)
        if (other.tag == "Package" && !this.hasPackage)
        {
            Debug.Log("Package hitted");
            hasPackage = true;
            Destroy(other.gameObject, destroyDelay);
            spriteRenderer.color = hasPackageColor;
        }
        else if (other.tag == "Customer" && this.hasPackage)
        {
            Debug.Log("Customer Delivered");
            hasPackage = false;
            spriteRenderer.color = noPackageColor;
        }
    }
}

Driver.cs

This script is mounted Caron and controls the acceleration and deceleration of the vehicle.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Driver : MonoBehaviour
{
    [SerializeField] float steerSpeed = 300f;
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float slowSpeed = 5f;
    [SerializeField] float fastSpeed = 20f;

    // Update is called once per frame
    void Update()
    {
        float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;
        float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        transform.Rotate(0, 0, -steerAmount);
        transform.Translate(0, moveAmount, 0);

    }

    private void OnCollisionEnter2D(Collision2D other) {
        moveSpeed = slowSpeed;
    }

    private void OnTriggerEnter2D(Collider2D other) {
        if (other.tag == "Boost") {
            moveSpeed = fastSpeed;
        }
    }
}

FollowCamera.cs

This script is mounted Main Cameraon , so that the camera can follow the vehicle.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowCamera : MonoBehaviour
{
    [SerializeField] GameObject car;

    // camera's position should be car's position
    void Update()
    {
        transform.position = car.transform.position + new Vector3(0, 0, -10);
    }
}

Guess you like

Origin blog.csdn.net/weixin_42938619/article/details/120819292