?
?????個(gè)人主頁(yè):@元宇宙-秩沅
hallo?歡迎 點(diǎn)贊?? 收藏? 留言?? 加關(guān)注?!
本文由?秩沅?原創(chuàng)
收錄于專欄?unity實(shí)戰(zhàn)入門
??相關(guān)文章?
———————————————————
-[本站最全-unity常用API大全(萬(wàn)字詳解),不信你不收藏]
-[關(guān)于游戲劇情模式中用到的基礎(chǔ)簡(jiǎn)單API]
-[控制游戲人物移動(dòng)的細(xì)節(jié)到底有多少?]
?-[坦克炮管旋轉(zhuǎn)發(fā)射炮彈 游戲demo ]
-[基于unity物體定點(diǎn)移動(dòng)與模擬剎車的細(xì)節(jié) GIF 圖文詳解]
————————————————————文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-436879.html
目錄
一.資源的導(dǎo)入
二,地圖地形的繪制
三,層級(jí)渲染
四,添加運(yùn)動(dòng)腳本
五,物理碰撞
六.Tilemap collider2D(瓦片地圖碰撞器)
七,添加主人公的生命值
八,陷阱效果
八,為人物添加動(dòng)畫(huà)
九.讓敵人移動(dòng)并且?guī)в袆?dòng)畫(huà)
核心代碼:
后續(xù)請(qǐng)看2022版本unity-2D游戲官方案例【2】--帶視頻案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)
一.資源的導(dǎo)入
視頻教案:
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游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_bilibili
1..怎么繪制地形
Hierarch面板-創(chuàng)建2D Object- Tilemap -Rectangle(長(zhǎng)方形)
?
?
2.怎么更快的繪制地形
編輯器的快捷鍵介紹:
S 選中 ,M移動(dòng) ,B畫(huà)筆, I取色器 ,U按母體繪制,D橡皮 ,G大范圍繪制
?
三,層級(jí)渲染
視頻教案:2022版本unity-2D游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_bilibili
為什么要實(shí)現(xiàn)層級(jí)渲染,因?yàn)樵诰庉嬤^(guò)程中會(huì)出現(xiàn),同級(jí)物體將物體隨機(jī)覆蓋的時(shí)候。這是人物和環(huán)境都被地形覆蓋。
?
1.渲染的先后順序
編輯器當(dāng)中,后渲染的會(huì)擋住先渲染的,成為系統(tǒng)自帶的“蓋被子”操作,要想實(shí)現(xiàn)這個(gè)順序的調(diào)換,咋們要實(shí)現(xiàn)一個(gè)“掀開(kāi)被子”操作,也是根據(jù)自己的想法讓誰(shuí)先顯示出來(lái)。
?
2.基本操作的細(xì)節(jié)
關(guān)鍵操作: sprite renderer -order in layer -將其層級(jí)設(shè)置為最小,可以為上圖的-1,即為先渲染
實(shí)現(xiàn)后渲染覆蓋先渲染,此時(shí)人物和環(huán)境顯示在畫(huà)面中。
?
四,添加運(yùn)動(dòng)腳本
視頻教案:2022版本unity-2D游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_bilibili
1.讓物體能夠上下左右進(jìn)行移動(dòng)
(1)借助了自身的組件position,根據(jù)實(shí)時(shí)更新它新的位置實(shí)現(xiàn)這樣一個(gè)移動(dòng)(建議這種方法)
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獲得一個(gè)水平方向的運(yùn)動(dòng)量
b = Input.GetAxis("Vertical"); //讓b獲得一個(gè)垂直方向的運(yùn)動(dòng)量
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)通過(guò)它的剛體組件讓其獲得一個(gè)運(yùn)動(dòng)的狀態(tài)
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獲得一個(gè)水平方向的運(yùn)動(dòng)量
b = Input.GetAxis("Vertical"); //讓b獲得一個(gè)垂直方向的運(yùn)動(dòng)量
Rigidbody2D ruby = GetComponent<Rigidbody2D>();
ruby.AddForce(new Vector2 (a*Time .deltaTime*speed ,b*Time .deltaTime*speed ));
//在x,y軸上面給它添加了一個(gè)力讓它進(jìn)行這個(gè)運(yùn)動(dòng)
}
五,物理碰撞
視頻教案:2022版本unity-2D游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_bilibili
1.了解基本物理系統(tǒng),物理組件(Rigidbody ,Box collider。。。)
?
1.先將rigidbody2D 里面的重力取消,將gravity Scale 變?yōu)?
?
2.調(diào)整碰撞器的范圍,讓其適應(yīng)游戲物體的大小
?
?
2.糾正常見(jiàn)出錯(cuò)的效果細(xì)節(jié)
1.碰撞后產(chǎn)生的旋轉(zhuǎn)
解決措施:勾選z軸,即可解決旋轉(zhuǎn)的效果
?
2.碰撞后的抖動(dòng)
腳本代碼:
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>();
//首先我們要得到組件,它有了實(shí)質(zhì)
}
// Update is called once per frame
void Update()
{
float a, b;
a = Input.GetAxis("Horizontal"); //讓a獲得一個(gè)水平方向的運(yùn)動(dòng)量
b = Input.GetAxis("Vertical"); //讓b獲得一個(gè)垂直方向的運(yùn)動(dòng)量
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;//改變的是游戲物體自身組件位置從而達(dá)到運(yùn)動(dòng)效果
ruby2d.MovePosition(position);
}
}
六.Tilemap collider2D(瓦片地圖碰撞器)
教案視頻:2022版本unity-2D游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_bilibili
1.目的:為了添加全局障礙
2.目的:便捷操作
(1)在Add component中添加Tilemap Collider2D。
?
(2)打開(kāi)Project面板中Tile瓦片素材,
?
?
None選項(xiàng)是不變成“障礙”,Grid讓其變成“障礙”
七,添加主人公的生命值
教案視頻:2022版本unity-2D游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_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("此時(shí)血量為滿血:" + fullblood);
}
// Update is called once per frame
void Update()
{
}
public void changeblood(float vaule) //血量變化
{
if(Health == 0)
{
Debug.Log("你已經(jīng)死亡!");
return;
}
Health += vaule;
if (vaule > 0)
{
Debug.Log("血量增加了!"+Health );
}
if (vaule < 0)
{
Debug.Log("血量減少了!"+Health );
}
}
}
八,陷阱效果
教案視頻:2022版本unity-2D游戲官方案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)_嗶哩嗶哩_bilibili
?
實(shí)現(xiàn)目的: 1.進(jìn)入后主人公掉血
操作流程:
1.給陷阱添加BOX Collider,并且勾選上IS Tigger,目的:將碰撞器改為觸發(fā)器。
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) //接觸到陷阱的時(shí)候發(fā)生
{
BLOOD ruby = collision.GetComponent<BLOOD>();
//建立通道,獲取BLOOD
if(collision .tag =="BOSS")
{
ruby.changeblood(-1.0f); //負(fù)數(shù)就是讓它的生命值減少
}
}
private void OnTriggerStay2D(Collider2D collision)//呆在陷阱里面發(fā)生
{
BLOOD ruby = collision.GetComponent<BLOOD>();
if (collision.tag == "BOSS")
{
ruby.changeblood(-1.0f); //負(fù)數(shù)就是讓它的生命值減少
}
}
}
實(shí)現(xiàn)的目的:
(1)接觸陷阱的時(shí)候掉血。
(2)呆在陷阱里面還是會(huì)掉血。
(3)添加計(jì)時(shí)器的腳本 (作為牛逼時(shí)間)
?
牛逼時(shí)間:(進(jìn)入牛逼時(shí)間不掉血)
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;//無(wú)敵時(shí)間
public float Ntime;
public bool istime;
// Start is called before the first frame update
void Start()
{
Debug.Log("此時(shí)血量為滿血:" + 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("你已經(jīng)死亡!");
return;
}
Health += vaule;
if (vaule > 0)
{
Debug.Log("血量增加了!"+Health );
}
if (vaule < 0)
{
istime = true;
Ntime = ntime;
Debug.Log("血量減少了!" + Health);
}
}
}
八,為人物添加動(dòng)畫(huà)
1.Animator(動(dòng)畫(huà)師),Inspector面板中添加Add component
?
2.Animator controller動(dòng)畫(huà)控制器 ,project面板里面添加,前提創(chuàng)建文件夾,便于自己梳理素材,拖拽到inspector面板中,Animation組件中的Controller中
?
3.window-Anniamtion-Animator 添加動(dòng)畫(huà)編輯器
?
4.添加動(dòng)畫(huà)資源
?
(1)怎樣添加動(dòng)畫(huà)資源(資源后綴名,.anim)
直接將動(dòng)畫(huà)資源拖拽到Animator 動(dòng)畫(huà)編輯器當(dāng)中
?
(2)實(shí)現(xiàn)動(dòng)畫(huà)的切換
右鍵單擊動(dòng)畫(huà)時(shí)間(被拖拽上去的素材)點(diǎn)擊第一個(gè)Make transition 創(chuàng)建一個(gè)過(guò)渡箭頭,點(diǎn)擊第二個(gè)Set 按鼠layer Default state 讓其作為初始的默認(rèn)動(dòng)畫(huà)。
?
(3)動(dòng)畫(huà)資源后綴名為.anim 這種動(dòng)畫(huà)資源是怎么制作呢
window -Animation-Aniamtion(快捷鍵ctrl+6)打開(kāi)動(dòng)畫(huà)素材編輯器
?
九.讓敵人移動(dòng)并且?guī)в袆?dòng)畫(huà)
?
1.添加兩個(gè)相互方向移動(dòng)的腳本
(1)讓敵人運(yùn)動(dòng)
(2)添加一個(gè)變化方向的參數(shù),為之后向反方向去運(yùn)動(dòng)。
(3)添加計(jì)時(shí)器代碼,目的:1控制移動(dòng)的時(shí)間 2.控制方向變化
?
2.在腳本中添加動(dòng)畫(huà)切換的參數(shù)(derection)
目的: 解決動(dòng)畫(huà)的毫無(wú)規(guī)律(讓動(dòng)畫(huà)按照我們規(guī)定來(lái))
?
3.讓動(dòng)畫(huà)和移動(dòng)的方向契合(細(xì)節(jié)點(diǎn))
1.關(guān)閉退出時(shí)間
?
2.將Transiton Duration由0.25變?yōu)?,
?
?
核心代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SPORTenemy : MonoBehaviour
{
public float derection = 1.0f; //代表右邊為正方向
public float maxtime = 5.0f; //給計(jì)時(shí)器定了個(gè)最大值
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) //計(jì)時(shí)器倒計(jì)時(shí)時(shí)改變時(shí)間和路程
{
Vector2 position = transform.position;
Rigidbody2D enemy = gameObject.GetComponent<Rigidbody2D>();
nowtime -= Time.deltaTime; //實(shí)時(shí)倒計(jì)時(shí)
position.x += Time.deltaTime * derection; //路程在變化
enemy.MovePosition(position); //路程在變化
}
else
{
derection = -derection;
nowtime = maxtime;
}
}
}
后續(xù)請(qǐng)看2022版本unity-2D游戲官方案例【2】--帶視頻案例(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)
??相關(guān)文章?
———————————————————
-[本站最全-unity常用API大全(萬(wàn)字詳解),不信你不收藏]
-[關(guān)于游戲劇情模式中用到的基礎(chǔ)簡(jiǎn)單API]
-[控制游戲人物移動(dòng)的細(xì)節(jié)到底有多少?]
?-[坦克炮管旋轉(zhuǎn)發(fā)射炮彈 游戲demo ]
-[基于unity物體定點(diǎn)移動(dòng)與模擬剎車的細(xì)節(jié) GIF 圖文詳解]
————————————————————
你們的點(diǎn)贊?? 收藏? 留言?? 關(guān)注?是我持續(xù)創(chuàng)作,輸出優(yōu)質(zhì)內(nèi)容的最大動(dòng)力!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-436879.html
到了這里,關(guān)于2022版本unity-2D游戲官方案例--帶視頻案例(1)(層級(jí)渲染,物理碰撞,粒子動(dòng)畫(huà),UI等多位基礎(chǔ)一體化)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!