using System.Collections; using System.Collections.Generic; using UnityEngine; public class Target : MonoBehaviour { public float speed; void Update() { transform.Rotate(new Vector3(0, 0, speed * Time.deltaTime)); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Needle : MonoBehaviour { Rigidbody2D rb; private void Start() { rb= GetComponent<Rigidbody2D>(); } //死亡碰撞 private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "needle") { print("collider"); //预留业务逻辑 } } //有效碰撞 private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "target") { print("hit"); //预留业务逻辑 } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shooter : MonoBehaviour { public GameObject needlePrefab; public AudioSource player; public AudioClip shootClip; public float up; void Update() { //预留时间缩放度的判断 if (Input.GetMouseButtonDown(0)) { GameObject needle = Instantiate(needlePrefab, transform.position, Quaternion.identity); needle.GetComponent<Rigidbody2D>().velocity = Vector2.up * up; player.PlayOneShot(shootClip); } } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "target") { print("hit"); rb.velocity = Vector2.zero; transform.parent = collision.transform; //预留加分逻辑 } }
//ScentCtrl.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class ScentCtrl : MonoBehaviour { public void loadSecen(string str) { SceneManager.LoadScene(str); } public void quitApp() { Application.Quit(); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour { public static GameManager instance; public GameObject ending; public Text scoreTxt; int score; private void Awake() { instance = this; } void Start() { Time.timeScale = 1; ending.SetActive(false); score = 0; } public void showScore() { score++; scoreTxt.text = score.ToString(); } public void showEnding() { ending.SetActive(true); Time.timeScale = 0; } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "target") { print("hit"); rb.velocity = Vector2.zero; transform.parent = collision.transform; GameManager.instance.showScore(); } }
private void OnCollisionEnter2D(Collision2D collision) { print(collision.gameObject.tag); if (collision.gameObject.tag == "needle") { GameManager.instance.showEnding(); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shooter : MonoBehaviour { public GameObject needlePrefab; public AudioSource player; public AudioClip shootClip; public float up; void Update() { if (Input.GetMouseButtonDown(0) && Time.timeScale != 0) { GameObject needle = Instantiate(needlePrefab, transform.position, Quaternion.identity); player.PlayOneShot(shootClip); needle.GetComponent<Rigidbody2D>().velocity = Vector2.up * up; } } }
//BGM.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BGM : MonoBehaviour { public static BGM instance; //AudioSource必须是public public AudioSource player; public AudioClip bgm; void Awake() { if (instance == null) { instance = this; } else if (instance != this) { Destroy(gameObject); } DontDestroyOnLoad(gameObject); } void Start() { player.clip = bgm; player.Play(); } }
//VolCtrl.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class VolCtrl : MonoBehaviour { public Image volumeImg; public Sprite normalSprite; public Sprite muteSprite; private void Update() { if (Input.GetKeyDown(KeyCode.M)) { BGM.instance.player.mute = !BGM.instance.player.mute; if (BGM.instance.player.mute) volumeImg.sprite = muteSprite; else volumeImg.sprite = normalSprite; } if (Input.GetKeyDown(KeyCode.UpArrow)) { BGM.instance.player.volume += 0.1f; if (BGM.instance.player.volume > 0) { BGM.instance.player.mute = false; volumeImg.sprite = normalSprite; } } if (Input.GetKeyDown(KeyCode.DownArrow)) { BGM.instance.player.volume -= 0.1f; if (BGM.instance.player.volume <= 0) { BGM.instance.player.mute = true; volumeImg.sprite = muteSprite; } } } }