Unity預設視窗佈局
恢復預設佈局 Window | Layouts | Default
調大頁面字型 Preference | UI Scaling
新專案預設建立了 SampleScene 場景 {攝像機,平行光}
SampleScene 裡 {攝像機,平行光} 就是兩個遊戲物體
新增物體
選中物體(橙色輪廓)(Inspector顯示該物體元件屬性)
重新命名、刪除物體
移動物體
public GameObject bgmNode;
void Update()
{
if (Input.GetMouseButtonDown(0))
PlayMusic();
}
void PlayMusic()
{
AudioSource audio = bgmNode.GetComponent<AudioSource>();
if (audio.isPlaying)
audio.Stop();
else audio.Play();
}
常用方法:在檢查器裡設定元件參照,指令碼直接存取該元件
public AudioSource bgmComponent;
void Update()
{
if (Input.GetMouseButtonDown(0))
PlayMusic();
}
void PlayMusic()
{
AudioSource audio = bgmComponent;
if (audio.isPlaying)
audio.Stop();
else audio.Play();
}
個人理解每個元件類都有 GetComponent<T> 泛型實體方法,用於獲取當前繫結的節點的各個元件
情景:用一個指令碼元件控制另一個指令碼元件的公開欄位,如修改轉速
可以是API獲取,通過物體節點再獲取指令碼元件型別,也可以直接參照,下面是直接參照做法(Unity框架自動完成元件查詢過程)
官方建議不要獲取物件尤拉角再覆蓋尤拉角,涉及轉換的一些問題
而是固定用一個Vector3當做物件的尤拉角
private Vector3 _eulerAngles;
public float rotateSpeed = 30f;
public GameObject Canno;
void Update()
{
float delta = rotateSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.W))
if (_eulerAngles.x > -60)
_eulerAngles.x -= delta;
if (Input.GetKey(KeyCode.S))
if (_eulerAngles.x < 30)
_eulerAngles.x += delta;
if (Input.GetKey(KeyCode.A))
_eulerAngles.y -= delta;
if (Input.GetKey(KeyCode.D))
_eulerAngles.y += delta;
Canno.transform.localEulerAngles = _eulerAngles;
}
剛體 RigidBody
使物體具有物理學特性。新增剛體元件後由物理引擎負責剛體的運動
碰撞體元件 Collider
設定物體的碰撞體積範圍。也由物理引擎負責
預設新增的碰撞體一般情況下會根據網格自動設定尺寸,可以另外編輯
通過物理材質,設定一些引數後將該物理材質賦給碰撞體元件的物理材質參照
RigidBody 元件引數 Is Kinematic 打勾,此時為運動學剛體
⭐零質量,不會受重力影響,但可以設定速度來移動;這種運動剛體完全由指令碼控制
需滿足以下兩個條件
public Vector3 speed;
void Update() =>
transform.Translate(speed * Time.deltaTime,Space.Self);
private void OnTriggerEnter(Collider other)
{
Debug.Log("發生碰撞");
Debug.Log(other.name);
}
給子彈新增如下程式碼
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.name);
Destroy(other.gameObject);
Destroy(gameObject);
}
Window | Rendering | Lighting (CTRL + 9)
public class BulletLogic : MonoBehaviour
{
public float speed = 1f;
void Update()
{
transform.Translate(0,0,speed * Time.deltaTime,Space.Self);
}
private void OnTriggerEnter(Collider other)
{
if (!other.name.StartsWith("怪獸")) return;
Destroy(other.gameObject);
Destroy(gameObject);
}
}
public class PlayerLogic : MonoBehaviour
{
public GameObject bulletPrefeb;
public GameObject bulletFolder;
public Transform firePos;
public Transform fireEulerAngles;
public float speed = 15f;
public float lifeTime = 3f;
public float interval = 2f;
private float _interval = 2f;
public float moveSpeed = 15f;
private void Update()
{
_interval += Time.deltaTime;
if (Input.GetMouseButtonDown(0) && _interval > interval)
{
_interval = 0f;
GameObject obj = Instantiate(bulletPrefeb, null);
obj.transform.SetParent(bulletFolder.transform);
obj.transform.position = firePos.position;
obj.transform.eulerAngles = fireEulerAngles.eulerAngles;
obj.GetComponent<BulletLogic>().speed = speed;
obj.GetComponent<BulletLogic>().lifeTime = lifeTime;
}
if(Input.GetKey(KeyCode.A))
transform.Translate(-Time.deltaTime * moveSpeed,0,0,Space.Self);
if(Input.GetKey(KeyCode.D))
transform.Translate(Time.deltaTime * moveSpeed,0,0,Space.Self);
}
}
public class CreatorLogic : MonoBehaviour
{
public GameObject enemyPrefeb;
void Start()
{
InvokeRepeating("CreateEnemy",1f,1f);
}
void CreateEnemy()
{
GameObject obj = Instantiate(enemyPrefeb,transform);
var pos = transform.position;
pos.x += Random.Range(-30, 30);
obj.transform.position = pos;
obj.transform.eulerAngles = new Vector3(0, 180, 0);
}
}
public GameObject explosionPrefeb;
...
private void OnTriggerEnter(Collider other)
{
if (!other.name.StartsWith("怪獸")) return;
Destroy(other.gameObject);
Destroy(gameObject);
GameObject obj = Instantiate(explosionPrefeb, null); // 不要掛載子彈節點下面
// 粒子特效播放完會自毀
obj.transform.position = transform.position;
}
Unity Documentation 、B站 阿發你好 入門視訊教學