🌟期末專案_嗨!地鼠
🌠遊戲程式碼撰寫
加入音效、顯示過關後的門
using System.Collections;using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class EasyControl : MonoBehaviour {
public static float gametimer;
float roundTimer, changeTime; //如果失敗跳回第一關的控制
int max;
bool gameOver = false, round = false;
public GameObject s1MouseOne, s1MouseTwo;
public List<ControlMouse> mouse = new List<ControlMouse>();
public List<Transform> hole = new List<Transform>();
GameObject door;
void Start()
{
door = GameObject.Find("door");
door.SetActive(false); //先將門隱藏起來
gametimer = 60f;
roundTimer = 0f;
changeTime = 3f;
gameOver = false;
this.GetComponent<AudioSource>().enabled=false; //先不播放背景音樂
}
void Update()
{
if (AttackTool.GameStart)
{
AttackTool.stage = 1;
Debug.Log("score:" + Score.score);
this.GetComponent<AudioSource>().enabled = true; //播放背景音樂
if (gametimer <= 0) gameOver = true;
gametimer -= Time.deltaTime;
roundTimer -= Time.deltaTime;
if (!gameOver && roundTimer <= 0.2f)
{
round = true;
roundTimer = 4f;
}
if (round)
{
max = Random.Range(1, 4);
while (max > 0)
{
max -= 1;
spawnS1Mouse();
}
round = false;
}
}
if (gameOver) //如果遊戲結束,判斷是否通關
{
changeTime -= Time.deltaTime;
AttackTool.GameStart = false;
this.GetComponent<AudioSource>().enabled = false;
if (Score.score >= 70) door.SetActive(true); //成功
else //失敗
{
AttackTool.stage = 0;
Score.score = 0;
GameObject.Find("Canvas").transform.Find("Text").GetComponent<Text>().text = "Game Over.";
if (changeTime <= 0) SceneManager.LoadScene("Easy");
}
}
}
void spawnS1Mouse() //產生第一關的地鼠
{
int index = Random.Range(0, 8);
while (mouse[index] != null) index = Random.Range(0, 8);
if (index % 2 == 0)
{
GameObject s1One = Instantiate(s1MouseOne, new Vector3(hole[index].position.x,
hole[index].position.y - 15f, hole[index].position.z), hole[index].transform.rotation);
mouse[index] = s1One.GetComponent<ControlMouse>();
Destroy(s1One, 3f);
}
else
{
GameObject s1Two = Instantiate(s1MouseTwo, new Vector3(hole[index].position.x,
hole[index].position.y - 15f, hole[index].position.z), hole[index].transform.rotation);
mouse[index] = s1Two.GetComponent<ControlMouse>();
Destroy(s1Two, 3f);
}
}
}
顯示得分及時間
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowScore : MonoBehaviour {
int gameTime;
string msg;
int gameTime;
string msg;
private void Update()
{
if (AttackTool.stage == 1) //第一關
{
gameTime = (int)EasyControl.gametimer;
if (gameTime < 0) gameTime = 0;
msg = "時間:" + gameTime+ " / 分數:" + Score.score+"/ 70";
}
if (AttackTool.stage == 2) //第二關
{
gameTime = (int)NormalControl.gametimer;
if (gameTime < 0) gameTime = 0;
msg = "時間:" + gameTime+ " / 分數:" + Score.score + "/ 200";
}
if (AttackTool.stage == 3) //第三關
{
gameTime = (int)HardControl.gametimer;
if (gameTime < 0) gameTime = 0;
msg = "時間:" + gameTime+ " / 分數:" + Score.score + "/ 300";
}
gameObject.transform.Find("Time").GetComponent<Text>().text = msg;
}
}
{
if (AttackTool.stage == 1) //第一關
{
gameTime = (int)EasyControl.gametimer;
if (gameTime < 0) gameTime = 0;
msg = "時間:" + gameTime+ " / 分數:" + Score.score+"/ 70";
}
if (AttackTool.stage == 2) //第二關
{
gameTime = (int)NormalControl.gametimer;
if (gameTime < 0) gameTime = 0;
msg = "時間:" + gameTime+ " / 分數:" + Score.score + "/ 200";
}
if (AttackTool.stage == 3) //第三關
{
gameTime = (int)HardControl.gametimer;
if (gameTime < 0) gameTime = 0;
msg = "時間:" + gameTime+ " / 分數:" + Score.score + "/ 300";
}
gameObject.transform.Find("Time").GetComponent<Text>().text = msg;
}
}
手把控制、切換場景
using System.Collections;using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(SteamVR_TrackedObject))]
//套用該腳本會自動添加SteamVR_TrackedObject元件
public class AttackTool : MonoBehaviour {
SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device;
public static bool GameStart = false; //遊戲開始控制
public static int stage;
void Awake () {
trackedObj = GetComponent<SteamVR_TrackedObject>();
stage = 0;
}
void FixedUpdate () {
device = SteamVR_Controller.Input((int)trackedObj.index);
if (stage == 0 && device.GetPressDown(SteamVR_Controller.ButtonMask.Grip)) //按下Grip遊戲開始
{
GameStart = true;
}
if (Score.score>=70 && stage == 1 && GameStart == false && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) //按下Trigger進下一關
{
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
SceneManager.LoadScene("Normal");
}
}
if (Score.score >= 200 && stage == 2 && GameStart == false && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) //按下Trigger進下一關
{
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
SceneManager.LoadScene("Hard");
}
}
}
private void OnTriggerEnter(Collider other) //當槌子碰到地鼠,發出音效、加分
{
if (stage == 1)
{
other.gameObject.GetComponent<AudioSource>().enabled = true;
other.gameObject.GetComponent<SphereCollider>().enabled = false;
Score.score += 5;
}
if (stage == 2)
{
other.gameObject.GetComponent<AudioSource>().enabled = true;
other.gameObject.GetComponent<SphereCollider>().enabled = false;
Score.score += 4;
}
if (stage == 3)
{
other.gameObject.GetComponent<AudioSource>().enabled = true;
other.gameObject.GetComponent<SphereCollider>().enabled = false;
Score.score += 3;
}
GameObject.Destroy(other.gameObject,0.5f); //地鼠消失
}
}
沒有留言:
張貼留言