0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

育成ゲームをunityで作る

Posted at

育成ゲーム制作記録 パート①

<目次>
(1) pleyerが他のオブジェクトに触れているかどうかを判定する。
(2) 1で作った変数を別のオブジェクトで参照する。
(3) 2で作った変数に合わせて、オブジェクトの表示非表示を切り替える
続きはパート②で作成します。

地面の当たり判定.gif

(0) まずはscratchでプロトタイプの作成

(1) field_touch変数を作る

役割:畑に主人公が侵入しているかどうかを明らかにするための変数
背景:畑に主人公が侵入している最中だけ、plantボタンを表示したい。そのために、判定変数を用意したい。
メモ:script名 ▶︎fieldMove

スクリーンショット 2025-02-28 10.53.33.png

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

public class fieldMove : MonoBehaviour
{
    public bool field_touch = false; 
    
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(field_touch);
    }

    private void OnTriggerStay(Collider other)
    {
        // 触れたオブジェクトが "Player" タグを持っている場合のみ true にする
        if (other.CompareTag("Player"))
        {
            field_touch = true;
            Debug.Log(field_touch);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        // 触れていたオブジェクトが "Player" タグなら false にする
        if (other.CompareTag("Player"))
        {
            field_touch = false;
            Debug.Log(field_touch);
        }
    }
}

(3) field_touch変数に応じて、plantボタンの表示を制御する

メモ:script名 ▶︎ plantButton

スクリーンショット 2025-02-28 23.17.40.png

新しく覚えたこと:

①他のオブジェクトについているスクリプトで宣言した変数を参照する方法

やり方:スクリプトを変数として宣言する型
private クラス名 変数名;

例)fieldMove というスクリプトを変数として使いたい場合
private fieldMove fieldMoveScript;

②見た目を見えなくする方法

失敗記録:
SetActive(false)を使って制御
→一度非アクティブにするとUpdate() が呼び出されなくなる。

対処法
スクリーンショット 2025-03-06 0.26.11.png

私は、CanvasGroup.alphaを使用。
理由:Rendererは3Dオブジェクト(Cube, Sphere など)に使うものなので、今回のUIのButtonには使えない。
備忘録;CanvasGroupコンポーネントとして追加する
CanvasGroup はUIの透明度やクリック可否を制御できる

今後一番使うことの多そうなrenderを使う方法も備忘録としてメモしておく
GetComponent<Renderer>().enabled = true; // 表示

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

public class PlantButton : MonoBehaviour
{
    private CanvasGroup canvasGroup; //renderを使えるようにするためにCanvasGroupを使用
    private fieldMove fieldMoveScript;  // fieldMove スクリプトを保持する変数
    public GameObject targetObject;  // fieldMove がついているオブジェクトを Inspector でセット

    void Start()
    {
        //field_touch変数を使えるようにするために、変数を取得
        fieldMoveScript = targetObject.GetComponent<fieldMove>();
        //canvasGroupを使えるようにコンポーネントを取得
        canvasGroup = GetComponent<CanvasGroup>();

    }

    // Update is called once per frame
    void Update()
    {
        if (fieldMoveScript.field_touch == true)    
        {
        //表示する
        Debug.Log("表示する");
        canvasGroup.alpha = 1f;  // 完全に表示
        canvasGroup.interactable = true;  // ボタンを押せる
        } else{
        //隠す
        Debug.Log("隠す");
        canvasGroup.alpha = 0f;  // 完全に透明
        canvasGroup.interactable = false;  // ボタンを押せない

        }

    }
}


0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?