24. diel - Unity (C #) Android: Pľuvanie ohňa
V minulej lekcii, Unity (C #) Android - Koniec hry , sme pridali zakončenia hry a potrebné GUI.
V minulej lekcii, Unity (C #) Android - Koniec hry , som sa zaoberal asynchrónnym načítaním scény, teda sa eliminoval zásek pred štartom hernej scény.
Dnešné tutoriál prinesie novú funkcionalitu a tou je pľuvanie ohňa, ktoré nám umožnia ničiť Stalag, než do nich narazíme. Zároveň som vytvoril obmedzenia, že oheň môžete použiť za hru iba 5x.
Video
Nový PlayerMoveScript
Upravený PlayerMoveScript prináša ovládanie a aktiváciu ohňa:
using UnityEngine.UI; using UnityEngine; using System.Collections; public class PlayerMoveScript : MonoBehaviour { public Text countOfFireText; float flapAmount = 10; public float speed = 150; bool android; bool started = false; private bool paused = false; private Quaternion pausedRotation; public GameObject pauseIndicator; Animator anim; private bool didFlap = false; private GameObject flame; private float fireLength = 2; private float currentFlameTime = 0; private bool currentlyFlaming = false; private int countOfFires = 5; // Use this for initialization void Start() { anim = GetComponent<Animator>(); flame = transform.GetChild(0).gameObject; if (Application.platform == RuntimePlatform.Android) android = true; else android = false; } // Update is called once per frame void Update () { if (currentlyFlaming) { if (currentFlameTime <= 0) { currentFlameTime = 0; DeactivateFire(); } else { currentFlameTime -= Time.deltaTime; } } if (Input.GetKeyDown(KeyCode.Escape)) { paused = !paused; if (paused) { Time.timeScale = 0; //pauseIndicator.enabled = true; pauseIndicator.SetActive(true); pausedRotation = transform.rotation; } else { Time.timeScale = 1; //pauseIndicator.enabled = false; pauseIndicator.SetActive(false); } } if (paused) { if (!android) { if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) { didFlap = true; } } else { if (Input.touches.Length > 0) { didFlap = true; } } transform.rotation = pausedRotation; return; } if (!started) { if (!android) { if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) { StartGame(); } } else { if (Input.touches.Length > 0) { StartGame(); } } return; } Vector3 vel = rigidbody2D.velocity; if (didFlap) { didFlap = false; vel = Flap(vel); } if (!android) { if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) { vel = Flap(vel); } if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.LeftControl)) { vel = Flap(vel); ActivateFire(); } } else { if (Input.touches.Length > 0) { vel = Flap(vel); } } vel.x = speed * Time.deltaTime; rigidbody2D.velocity = vel; RotateMe(vel.y); } void StartGame() { started = true; rigidbody2D.gravityScale = 2.5f; Vector3 vel = rigidbody2D.velocity; vel = Flap(vel); rigidbody2D.velocity = vel; } void RotateMe(float y) { // -20, 10 // -70, 0 // 0 -70 if(y >= 0) { transform.rotation = Quaternion.Euler(0, 0, 0); } else { float angle = Mathf.Lerp(0, -70, -y); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 0.05f); } } Vector3 Flap(Vector3 v) { anim.SetTrigger("DoFlap"); v.y = flapAmount; return v; } void ActivateFire() { if (countOfFires <= 0) return; countOfFires--; countOfFireText.text = "Fire count avalible: " + countOfFires; currentlyFlaming = true; flame.SetActive(true); currentFlameTime = fireLength; } void DeactivateFire() { currentlyFlaming = false; flame.SetActive(false); } }
Upravenie StalagScriptu
using UnityEngine; using System.Collections; public class StalagScript : MonoBehaviour { public GameObject parts; void React() { //print("destroyed"); Destroy(Instantiate(parts, transform.position, Quaternion.identity), 2); Destroy(gameObject); } }
Vytvorenie nového skriptu: FlameCollider
using UnityEngine; using System.Collections; public class FlameCollider : MonoBehaviour { // psala mi velká spousta lidí, že jim nefungují kolize. V 95% případů je to dáno špatnou velikostí písmen v hlavičce této metody, dejte si na to pozor :) void OnTriggerEnter2D(Collider2D col) { if (col.tag == "Stalag") { col.SendMessage("React"); } } }
Ako hra vyzerá teraz?
Problémy?
Ak máte nejaké otázky, neváhajte sa opýtať v komentároch alebo mi napísať do správ.
V budúcej lekcii, Unity (C #) Android: Vylepšenia pľuvanie ohňa , zmeníme ovládanie pľuvanie ohňa.