XGundam05 发表于 2022-8-17 14:04

Unity从入门到入门

Unity是一款游戏制作软件,今天就要拿ta做游戏看看(极简易,但有很多BUG)
开始!
我的系统
版本        Windows 11 专业工作站版 Insider Preview
版本        22H2
安装日期        8/10/2022
操作系统版本        25174.1010
体验        Windows Feature Experience Pack 1000.25174.1010.01、访问官网:Unity


先点击头像-》Create a Unity ID
自己填写即可(微信在线登陆也是可以的)(账号备用)
访问:
点击:




点击下载Windows版
等待
会看到这个:


双击


点击我同意


可以更改路径或者默认


点击完成
打开软件:


点击 Sign in
走完流程以后,先跳过安装


点击蓝色按钮即可
点击设置


像这样
2、安装编辑器
点击安装编辑器按钮


(我作屎,下载预发行版)


点击安装


把上面的编辑器勾掉
下载这些包




点击安装即可(等待很长时间)


做游戏,做游戏,做游戏
(准备好坟)
1、创建游戏的Project
点击项目-》新项目


选择3D等待并按创建项目即可


创建一个Cube,并命名成“游戏地面”或者“地面”


点击Main Camera,切换到摄像机(游戏)视角,并用伸长工具伸长地面,像这样


2、搞一个玩家(球体)
创建球体:


这样点即可生成圆球,并命名为玩家
给玩家搞点颜色上去
先在这里创建一个文件夹,名字为颜色




双击颜色
新建颜色球


并命名为玩家颜色


调制颜色,像我这样:


把这个球拖到玩家那里,相当于这个与玩家绑定
这个就是绑定以后的玩家球


把玩家拖放到地面上面


给球子增加真实感觉
也就是物理有关的设置
点击玩家


在角色的属性栏点击Add Component,添加Rigid Body属性


把底下的文件那里导到Assets,创建新的文件夹,命名为脚本




点进去
新建一个脚本并命名为Player Script (玩家脚本)




打开那个脚本


这个是原来的初始代码(框架)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 玩家脚本 : MonoBehaviour
{
    // Start is called before the first frame update               //只在程序运行的第一帧运行
    void Start()
    {
      
    }

    // Update is called once per frame       //在每一帧都运行
    void Update()
    {
      
    }
}
我们先设置一个刚体(Rigidbody)以让程序成功运行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 玩家脚本 : MonoBehaviour
{
    public Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}
那个里面的 public Rigidbody rb; 就是刚体的定义
这个游戏的模式就是先让球体滚动并躲避障碍物通关,就得设置移动速度
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 玩家脚本 : MonoBehaviour
{
    public Rigidbody rb;
    public float moveSpeed = 30; //移动速度是30
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}
先给这个球持续增加一个外力
在Void Update(){的后面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 玩家脚本 : MonoBehaviour
{
    public Rigidbody rb;
    public float moveSpeed = 30;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      rb.AddForce(0, 0, 1200 * Time.deltaTime);
    }
}
然后在添加用键盘控制方向的代码
在 rb.AddForce(0,0,1200* Time.deltaTime); 之后两行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 玩家脚本 : MonoBehaviour
{
    public Rigidbody rb;
    public float moveSpeed = 30;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      rb.AddForce(0, 0, 1200 * Time.deltaTime);
      float moveX = Input.GetAxis("Horizontal");
      float moveY = Input.GetAxis("Vertical");
    }
}
然后加入代码(在 float moveY = Input.GetAxis("Vertical"); 之后)
保存。
回来以后运行,点击


老是报错,这个就是解决办法:
点开玩家


下拉,找到:


把上面的刚体拖拽到Rb那里,像我这样,下面是现在的效果


https://www.zhihu.com/video/1542141866077728768
添加障碍物(增加可玩性,因为Unity系统会给你赠送一个真实效果)


https://www.zhihu.com/video/1542274822155735040
只要是在Unity在GameObject-> 3D Object里面的东西,都能随便加
添加完毕以后,点击这个


命名为障碍,并把放置好的障碍物都归到一起,方便后面操作
把相机建一个Creat Empty,命名为CameraAnc


把那个拖拽到玩家
最后是这样的


CameraAnc以玩家的CameraAnc为主
代码是这样的:


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

public class CameraScript : MonoBehaviour
{
    public GameObject followTarget;
    public float moveSpeed;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      if(followTarget!=null)
      {
            var newZ = Mathf.Lerp(transform.position.z, followTarget.transform.position.z, Time.deltaTime * moveSpeed);
            var newVector3 = new Vector3(transform.position.x, transform.position.y, newZ);
            transform.position = newVector3;
      }
    }
}
把这个脚本绑定到Main Camera上面


这个是最终效果:


https://www.zhihu.com/video/1542470192671580160
如何打包?
点击




点击Add open Scenes -> Build,选择路径即可。
债见!
页: [1]
查看完整版本: Unity从入门到入门