Unity study notes [1] RollBall game

Table of contents

1. Adaptation vs.

2. Getting to know Unity for the first time

2.1 Unity core module

2.2 Unity basic operation and scene operation

2.3 World coordinate system and local coordinate system

2.4 Toolbar QWER

3. Basic knowledge

3.1 Basic components

3.2 Rigid body components

3.2.1 Get rigid body components

3.2.2 Applying force to a rigid body

3.3 Three-dimensional vector Vector3

3.4 Control left and right movement by buttons

3.5 Controlling camera position and following

3.6 Object Rotation

3.7 Collision detection

3.8 Trigger detection

4. RollBall game development case


1. Adaptation vs.

Edit -> Preference -> External Tools、

2. Getting to know Unity for the first time

2.1 Unity core module

(1) Project : Project panel, which stores various resources of the project. Sounds, models, scenes, materials, etc.

(2) Hierarchy : Hierarchy panel, what things (game objects) are in the scene currently opened by the fighter.

(3) Inspector : Inspector panel (property panel), check which components a game object is composed of.

So, Scene = Multiple GameObjects Multiple GameObjects Contains Multiple Components

(4) Scene : Scene panel, showing the current scene

2.2 Unity basic operation and scene operation

1. How to create a basic model and how to import a complex model

2. Basic operation of the scene Focus: double-click the game object or F to zoom in and out: the mouse wheel rotates around the object: Alt + left mouse button to use MoveTool to move the object

3. Field of view classification Persp Perspective field of view ISO Parallel field of view Under different fields of view: About the difference of the right mouse button

4. Save (scene save, code save) Ctrl + S

2.3 World coordinate system and local coordinate system

(1) Coordinate system : x left and right y up and down z front and back

(2) Local coordinate system   : parent object and child object

(3) Unit : Unity coordinates are in meters

2.4 Toolbar QWER

3. Basic knowledge

3.1 Basic components

Transform: Transform components, position, rotation, scaling.

Mesh Filter: Mesh

Meth Render: grid rendering (this component will use material for rendering)

Collider: collision detection

3.2 Rigid body components

3.2.1 Get rigid body components

private Rigidbody rd;
rd = GetComponent<Rigidbody>();

3.2.2 Applying force to a rigid body

rd.AddForce(Vector3.forward);

3.3 Three-dimensional vector Vector3

Three-dimensional vector (x,y,z) Vector3.forward is equal to (0,0,1)

Some commonly used vectors Vector3.right Vector3.left Vector3.forward Vector3.back Vector3.up Vector3.down

Create vector new Vector3(x,y,z)

3.4 Control left and right movement by buttons

1. How to set (Project Setting -> Input Manager)

2. Left and right keys/AD

float h = Input.GetAxis("Horizontal");

 3. Up and down keys/WS

float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(h,0, v));

3.5 Controlling camera position and following

step:

1. Get the Player's Transform

2. Calculate the position offset

3. Set the position of the camera according to the position offset

public Transform playerTransform;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - playerTransform.position;
}
// Update is called once per frame
void Update()
{
transform.position = playerTransform.position + offset;
}

3.6 Object Rotation

transform.Rotate(Vector3.up,Space.World);

3.7 Collision detection

Collision event   OnCollisionEnter OnCollisionExit OnCollisionStay

private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag  == "Food")
        {
            Destroy(collision.gameObject);
        }
    }

3.8 Trigger detection

Trigger events  OnTriggerEnter OnTriggerStay OnTriggerExit


    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Food")
        {
            Destroy(other.gameObject);
            score++;
            scoreText.text = "分数: " + score;
        }
        if(score >= 11)
        {
            winText.SetActive(true); //物体可见
        }
    }

4. RollBall game development case

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

public class Player : MonoBehaviour
{
    public Rigidbody rd;
    public int score = 0;
    public Text scoreText;
    public GameObject winText;
    
    // Start is called before the first frame update
    void Start()
    {
        //  Debug.Log("游戏开始了");
        rd = GetComponent<Rigidbody>();
      
    }

    // Update is called once per frame
    void Update()
    {
        // Debug.Log("游戏正在执行");
        float fh = Input.GetAxis("Horizontal");
        float fw = Input.GetAxis("Vertical");
        //   rd.AddForce(new Vector3(fh, 0, fw));

        rd.AddForce(new Vector3(fh,0,fw));

        //rd.AddForce(Vector3.up);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag  == "Food")
        {
            Destroy(collision.gameObject);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Food")
        {
            Destroy(other.gameObject);
            score++;
            scoreText.text = "分数: " + score;
        }
        if(score >= 11)
        {
            winText.SetActive(true);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_40582034/article/details/127944713