作業一 : 期末專題
一、今日目標
今天進度 :
1. 完成各自的分工建模,並匯入遊戲中。
2. 在Unity中,撰寫分數程式。
3. 在Unity中,套上飛鏢固定牆壁的程式碼,和讓飛鏢飛行方向一致的程式碼。
二、成果
1. 匯入建模並設計場景。
討論後決定以圍繞噴水池進行攤子擺放。
討論後決定以圍繞噴水池進行攤子擺放。
2. 撰寫分數的程式碼(每個攤位的程式碼不同但類似)
有個存放所有分數變數的程式碼 : Share
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Share : MonoBehaviour {
public static int Score_circle=0; //計算套圈圈的程式碼
public static int Score_balloon=0; //計算射氣球的程式碼
}
using System.Collections.Generic;
using UnityEngine;
public class Share : MonoBehaviour {
public static int Score_circle=0; //計算套圈圈的程式碼
public static int Score_balloon=0; //計算射氣球的程式碼
}
(1) 套圈圈的分數程式碼 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score_Circle : MonoBehaviour {
public Text ScoreBar; //顯示分數版
void OnTriggerEnter(Collider c){
if (c.gameObject.tag == "toy") {
Share.Score_circle++; //從Share程式碼拿變數,計算套中的數量
ScoreBar.text = "" + Share.Score_circle; //顯示分數
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score_Circle : MonoBehaviour {
public Text ScoreBar; //顯示分數版
void OnTriggerEnter(Collider c){
if (c.gameObject.tag == "toy") {
Share.Score_circle++; //從Share程式碼拿變數,計算套中的數量
ScoreBar.text = "" + Share.Score_circle; //顯示分數
}
}
}
成果 :
套圈圈加一個空物件設碰撞,當碰到玩具的空物件碰撞即有分數。
(2) 射氣球的分數程式碼 : (計算分數程式碼直接寫入當一顆氣球消失,加一分)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Ball : MonoBehaviour {
public Text ScoreBar_balloon; //顯示分數版
public GameObject ball; //氣球
void OnTriggerEnter( Collider Ball){
if (Ball.gameObject.tag == "ball1") {
ball.gameObject.SetActive (false);
Share.Score_balloon++;//從Share程式碼拿變數,計算射中的數量
ScoreBar_balloon.text = "" + Share.Score_balloon; //顯示分數
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Ball : MonoBehaviour {
public Text ScoreBar_balloon; //顯示分數版
public GameObject ball; //氣球
void OnTriggerEnter( Collider Ball){
if (Ball.gameObject.tag == "ball1") {
ball.gameObject.SetActive (false);
Share.Score_balloon++;//從Share程式碼拿變數,計算射中的數量
ScoreBar_balloon.text = "" + Share.Score_balloon; //顯示分數
}
}
(3) 成果 :
當飛鏢碰到氣球的碰撞即有分數。
3. 套上飛鏢固定牆壁和讓飛鏢飛行方向一致的程式碼
(1) 程式碼 : (套入飛鏢中)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class Shoot : MonoBehaviour {
public GameObject shoot1; //飛鏢
SteamVR_Controller.Device device; //手把
private void OnTriggerEnter(Collider shoot)
{
if (shoot.gameObject.tag == "wall") { //飛鏢偵測到牆壁
Rigidbody body1 = shoot1.GetComponent<Rigidbody> ();
body1.isKinematic = true; //關上飛鏢的動作,飛鏢就被固定在牆上
body1.detectCollisions = false;
}
}
void OnTriggerStay() {
if (device.GetTouchUp (SteamVR_Controller.ButtonMask.Trigger)) { //當手把放開Trigger
shoot1.transform.rotation = Quaternion.identity; //飛鏢方向固定不旋轉
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class Shoot : MonoBehaviour {
public GameObject shoot1; //飛鏢
SteamVR_Controller.Device device; //手把
private void OnTriggerEnter(Collider shoot)
{
if (shoot.gameObject.tag == "wall") { //飛鏢偵測到牆壁
Rigidbody body1 = shoot1.GetComponent<Rigidbody> ();
body1.isKinematic = true; //關上飛鏢的動作,飛鏢就被固定在牆上
body1.detectCollisions = false;
}
}
void OnTriggerStay() {
if (device.GetTouchUp (SteamVR_Controller.ButtonMask.Trigger)) { //當手把放開Trigger
shoot1.transform.rotation = Quaternion.identity; //飛鏢方向固定不旋轉
}
}
}
(2) 成果 :


沒有留言:
張貼留言