# 2D遊戲實務 筆記 ## 2021/10/12 ### ●空白資料夾範本(命名方式) ```csharp= Plugins //外掛模組(3rd-SDK) Resources //素材,注意 *s* Audios //音效擋案 Models //模型擋案 Prefabs //預製檔 Sprites //圖檔 Scenes //場景放這裡 Scripts //source code 這裡 ``` ### ※筆記 -Resources.Coad (關於存檔) -素材匯出:(右鍵)點選要匯出的素材/Export package -調整畫面:Pixels PerUnit: ~~100~~ 16 =>一格 16x16 ### ※啟動Tilemap ●先建立 Game Object/Tilemap/Rectangula ●多圖模式:Sprite Mode/Multiple       Slice ▼/Type:Grid By Cell Size/Pixel Size (x:16 y:16)【角色也要改!!】 ●編輯地圖:Create New Palette/命名/拖拉 ▲尋找Tilemap:Windows/2D ●碰撞設定 -角色:Add-Tilempa/Rigidbody 2D + BoxCollider 2D (BoxCollider 2D 設定Edit Collider) -Tilemap:Add-Tileamp (地面互動) ※程式:點角色/拖移程式至右視窗/RB:拖移角色(指定)     ★不旋轉設定:Rigidbody 2D/Constraints z☑ ### ※圖片 ![](https://i.imgur.com/I6JtqF1.png) ![](https://i.imgur.com/SgduMQ2.png) ▲多圖模式 ![](https://i.imgur.com/vVWgXmA.png) ▲16x16的涵義 ![](https://i.imgur.com/ACq55YH.png) ![](https://i.imgur.com/MUoxq3f.png) ▲編輯地圖 ![](https://i.imgur.com/l8iS5Re.png) ▲碰撞框修正 ![](https://i.imgur.com/NsHQCf9.png) ▲地面互動 ### ※移動程式碼 ```csharp= public class PlayerHandler : MonoBehaviour { public float speed = 6.0f; public Rigidbody2D rb; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Movement(); } void Movement() { float horizontalMove = Input.GetAxis("Horizontal"); //讀取輸入數據 if (horizontalMove!=0) { rb.velocity = new Vector2(horizontalMove * speed, rb.velocity.y); //物理位移 } } } ``` ## 2021/10/26 ### ※控制動畫 1.開啟 -window/Animation/.Anitmation            .Anitmator 2.點選角色 -Animator:方框右鍵/.Make Transition 指向             .Set as Layer Default State 初始 -Layers/Parameters + /.Float ☑ /點選"指向箭頭"/右視窗Conditions/running ▼/            .Int                  ↓Geater▼(大於):0.1            .Bool                     ↑Less▼(小於):0.1            .Trigger ## 2021/11/02 ### ※追蹤攝影機 #### -設定跳躍重力 Player右視窗/Player Hanlder (Script)/Jump speed (調整跳躍速度) #### -移動攝影機程式 建立新Scripts為 [CameraHandler] 程式: ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraHandler : MonoBehaviour { void Start() { } // 接收 Tran...(玩家的位置) public Transform player; void Update() { //不斷更新自己(攝影機)的位置Tran, 依據:玩家的位置 this.transform.localPosition = new Vector3 (player.localPosition.x,                          player.localPosition.y,                           z:-10); } } ``` 筆記: transform(變數) Transform(類型) localPosition(本地位置) #### ●載入Cinemachine ##### -安裝方式: Windows / Package Manager/ 視窗右搜尋 "Cinemachine"/ Install下載 ##### -新增攝影機: 上方視窗Cinemachine/Create 2D /新增"CM vcm1" ##### -設定走路背景不超出: 加長背景/把所有背景新增到一個 Create Empty 命名為"BG"/點選"BG"右視窗新增Add/搜尋 Polygon Collider 2D/畫面出現五邊形/▲按 Ctril 點擊"線"刪除一邊 /框出背景區塊/▲右視窗 Is Trigger 打勾(防止角色掉出)/右視窗下面 Extensions AddExtensions:CinemachineConfiner/把"BG"拖進 Bounding Shape2D ### ※圖片 ![](https://i.imgur.com/xWwUaMi.png) ▲右視窗 Is Trigger 打勾(防止角色掉出 ![](https://i.imgur.com/sJmneaw.png) ▲按 Ctril 點擊"線"刪除一邊