初心者のためのUnity講座! サイドビューアクションゲームの作り方 アイテム取得時にスコア表示を更新させる方法の巻
みなさんこんにちはスタジオしまづの嶋津です。
前回
アイテム取得の実装ができました。 simanezumi1989.hatenablog.com
今回
アイテム取得時にスコアを更新する方法を紹介します。
youtu.be (役に立ったという方はチャンネル登録していただけると嬉しいです^^)
コードは以下のようになります。
using UnityEngine; public class ItemManager : MonoBehaviour { GameManager gameManager; private void Start() { gameManager = GameObject.Find("GameManager").GetComponent<GameManager>(); } public void GetItem() { gameManager.AddScore(100); Destroy(this.gameObject); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { [SerializeField] GameObject gameOverTextObj; [SerializeField] GameObject gameClearTextObj; [SerializeField] Text scoreText; const int MAX_SCORE = 9999; int score = 0; private void Start() { scoreText.text = score.ToString(); } public void AddScore(int val) { score += val; if (score > MAX_SCORE) { score = MAX_SCORE; } scoreText.text = score.ToString(); } public void GameOver() { gameOverTextObj.SetActive(true); Invoke("ReStartThisScene", 1f); } public void GameClear() { gameClearTextObj.SetActive(true); Invoke("ReStartThisScene", 1f); } void ReStartThisScene() { Scene ThisScene = SceneManager.GetActiveScene(); SceneManager.LoadScene(ThisScene.name); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] GameManager gameManager; [SerializeField] LayerMask blockLayer; Rigidbody2D rigidbody2D; float speed = 0; // Jump float jumpPower = 400; public enum MOVE_DIRECTION { STOP, LEFT, RIGHT, } MOVE_DIRECTION moveDirection = MOVE_DIRECTION.STOP; void Start() { rigidbody2D = GetComponent<Rigidbody2D>(); } void Update() { float x = Input.GetAxis("Horizontal"); if (x==0) { // 止まる moveDirection = MOVE_DIRECTION.STOP; } else if (x>0) { // 右に移動 moveDirection = MOVE_DIRECTION.RIGHT; } else if (x < 0) { // 左に移動 moveDirection = MOVE_DIRECTION.LEFT; } if (IsGround() && Input.GetKeyDown("space")) { Jump(); } } private void FixedUpdate() { switch (moveDirection) { case MOVE_DIRECTION.STOP: speed=0; break; case MOVE_DIRECTION.LEFT: speed= -3; break; case MOVE_DIRECTION.RIGHT: speed= 3; break; } rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); } void Jump() { rigidbody2D.AddForce(Vector2.up * jumpPower); } bool IsGround() { return Physics2D.Linecast(transform.position-transform.right*0.3f, transform.position - transform.up * 0.1f, blockLayer) || Physics2D.Linecast(transform.position + transform.right * 0.3f, transform.position - transform.up * 0.1f, blockLayer); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Trap") { gameManager.GameOver(); } if (collision.gameObject.tag == "Finish") { gameManager.GameClear(); } if (collision.gameObject.tag == "Item") { collision.gameObject.GetComponent<ItemManager>().GetItem(); } } }
次の動画ではいよいよ敵の実装を行います
スタジオしまづから
ゲームをリリースしたい人向けのUnity講座を動画販売しています。c#の基礎からリリースまで体系的に学びたい方はぜひ。
https://www.udemy.com/studio_shimazu_nfrpg/?couponCode=FREE_FIRST
また、一人でゲームを作るのは難しいという方はゲームリリースを目的とするオンラインサロンもやっているのでよければどうぞ!話だけでもという方はTwitterのDMだけでもOKです^^
camp-fire.jp