【unity实践demo】unity-2D游戏官方案例-

 

👨‍💻个人主页:@元宇宙-秩沅

hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

本文由 秩沅 原创

收录于专栏 unity实战入门

 ⭐相关文章⭐
———————————————————
-[本站最全-unity常用API大全(万字详解),不信你不收藏]

-[关于游戏剧情模式中用到的基础简单API]

-[控制游戏人物移动的细节到底有多少?]

 -[坦克炮管旋转发射炮弹 游戏demo ]

-[基于unity物体定点移动与模拟刹车的细节 GIF 图文详解]
————————————————————

目录

一.资源的导入

二,地图地形的绘制

三,层级渲染

四,添加运动脚本

五,物理碰撞

六.Tilemap collider2D(瓦片地图碰撞器)

七,添加主人公的生命值

八,陷阱效果

八,为人物添加动画

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

核心代码:

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


一.资源的导入

视频教案:

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

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

bc55192bbb9c52a195308135879eaf09.jpeg

584065c08141c492f0bf6932399f8917.jpeg

2D Beginner: Complete Project

二,地图地形的绘制

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

1..怎么绘制地形

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

e602a6539a3841b94b44bf8eef65f715.jpeg

5ab4c420185bd0c09628bdce25704d0b.jpeg

2.怎么更快的绘制地形

编辑器的快捷键介绍:

S 选中 ,M移动 ,B画笔, I取色器 ,U按母体绘制,D橡皮 ,G大范围绘制

aa56d21732db05d3360624187918c4bf.jpeg

三,层级渲染

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

为什么要实现层级渲染,因为在编辑过程中会出现,同级物体将物体随机覆盖的时候。这是人物和环境都被地形覆盖。

1aac5ed8b9a0086f3310e3dd8fec5d76.jpeg

1.渲染的先后顺序

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

c7a500ace164252bc23336829aafa9b7.jpeg

2.基本操作的细节

关键操作: sprite renderer -order in layer -将其层级设置为最小,可以为上图的-1,即为先渲染

实现后渲染覆盖先渲染,此时人物和环境显示在画面中。

5cd65a89a789bcbd2c67487c832b9a88.jpeg

四,添加运动脚本

视频教案: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。。。)

f61b3192c8a94937e15dbf67b5b74b87.jpeg

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

fb51639588f56f559c374d0509f61e2d.jpeg

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

19ef2b1bd8b248144476f34ecfc54aae.png

9a112608e38b4c05750eb12292d0bed0.jpeg

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

1.碰撞后产生的旋转

解决措施:勾选z轴,即可解决旋转的效果

bb8a96867b2fadac1d532fc1b65cc3b6.jpeg

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。

10f596fbaa2501ce7b6ad78e8da70ae5.jpeg

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

9b4cf16e6fa0195b502d555d0cf457cf.jpeg

7427b48c42b56ae89188be98bbde51a9.jpeg

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

1b6e00c157f2917a30db4a5ad6326970.jpeg

实现目的: 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)添加计时器的脚本 (作为牛逼时间)

0172bdc504543974497f8074eecee08f.jpeg

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

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

a819abe5a9544478e3f85d90b7f21051.jpeg

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

6e365c27da308b9db8913fe91624afdc.jpeg

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

691981d8ed528288b8b550566c9766a7.jpeg

4.添加动画资源

e3d97dd40f94895cfa7b528cfc5de5aa.jpeg

(1)怎样添加动画资源(资源后缀名,.anim)

直接将动画资源拖拽到Animator 动画编辑器当中

988af08a022fa432351622db4a77c078.jpeg

(2)实现动画的切换

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

f4027bb780d9e33526f9f269dbbd57b6.jpeg

(3)动画资源后缀名为.anim 这种动画资源是怎么制作呢

window -Animation-Aniamtion(快捷键ctrl+6)打开动画素材编辑器

8f660bf1ef9c5f5a3909ad48669e1469.jpeg

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

fe3d00cafae3f6912f2dd22367d17610.jpeg

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

(1)让敌人运动

(2)添加一个变化方向的参数,为之后向反方向去运动。

(3)添加计时器代码,目的:1控制移动的时间 2.控制方向变化

1d351029136fda2b4fe300511a763e5e.jpeg

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

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

79c33ddeb093faf765fd092160795795.png

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

1.关闭退出时间

5ac07a5b3cfceeead2cf1f1706528900.jpeg

2.将Transiton Duration由0.25变为0,

8dd572c59c3f2a75db0619bd956f62fc.jpeg

c02bfc2a96afa7bd217938c5f506a731.jpeg

核心代码:

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常用API大全(万字详解),不信你不收藏]

-[关于游戏剧情模式中用到的基础简单API]

-[控制游戏人物移动的细节到底有多少?]

 -[坦克炮管旋转发射炮弹 游戏demo ]

-[基于unity物体定点移动与模拟刹车的细节 GIF 图文详解]
————————————————————

你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秩沅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值