找回密码
 立即注册
查看: 213|回复: 0

2022版本unity-2D游戏官方案例【1】--带视频案例(层级 ...

[复制链接]
发表于 2022-9-2 10:06 | 显示全部楼层 |阅读模式
一.资源的导入

视频教案:
https://www.bilibili.com/video/BV1KW4y1t7aT?p=1&vd_source=417b589e47f6429bbd2ce14c6abe5080

  操作:windoes - Asset store -搜索2D Beginner: Complete Project





2D Beginner: Complete Project

二,地图地形的绘制

视频教案:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili
1..怎么绘制地形

   Hierarch面板-创建2D Object- Tilemap -Rectangle(长方形)




2.怎么更快的绘制地形

  编辑器的快捷键介绍:
  S 选中 ,M移动 ,B画笔, I取色器 ,U按母体绘制,D橡皮 ,G大范围绘制


三,层级渲染


视频教案:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili
为什么要实现层级渲染,因为在编辑过程中会出现,同级物体将物体随机覆盖的时候。这是人物和环境都被地形覆盖。


1.渲染的先后顺序

  编辑器当中,后渲染的会挡住先渲染的,成为系统自带的“盖被子”操作,要想实现这个顺序的调换,咋们要实现一个“掀开被子”操作,也是根据自己的想法让谁先显示出来。


2.基本操作的细节

关键操作: sprite renderer -order in layer -将其层级设置为最小,可以为上图的-1,即为先渲染
  实现后渲染覆盖先渲染,此时人物和环境显示在画面中。


四,添加运动脚本

视频教案:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili
1.让物体能够上下左右进行移动
(1)借助了自身的组件position,根据实时更新它新的位置实现这样一个移动(建议这种方法)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MOVE1 : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed=3.0f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float a, b;
        a = Input.GetAxis("Horizontal"); //让a获得一个水平方向的运动量
        b = Input.GetAxis("Vertical");  //让b获得一个垂直方向的运动量
        Transform ruby = GetComponent<Transform>();
        Vector2 position = ruby.transform.position;
        position.x = position.x + a*Time .deltaTime*speed ;
        position.y = position.y + b*Time .deltaTime *speed;
        ruby.transform.position = position;
        
    }
}
(2)通过它的刚体组件让其获得一个运动的状态
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MOVE1 : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed = 2000.0f;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float a, b;
        a = Input.GetAxis("Horizontal"); //让a获得一个水平方向的运动量
        b = Input.GetAxis("Vertical");  //让b获得一个垂直方向的运动量
        Rigidbody2D ruby = GetComponent<Rigidbody2D>();
        ruby.AddForce(new Vector2 (a*Time .deltaTime*speed ,b*Time .deltaTime*speed ));
        //在x,y轴上面给它添加了一个力让它进行这个运动

    }
五,物理碰撞

视频教案:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili
1.了解基本物理系统,物理组件(Rigidbody ,Box collider。。。)



1.先将rigidbody2D 里面的重力取消,将gravity Scale 变为0


2.调整碰撞器的范围,让其适应游戏物体的大小




2.纠正常见出错的效果细节

1.碰撞后产生的旋转
解决措施:勾选z轴,即可解决旋转的效果


2.碰撞后的抖动
  脚本代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MOVE1 : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed=3.0f;
    public Rigidbody2D ruby2d;
    void Start()
    {
        ruby2d = GetComponent<Rigidbody2D>();
        //首先我们要得到组件,它有了实质
    }

    // Update is called once per frame
    void Update()
    {
        float a, b;
        a = Input.GetAxis("Horizontal"); //让a获得一个水平方向的运动量
        b = Input.GetAxis("Vertical");  //让b获得一个垂直方向的运动量
       Transform ruby = GetComponent<Transform>();
        Vector2 position = ruby.transform.position;
        position.x = position.x + a*Time .deltaTime*speed ;
        position.y = position.y + b*Time .deltaTime *speed;
        // ruby.transform.position = position;//改变的是游戏物体自身组件位置从而达到运动效果
      
        ruby2d.MovePosition(position);
        
    }
}
六.Tilemap collider2D(瓦片地图碰撞器)

教案视频:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili
1.目的:为了添加全局障碍
2.目的:便捷操作
(1)在Add component中添加Tilemap Collider2D。



   (2)打開Project面板中Tile瓦片素材,




None选项是不变成“障碍”,Grid让其变成“障碍”
七,添加主人公的生命值

教案视频:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili
1.给主人公添加生命值脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class blood : MonoBehaviour
{
    float fullblood = 5.0f;
    public float Health = 5.0f;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("此时血量为满血:" + fullblood);
    }
    // Update is called once per frame
    void Update()
    {

    }
    public void changeblood(float vaule) //血量变化
    {
        if(Health == 0)
        {
            Debug.Log("你已經死亡!");
            return;
        }
        Health += vaule;
        if (vaule > 0)
        {
            Debug.Log("血量增加了!"+Health );
        }
        if (vaule < 0)
        {
            Debug.Log("血量减少了!"+Health );
        }
        
    }
}
八,陷阱效果

