Unityでスイカゲーム

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallController : MonoBehaviour
{
    public GameObject nextBall;
    public int point;
    private GameController gameController; // GameControllerへの参照

    // Start is called before the first frame update
    void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>(); // GameControllerコンポーネントの取得
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(this.gameObject.name == collision.gameObject.name)
        {
            Destroy(this.gameObject);
            gameController.AddScore(point);
            collision.gameObject.GetComponent<BallController>().nextBall = null;
            if (nextBall != null)
            {
                Instantiate(nextBall, transform.position, transform.rotation);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f; // プレイヤーの移動速度
    public GameObject[] ballToSpawn;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        MovePlayer();
        if (Input.GetKeyDown(KeyCode.Space)) // スペースキーが押されたか確認
        {
            Spawn();
        }
    }

    void MovePlayer()
    {
        float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime; // 水平方向の入力を取得
        transform.position += new Vector3(moveX, 0, 0); // プレイヤーの位置を更新
    }
    void Spawn()
    {
        // プレイヤーの位置でプレハブをインスタンス化
        Instantiate(ballToSpawn[Random.Range(0,3)], transform.position, transform.rotation);
    }

}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{
    public TextMeshProUGUI textScore;
    private int score = 0; // スコアの値

    // Start is called before the first frame update
    void Start()
    {
        textScore.text = "Score: 0";
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void AddScore(int newScore)
    {
        score += newScore;
        textScore.text = "Score: " + score; // テキストオブジェクトを更新
    }

}
つむつむ

前の記事

57.#7