# Unity基礎教學 ## 基礎版面配置 以下皆以2by3來介紹(可從右上版面配置來更改) ![](https://i.imgur.com/61grbeb.jpg) 1、 場景畫面 基礎操作(左鍵: 移動場景 右鍵: 轉動視角 右鍵+WASD 向那個方向前進) 2、 遊戲畫面 3、 場靜物件 4、 專案資料夾 5、 專案物件 6、 物件編輯器 ## 物件移動 最陽春的物件控制方法,且不方便大量修改 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class player_move2 : MonoBehaviour { // Update is called once per frame void Update() { if (Input.GetKey(KeyCode.W)) #偵測輸入按鍵 { gameObject.transform.position += new Vector3(0f, 0f, 0.5f); #物件Z軸增加0.5 } if (Input.GetKey(KeyCode.S)) { gameObject.transform.position += new Vector3(0f, 0f, -0.5f); } if (Input.GetKey(KeyCode.A)) { gameObject.transform.position += new Vector3(-0.5f, 0f, 0f); } if (Input.GetKey(KeyCode.D)) { gameObject.transform.position += new Vector3(0.5f, 0f, 0f); } } } ``` 利用Character Controller來進行控制 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class player_move : MonoBehaviour { public CharacterController controller; #導入CharacterController 命名成controller public float player_speed = 5f; #定義物件移動速度 public float player_jumpSpeed = 3.5f; #定義物件跳躍速度 public float player_gravity = -9.18f; #定義物件重力加速度 Vector3 velocity; #定義一個向量速度 // Update is called once per frame void Update() { if (controller.isGrounded && velocity.y < 0) #如果CharacterController偵測到物件在地上且沒有再向下掉了 { velocity.y = 0f; #將Y軸速度設為0 } float x = Input.GetAxis("Horizontal"); #定義x是X軸上的變化量(初始按鍵是W、S和↑、↓) float z = Input.GetAxis("Vertical"); #定義z是Z軸上的變化量 Vector3 move = transform.right * x + transform.forward * z; if (controller.isGrounded) { if (Input.GetButtonDown("Jump")) { velocity.y = player_jumpSpeed; } } controller.Move(move * player_speed * Time.deltaTime); velocity.y += player_gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } } ``` 有關Time.deltaTime的說明可以<a href=https://youtu.be/a-w7w8x_moE/>看這裡</a> ## 鏡頭跟隨 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class player_look : MonoBehaviour { public float player_sensitive = 100f; #定義鏡頭靈敏度 public Transform player; 導入玩家的位置轉變 float xRotation = 0f; // Start is called before the first frame update void Start() { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; #將滑鼠設為不可視且鎖住位置 } // Update is called once per frame void Update() { float mouseX = Input.GetAxis("Mouse X") * player_sensitive * Time.deltaTime; float mouseY = Input.GetAxis("Mouse Y") * player_sensitive * Time.deltaTime; xRotation -= mouseY; #將xRotation設為負的滑鼠Y軸變化量 xRotation = Mathf.Clamp(xRotation, -90f, 90f); #控制Rotation會限制在90~-90度之間 transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); #抵銷掉玩家自身X軸的旋轉量 player.Rotate(Vector3.up * mouseX); #玩家轉向以自身Y軸乘以滑鼠X軸的變化量 } } ``` ## 敵人控制 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class armyControll : MonoBehaviour { public GameObject aimObject; #從整個遊戲場景中抓目標物件 NavMeshAgent nav; #導入NavMeshAgent // Use this for initialization void Start () { nav = GetComponent<NavMeshAgent>(); #導入NavMeshAgent } // Update is called once per frame void Update () { nav.SetDestination(aimObject.transform.position); #追蹤敵人 } } ``` ![](https://i.imgur.com/tRD4gdC.png) 選擇可以導航的場地 ![](https://i.imgur.com/rMM6pFm.png) 將Navigation Static打勾 ![](https://i.imgur.com/TQD3Ych.png) 接著Bake ## 敵人攻擊 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class armyAttack : MonoBehaviour { public GameObject player; public float attackDistance = 2f; public playerHP playerHP; public int damage; // Update is called once per frame void Update () { Vector3 distance = gameObject.transform.position - player.transform.position; #向量中的距離=物件位置-玩家位置 float nowDistance = distance.magnitude; #將向量轉換成長度 if(nowDistance <= attackDistance) { attack(); } } void attack() { playerHP.hpDecrease(damage); #連接到玩家血量腳本裡面的hpDecrease函數,傳入damage } } ``` ## 玩家血量 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerHP : MonoBehaviour { public int MaxHealth = 100; public int playerhp; private float nextFire; public float delay; #受到傷害的間閣 // Use this for initialization void Start () { nextFire = 0f; delay = 1f; playerhp = MaxHealth; } public void hpDecrease(int damage) { nextFire += delay; if(nextFire >= 60) { playerhp -= damage; nextFire = 0f; } } ``` ## UI介面 剛剛的程式已經可以改變玩家的血量了,但如果要讓玩家親自看到自己的血量就需要用到UI介面了! ![](https://i.imgur.com/VplFh79.png) 可以將腳本改成這樣 ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class player_HP : MonoBehaviour { public int MaxHealth = 100; public int playerhp; private float nextFire; public float delay; public Text HP; // Use this for initialization void Start() { nextFire = 0f; delay = 1f; playerhp = MaxHealth; } public void hpDecrease(int damage) { nextFire += delay; if (nextFire >= 60) { playerhp -= damage; HP.gameObject.GetComponent<Text>().text = "" + playerhp; nextFire = 0f; } } } ``` ## 資源商店 ![](https://i.imgur.com/5PfsFMr.png) 選擇自己想要的套件後 ![](https://i.imgur.com/7Doa4AP.png) ![](https://i.imgur.com/F2Vz16U.png) 左上Packages選擇My Assets,右下import套件