教案视频:2022版本unity-2D游戏官方案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)_哔哩哔哩_bilibili


实现目的: 1.进入后主人公掉血      

操作流程:

  1.给陷阱添加BOX Collider,并且勾选上IS Tigger,目的:将碰撞器改为触发器。
  2.添加代码(让主人公掉血)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DAMAGE : MonoBehaviour
{
    // Start is called before the first frame update
    private void OnTriggerEnter2D(Collider2D collision) //接触到陷阱的时候发生
    {
        BLOOD ruby = collision.GetComponent<BLOOD>();
        //建立通道,获取BLOOD
        if(collision .tag =="BOSS")
        {
            ruby.changeblood(-1.0f); //负数就是让它的生命值减少
        }
    }
    private void OnTriggerStay2D(Collider2D collision)//呆在陷阱里面发生
    {
        BLOOD ruby = collision.GetComponent<BLOOD>();
        if (collision.tag == "BOSS")
        {
            ruby.changeblood(-1.0f); //负数就是让它的生命值减少
        }
    }
}
实现的目的:

(1)接触陷阱的时候掉血。
(2)呆在陷阱里面还是会掉血。
(3)添加计时器的脚本 (作为牛逼时间)


牛逼时间:(进入牛逼时间不掉血)

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

public class blood : MonoBehaviour
{
    float fullblood = 5.0f;
    public float Health = 5.0f;
    public  float  ntime = 2.0f;//无敌时间
    public float Ntime;
    public bool istime;
  
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("此时血量为满血:" + fullblood);

    }
    // Update is called once per frame
    void Update()
    {
        if(istime)
        {
            if (Ntime > 0)
            {
                Ntime -= Time.deltaTime;
               
            }
            else
            {
                istime = false;
            }
        }
    }
    public void changeblood(float vaule) //血量变化
    {
        if(Health == 0)
        {
            Debug.Log("你已經死亡!");
            return;
        }
        Health += vaule;
        if (vaule > 0)
        {
            Debug.Log("血量增加了!"+Health );
        }
        if (vaule < 0)
        {
            istime = true;
            Ntime = ntime;
            Debug.Log("血量减少了!" + Health);
        }
        
    }
}
八,为人物添加动画

1.Animator(动画师),Inspector面板中添加Add component


2.Animator controller动画控制器 ,project面板里面添加,前提创建文件夹,便于自己梳理素材,拖拽到inspector面板中,Animation组件中的Controller中


3.window-Anniamtion-Animator 添加动画编辑器


4.添加动画资源


(1)怎样添加动画资源(资源后缀名,.anim)
         直接将动画资源拖拽到Animator 动画编辑器当中


(2)实现动画的切换
   右键单击动画时间(被拖拽上去的素材)点击第一个Make transition 创建一个过渡箭头,点击第二个Set 按鼠layer Default state 让其作为初始的默认动画。


(3)动画资源后缀名为.anim 这种动画资源是怎么制作呢
  window -Animation-Aniamtion(快捷键ctrl+6)打开动画素材编辑器


九.让敌人移动并且带有动画



1.添加两个相互方向移动的脚本

(1)让敌人运动
(2)添加一个变化方向的参数,为之后向反方向去运动。
(3)添加计时器代码,目的:1控制移动的时间 2.控制方向变化


2.在脚本中添加动画切换的参数(derection)

目的: 解决动画的毫无规律(让动画按照我们规定来)


3.让动画和移动的方向契合(细节点)

1.关闭退出时间


2.将Transiton Duration由0.25变为0,




核心代码:

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

public class SPORTenemy : MonoBehaviour
{
    public float derection = 1.0f; //代表右边为正方向
    public float maxtime = 5.0f; //给计时器定了个最大值
    public float nowtime = 5.0f;
    public Animator robot;
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()//频率是0.02 和 Void Update //生命周期
    {
        robot.SetFloat("enemyderection", derection);

        if (nowtime>0) //计时器倒计时时改变时间和路程
        {
            Vector2 position = transform.position;
            Rigidbody2D enemy = gameObject.GetComponent<Rigidbody2D>();
            nowtime -= Time.deltaTime; //实时倒计时
            position.x += Time.deltaTime * derection; //路程在变化
                      enemy.MovePosition(position);  //路程在变化
        }
        else
        {
            derection = -derection;
            nowtime = maxtime;
        }

        
      
    }
}
后续请看2022版本unity-2D游戏官方案例【2】--带视频案例(层级渲染,物理碰撞,粒子动画,UI等多位基础一体化)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-30 09:02 , Processed in 0.097715 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表