一、素材處理
背景
(一)
在文件夾中找到back圖片,并在檢查器面板中將back圖片的每單位像素?cái)?shù)設(shè)置為16。

(文件所在地)

(面板設(shè)置)
(二)
將圖片拖入到場景中

場景素材
(一)
生成矩形的瓦片地圖

(二)
打開平鋪調(diào)色板

(三)
新建并命名為map,在原目錄新建一個(gè)文件夾,用于存放你在這個(gè)瓦片地圖里所使用的像素。


(四)
選中tileset-sliced,并在檢查器面板中將tileset-sliced圖片的每單位像素?cái)?shù)設(shè)置為16。

若沒有切割好的tileset,選中tileset,并在檢查器中將Sprite模式更改為多個(gè)并點(diǎn)擊“Sprite Editor”,選擇自定義切片,將數(shù)值更改為每單位像素?cái)?shù)以方便你對(duì)每個(gè)像素的使用,最后點(diǎn)擊“應(yīng)用”確定。

(五)
將修改完成的tileset-sliced放到平鋪調(diào)色板中

二、圖層與角色建立
繪制場景
使用平鋪調(diào)色板繪制來搭建場景

圖層管理
在檢查器中,選擇排序圖層添加標(biāo)簽“Background”“Fontground”.

越是下面的圖層,在場景中的顯示就越上。

處于同一層的顯示,則按照圖層順序的大小,越大則越優(yōu)先顯示。這里我們把背景圖“back”的排序圖層設(shè)置為“Background”,圖層順序設(shè)置為“0”;繪制的場景“Tilemap”的排序圖層設(shè)置為“Background”,圖層順序設(shè)置為“1”,有需要可再做修改。

3.人物建立
(一)
在空白處右鍵新建一個(gè)精靈,選擇正方形即可。

(二)
在下列途徑中找到player-idle

記得更改每單位像素值

(三)
將player-idle-1拖入到sprite的組件Sprite Renderer中的精靈,并修改排序圖層為Frontground。然后將sprite重命名為Player,將位置重置,接下來就能在場景中看見小狐貍了。

(四)
接下來要賦予Player重力以及碰撞,所以我們要給Player添加"Rigidbody 2D"組件和“Box Collider 2D”組件,再給我們繪制的地圖“Tilemap”添加碰撞組件“Tilemap Collider 2D”。嘗試運(yùn)行,發(fā)現(xiàn)小狐貍收到重力影響掉落在繪制的場景中。


三、角色移動(dòng)
1.Player移動(dòng)腳本
新建腳本,命名為PlayerController,代碼如下,實(shí)現(xiàn)對(duì)象根據(jù)按鍵來進(jìn)行左右移動(dòng)。請?jiān)诳刂泼姘彐i定Player的rigbody2d組件的z軸。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayController : MonoBehaviour
{
public Rigidbody2D rb; //獲取對(duì)象剛體
public float speed; //設(shè)置對(duì)象移動(dòng)速度
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()//角色移動(dòng)
{
float horizontalmove;
horizontalmove = Input.GetAxis("Horizontal");
if(horizontalmove != 0)
{
rb.velocity = new Vector2(horizontalmove * speed, rb.velocity.y);
}
}
}
(一)添加跳躍功能和對(duì)象轉(zhuǎn)向功能
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayController : MonoBehaviour
{
public Rigidbody2D rb; //獲取對(duì)象剛體
public float speed; //設(shè)置對(duì)象移動(dòng)速度
public float jumpforce;//設(shè)置跳躍獲得的縱向力
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Movement();
}
void Movement()//角色移動(dòng)
{
float horizontalmove = Input.GetAxis("Horizontal");
float facedicetion = Input.GetAxisRaw("Horizontal");
//如果orizontalmove為1則向左走,-1則向右走
if (horizontalmove != 0)
{
rb.velocity = new Vector2(horizontalmove * speed * Time.deltaTime, rb.velocity.y);
}
//如果facedicetion為1則向左轉(zhuǎn),-1則向右轉(zhuǎn)
if (facedicetion != 0)
{
transform.localScale = new Vector3(facedicetion, 1, 1);
}
//如果按下跳躍鍵,對(duì)象獲得縱向力
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x,jumpforce * Time.deltaTime);
}
}
}
四、角色動(dòng)畫
前置工作
給Player添加組件Animator。

新建文件夾用于存放動(dòng)畫,并在對(duì)應(yīng)文件夾新建動(dòng)畫控制器(Animator Contorller)。

把動(dòng)畫控制器“Player”拖到對(duì)象“Player”的Animator組件。

打開動(dòng)畫

2.站立動(dòng)畫和跑動(dòng)動(dòng)畫
單擊對(duì)象“Player”,然后點(diǎn)擊動(dòng)畫創(chuàng)建,在如下圖所示找到對(duì)應(yīng)的圖片,放到Animation中,調(diào)整合適即可。


3.站立動(dòng)畫與跑動(dòng)動(dòng)畫的轉(zhuǎn)換
打開如下窗口

在“run”與“idle”兩個(gè)動(dòng)畫之間創(chuàng)立過度

添加一個(gè)新參數(shù)“running”

選中“idle”到“run”的過渡關(guān)系,進(jìn)行如下調(diào)整:關(guān)閉退出時(shí)間,過渡持續(xù)時(shí)間為0,當(dāng)移動(dòng)速度大于0.1時(shí),動(dòng)畫過度為“run”。同理設(shè)置“run”到“idle”的過渡關(guān)系。文章來源:http://www.zghlxwxcb.cn/news/detail-775315.html


編輯腳本PlayerController.cs,添加由按鍵判斷動(dòng)畫的功能。文章來源地址http://www.zghlxwxcb.cn/news/detail-775315.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayController : MonoBehaviour
{
public Rigidbody2D rb; //獲取對(duì)象剛體
public Animator anim; //獲取對(duì)象動(dòng)畫組件
public float speed; //設(shè)置對(duì)象移動(dòng)速度
public float jumpforce;//設(shè)置跳躍獲得的縱向力
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Movement();
}
void Movement()//角色移動(dòng)
{
float horizontalmove = Input.GetAxis("Horizontal");
float facedicetion = Input.GetAxisRaw("Horizontal");
//如果orizontalmove為1則向左走,-1則向右走
if (horizontalmove != 0)
{
rb.velocity = new Vector2(horizontalmove * speed * Time.deltaTime, rb.velocity.y);
anim.SetFloat("running", Mathf.Abs(horizontalmove)); //按左鍵時(shí),horizontalmove為負(fù)數(shù),動(dòng)畫由“run”轉(zhuǎn)變?yōu)椤癷dle”,故此處需要Mathf.Abs轉(zhuǎn)變?yōu)榻^對(duì)值
}
//如果facedicetion為1則向左轉(zhuǎn),-1則向右轉(zhuǎn)
if (facedicetion != 0)
{
transform.localScale = new Vector3(facedicetion, 1, 1);
}
//如果按下跳躍鍵,對(duì)象獲得縱向力
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x,jumpforce * Time.deltaTime);
}
}
}
到了這里,關(guān)于Unity2d游戲項(xiàng)目--小狐貍的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!