1. 程式人生 > 其它 >unity 滾屏2D遊戲設計指南

unity 滾屏2D遊戲設計指南

技術標籤:C#unity3d

UNITY 2D - SideScroller Gameplay(上篇)

(本文所用素材下載連結)

本教程涵蓋側卷軸平鋪/視差環境,玩家移動(帶跳牆),近戰戰鬥,與NPC交談,以及設定庫存模組。其中許多元素可以在其他遊戲模式中使用。

設定和播放器移動:

建立新的Unity 2D專案。匯入到資原始檔夾:背景天空和城市,以及人物影象。

第一步。設定:精靈層和四邊形

在Unity中,2D藝術被新增為精靈。要確定哪些精靈出現在其他精靈的前面,它們可以佔用不同的排序層,或者在排序層中可以為它們指定不同的層順序。開始時唯一的排序層是預設的。單擊Default新增排序層。該列表中的層越低,其內容在場景中顯示的越靠前。在一個排序層中,多個精靈可以通過在層編號中以更高的順序放在前面來排列。

專案背景
EXAMPLE: This project has three Sprites for the sky:
tileSky_blue, tileSky_clouds, and tileSky_wave.
We can drag them all into the Hierarchy and select one to find Sorting Layer in the Inspector.
Click “Default,” add new Sorting Layer “BG_Sky.” Move the Layer above Default (so it is furthest back).
Select each of these 3 sky sprites to add them to that Sorting Layer.
Select tilingSky_blue to set Order in Layer to 2. Set tileSky_wave to Order in Layer 4 (to be in front of tileSky_blue), and set tileSky_clouds to 6.
(To start the next tutorial, delete these sprites from Hierarchy).
第二步。天空無限滾動(來源:精靈設定和滾動

建立一個視差效果,將3個天空精靈分別應用於四邊形並以不同的速度滾動。注意:此指令碼不適用於普通精靈;它們需要應用於三維四邊形網格(3D Quad mesh):
a、 選擇專案中的每個天空影象,將網格型別從“Tight”更改為“Full Rect”,將包裹模式從“Clamp”更改為“Repeat”,compression=none,(點選[Apply])。
b、 右鍵單擊層次以建立三維物件>四邊形。縮放X=18和Y=10以填充舞臺。
c、 選中四邊形後,將天空精靈拖到“檢查器”上以建立和應用材質。將“材質著色器”(Material Shader)設定為“取消照明/紋理”(Unlight/Texture)以獲得全亮度。在“材質”資料夾中找到此材質並複製兩次:選中它並按[Cmd/Ctr]+[d]。選中這兩種其他材質後,在檢查器中按[select]新增其他精靈。
d、 在Hieraerchy中,複製四元體兩次,然後應用另外兩個精靈,這樣每個精靈都有一個四元體。
e、 若要將四邊形推到其他四邊形後面,請將位置Z設定得略高(如0.1)。
f、 拖動背景_滾動指令碼BG_Scroll.cs到每個天空四邊形sky Quads。
g、 在Inspector中,更改不同速度的BGspeed值。
h、 清理:建立一個空的,稱之為背景天空。將天空四邊形拖到上面,使其成為孩子。
using System.Collections;
using UnityEngine;
public class BG_Scroll : MonoBehaviour {

public float BGspeed = 0.5f;
public Renderer BGrend;

void Start(){
	BGrend = GetComponent<Renderer>();
	//Change the GameObject's Material Color to red
	//BGrend.material.color = Color.red;
}

void Update () {
	Vector2 hOffset	= new Vector2 (Time.time * BGspeed, 0);
	BGrend.material.mainTextureOffset = hOffset;		
}

}
第三步。城市視差對攝像機運動的影響

通過3個城市背景層,我們可以使視差運動響應玩家的運動:
a、 建立一個空的遊戲物件,命名為Background\u City。拖到相機上,使其成為孩子。
b、 將每個城市精靈拖到層次結構中,然後拖到背景城市中,使其成為子物件。
c、 將層中的順序設定為2(城市\後面)、4(城市\中間)和6(城市\前面)。在四邊形前面,將Z位置設定為高於四邊形(注意:這些城市精靈被新增到預設排序層,因此所有城市精靈都自動位於BG\ U天空排序層上任何天空精靈的前面)。
d、 將圖層複製兩次。將這些副本移動到原始影象的任一側(如果影象長度為2000畫素,則預設的每單位畫素數=100表示它們可以是位置X=20和X=-20)。將每個副本拖到原始副本上,使其成為子副本(將city\u向後(1)和city\u向後(2)拖到city\u向後,等等)。
e、 選擇每個原始城市精靈,拖動指令碼BG_Scroll.cs將攝影機拖到指令碼槽中。對於BG_天空(空圍棋舉行四邊形),只需將它拖到相機上,使它完全遵循相機。
f、 為每個視差效果槽設定不同的值:city\u Front=0(因此它移動最多)、city\u Mid=0.3、city\u Back=0.7、BG\u Sky=1(因此它根本不移動)。
g、 點選播放並在場景中來回拖動相機,以檢視指令碼工作!

建立測試平臺:建立一個空的遊戲物件,命名為“TestPlatform”。新增Physics2D>BoxCollider2D。將碰撞器大小X設定為原始背景的寬度,Y設定為0.5。設定位置Z=-3,以匹配玩家。
步驟4a.玩家建立和移動
將精靈新增到玩家角色的層次結構中(二次方冪:64x64、128x128、256x256等)。
a、 在層中設定order=50,位置Z=-3。把它命名為playerart。
b、 新增一個空的遊戲物件,命名為Player,重置位置,設定tag=Player。將PlayerArt拖到player上,使其成為子級。新增到player:
c、 新增碰撞元件Component ,Physics2D>BoxCollider2D並縮放以適合上半部分。新增Physics2D>CircleCollider2D並縮放以適合下半部分(以更平滑地與坡道合併)。
d、 新增剛體元件Component, Physics2D>Rigidbody2D,將“約束>凍結旋轉Z”(Constraints>Freeze Rotation Z)設定為啟用,並將碰撞檢測從“離散”(Discrete)更改為“連續”(Continuous)。
第4b步。玩家的動作
f、 拖動playermove.cs指令碼到你的player上(參見上面的player建立)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {

private bool FaceRight = true;  // determine which way player is facing.
public float runSpeed = 10f;

void Update () {
	//Horizontal axis: [a]/left arrow is -1, [d]/right arrow is 1
	Vector3 hMove = new Vector3(Input.GetAxis ("Horizontal"), 0.0f, 0.0f );
	transform.position = transform.position + hMove * runSpeed * Time.deltaTime;

	// if input is moving player right and player faces left, turn, and vice-versa
	if ((hMove.x < 0 && !FaceRight) || (hMove.x > 0 && FaceRight)){
		Turn();
	}
}

private void Turn()
{
	// Switch player facing label
	FaceRight = !FaceRight;

	// Multiply player's x local scale by -1.
	Vector3 theScale = transform.localScale;
	theScale.x *= -1;
	transform.localScale = theScale;
}

}
g、 拖動BetterJump.cs公司把劇本放到你的player上。此指令碼使用跳轉輸入,預設情況下,跳轉設定為鍵盤空格鍵和控制器操縱桿按鈕3(在XBox控制器上:(Y))。更改這些或新增更多選項:開啟“編輯>專案設定>輸入Edit > Project Settings > Inputs.”。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BetterJump : MonoBehaviour
{
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
Rigidbody rb;

void Awake(){
	rb = GetComponent<Rigidbody>();
}

void Update(){
	if (rb.velocity.y < 0) {
	rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
	} else if (rb.velocity.y > 0 && !Input.GetButton ("Jump")){  
		rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
	}
}

}
h、 你的玩家角色的對撞機會卡住嗎?嘗試新增一個光滑的新物理材質到播放器碰撞器材質槽,摩擦和反彈設定為零。
第五步。攝影機跟蹤播放器指令碼

基本跟隨

a、 拖動CameraFollowX.cs公司指令碼到相機上的一個側面滾動遊戲(注:這個指令碼只遵循X軸,滾動背景。確保玩家有玩家標籤)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollowX : MonoBehaviour {
// this script is aplied to a camera meant to folow the player, without LERP.

public GameObject player; // reference to player object
private Vector3 offset; // distance we want to maintain from the player

void Start () {
	// get player by tag
	if (GameObject.FindGameObjectWithTag ("Player") != null) {
		player = GameObject.FindGameObjectWithTag ("Player");
	}
	// camera position - object position before first frame
	offset = transform.position - player.transform.position; 
}

// LateUpdate is called after all other Scene Object scripts finish their Updates
void LateUpdate () {
	Vector3 newPos = new Vector3 ((player.transform.position.x + offset.x), transform.position.y, transform.position.z);
	transform.position = newPos;
}

}
b、 點選播放並通過移動玩家player來測試所有指令碼。

c、 試試這個CameraFollow.cs公司如果需要攝影機(或其他物件)在所有方向上跟隨 。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {
// this script is aplied to a camera meant to folow the player, without LERP.

public GameObject player; // reference to player object
private Vector3 offset; // distance we want to maintain from the player

void Start () {
	// get player by tag
	if (GameObject.FindGameObjectWithTag ("Player") != null) {
		player = GameObject.FindGameObjectWithTag ("Player");
	}
		// camera position - object position before first frame
	offset = transform.position - player.transform.position; 
}

// LateUpdate is called after all other Scene Object scripts finish their Updates
void LateUpdate () {
	transform.position = player.transform.position + offset;
}

}
跟隨LERP
運動延遲: 優秀的LERP教程

拖動CameraControl2DLERP.cs公司在攝影機上編寫指令碼(或使用Rigidbody2D和重力比例=0的新精靈物件)。將玩家精靈拖到目標指令碼槽上。通過移動玩家來點選播放和測試指令碼。
using UnityEngine;

public class CameraControl2DLERP : MonoBehaviour {

public GameObject target;

void FixedUpdate () {
	Vector2 pos = Vector2.Lerp ((Vector2)transform.position, (Vector2)target.transform.position, Time.fixedDeltaTime);
	//transform.position = new Vector3 (pos.x, pos.y, transform.position.y);
	transform.position = new Vector3 (pos.x, pos.y, transform.position.z);
}

}
(注意:對於三維專案,這裡有一個三維攝影機跟隨指令碼:CameraControl3DLERP.cs公司).
using UnityEngine;

public class CameraControl3DLERP : MonoBehaviour {

public Transform target; // drag the intended target object into the Inspector slot
public float smoothSpeed = 10f;
public Vector3 offset; // set the offset in the editor


// Update is called once per frame
void FixedUpdate () {
	Vector3 desiredPosition = target.position + offset;
	Vector3 smoothPosition = Vector3.Lerp (transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
	transform.position = smoothPosition;

	transform.LookAt (target);
}

}