unity
楓資 鄭云晶
-2
加入重力
比圖像小更容易通過
組件清單自訂元件
雙擊
取名
public Rigidbody2D myRigidbody2D;將Rigidbody2D拖入
void Update(){
myRigidbody.velocity=(vector2.up*10);
}void Start():啟用後運行一次
(0,1)
速度
二維向量表示鳥的飛行方向
void Update():每一幀反覆執行
條件判斷
if (Input.GetKeyDown(KeyCode.Space) == true) {
myRigidbody2D.linearVelocity=Vector2.up*10;
}按下空格
public float height;if (Input.GetKeyDown(KeyCode.Space) == true){
myRigidbody2D.linearVelocity=Vector2.up*height;
}新增公共插槽
父對象(parents)
子對象(child)
子對象
父對象
x維持在0
圖片翻轉
新增pipemove腳本
public float movespeed=2;創建移動速度變數
transform.position = transform.position + (Vector3.left*movespeed); 不同電腦運行速度可能不同
transform.position = transform.position + (Vector3.left*movespeed)* Time.delTatime; 創建一個預設(預製)遊戲物件
Prefabricated
創建一個物件pipecreate
創建一個腳本pipecreate
將pipecreate放到攝影機右邊
每隔一段時間產生pipe(剛剛預製好的)
引用預製物件
public GameObject pipe;Instantiate
物件實例化
可以生成場景中不存在的物件
Instantiate(prefab, transform.position, Quaternion.identity);
Instantiate(pipe,transform.position,transform.rotation);生成在生成器的位置
public float createrate=2;幾秒生成一個管子
private float timer=0;計時器
if (timer < createrate) {
timer=Timer+Time.deltaTime;
}
else {
Instantiate(pipe,transform.position,transform.rotation);
timer=0;
}條件假設
void Start()
{
Instantiate(pipe,transform.position,transform.rotation);
}等待第一個管子....
void createpipe() {
Instantiate(pipe,transform.position,transform.rotation);
}函數
void Start(){
createpipe();
}隨機高度
X不變 y隨機值
生成器
高度偏差
public float height=10;float lowestpoint = transform.position.y - height;
float highestpoint = transform.position.y + height;lowestpoint
highestpoint
transform.position
void createpipe() {
Instantiate(pipe,transform.position,transform.rotation);
}new Vector3(transform.position.x,Random.Range(lowestpoint,highestpoint),0)產生隨機數 (整數或浮點數)
private float death=-40;
if (transform.position.x < death){
Destroy(gameObject);
}刪除物件
刪除螢幕外管子
Debug.Log("pipe deleted");顯示在Console
Debug
開啟文本UI
隨螢幕大小縮放
解析度1920x1080
調整文字內容/大小/粗細
調整長寬
新增一個物件
創建腳本
引用文本
public int score;using UnityEditor.UI;public Text Score;public void addscore()
{
score=score+1;
Score.text=score.ToString();
}轉成字串
在預製pipe新增middle物件
在穿過管子時分數加一
盒子碰撞器
觸發器
把形狀調整成長方形在管道間
第一次進入碰撞器就會運行
private void OnTriggerEnter2D(Collider2D collision) {
}引用addscore函數
public LogicScript logic;需要使用程式碼來引用
找到邏輯腳本
Add Tag:logic