Unity学习笔记[一] RollBall小游戏

目录

一、适配vs

二、初识Unity

2.1 unity核心模块

2.2 Unity基本操作和场景操作

2.3 世界坐标系和局部坐标系

2.4 工具栏 QWER

三、基础知识

3.1 基本组件

3.2 刚体组件

3.2.1 获取刚体组件

3.2.2 给刚体施加力

3.3 三维向量Vector3

3.4 通过按键控制左右运动

3.5 控制相机位置和跟随

3.6 物体旋转

3.7 碰撞检测

3.8 触发检测

四、RollBall游戏开发案例


一、适配vs

Edit -> Preference -> External Tools、

二、初识Unity

2.1 unity核心模块

(1)Project:工程面板,存放工程的各种资源。声音、模型、场景、材质等。

(2)Hierarchy: 层级面板,战士当前打开的场景里面有那些东西(游戏物体)。

(3)Inspector:检视面板(属性面板),查看一个游戏物体由哪些组件组成。

因此,场景 = 多个游戏物体   多个游戏物体包含多个组件

(4)Scene:场景面板,显示当前场景的样子

2.2 Unity基本操作和场景操作

1、如何创建基本模型和如何导入复杂模型

2、场景基本操作 聚焦:双击游戏物体 或者 F 放大缩小视野:鼠标滚轮 围绕物体旋转:Alt+鼠标左键 使用MoveTool下 移动物体

3、视野分类 Persp 透视视野 ISO平行视野 在不同视野下:关于鼠标右键的不同

4、保存(场景保存、代码保存) Ctrl + S

2.3 世界坐标系和局部坐标系

(1)坐标系:x 左右 y 上下 z 前后

(2)局部坐标系  : 父物体与子物体

(3)单位: Unity坐标以米为单位

2.4 工具栏 QWER

三、基础知识

3.1 基本组件

Transform:变换组件,位置、旋转、缩放。

Mesh Filter:网格

Meth Render:网格渲染(这个组件会使用材质进行渲染)

Collider:碰撞检测

3.2 刚体组件

3.2.1 获取刚体组件

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

3.2.2 给刚体施加力

rd.AddForce(Vector3.forward);

3.3 三维向量Vector3

三维向量 (x,y,z) Vector3.forward 等于 (0,0,1)

一些常用的向量 Vector3.right Vector3.left Vector3.forward Vector3.back Vector3.up Vector3.down

创建向量 new Vector3(x,y,z)

3.4 通过按键控制左右运动

1. 如何设置(Project Setting -》Input Manager)

2.左右键/AD

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

 3.上下键/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 控制相机位置和跟随

步骤:

1、得到Player的Transform

2、计算位置偏移

3、根据位置偏移设置相机的位置

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 物体旋转

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

3.7 碰撞检测

碰撞事件  OnCollisionEnter OnCollisionExit  OnCollisionStay

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

3.8 触发检测

触发事件 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); //物体可见
        }
    }

四、RollBall游戏开发案例

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);
        }
    }
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>