code VR

Unity×VR(64)

スポンサーリンク

//tips

//時間差でのアイテム配布について

時間差でアイテムのロックを解除し、解除したアイテムアイコンの色を変えられることを確認した。

最初はアイテムアイコンをグレーにしておき、それをデフォルトの状態に戻すように変更する。

void SlotManage(int index)
{
Debug.Log("SlotManage");
Debug.Log(inventorytest.items[index].name);

if (inventorytest.items[index].name == "DIY")
{
Button button = slots[0].GetComponentInChildren<Button>();
button.interactable = true;

changeColoricon = slots[0].GetComponentInChildren<ChangeColoricon>();
Debug.Log("changeColoricon"+changeColoricon);
changeColoricon.BackColor();

inventorytest.items.RemoveAt(index);
return;
}

if (inventorytest.items[index].name == "Melon")
{
Button button = slots[1].GetComponentInChildren<Button>();
button.interactable = true;

changeColoricon = slots[1].GetComponentInChildren<ChangeColoricon>();
Debug.Log("changeColoricon" + changeColoricon);
changeColoricon.BackColor();

inventorytest.items.RemoveAt(index);
return;
}

if (inventorytest.items[index].name == "Sea")
{
Button button = slots[2].GetComponentInChildren<Button>();
button.interactable = true;

changeColoricon = slots[2].GetComponentInChildren<ChangeColoricon>();
Debug.Log("changeColoricon" + changeColoricon);
changeColoricon.BackColor();

inventorytest.items.RemoveAt(index);
return;
}

きちんと機能した。他のslotオブジェクトにも上記スクリプトをアタッチするのを忘れない。

現在インベントリーのアイテムをクリックすると、slotスクリプトを経由してItemスクリプトの下記を呼び出している。

public void Use()
{
Debug.Log(name + "を使用");
}

各slotごとにアイテムが分かれており、ButtonにアタッチできるOnClickの機能も異なって問題ないので、それぞれのアイテム使用について考えていく。

まずはプレイヤーの回復から考えていきたいので、以前作成したDecoyのHPUIをprefab化し、Playerにアタッチする。

OnTriggerを発動させるためにはisTriggerにチェックを入れる必要があるが、そうした場合、床の物理判定も無効化し、すり抜けて落下してしまう。

OnCollisionEnterでの処理を考える。

OnCollisionEnterは衝突した際の反発がある場合に発生するので、Coliderコンポーネント内のisTriggerにチェックは外レていなければならない。もちろん、判定したいオブジェクトにRigiBodyコンポーネントが必要。

DecoyにはrodとのOntriggerでの接触判定を設けているが、rodの方のコライダーにIs Triggerチェックが入っているので、Decoyの方には必ずしも必要ではないのでチェックは外すことができる。少なくとも一方のColliderのIs TriggerがtrueならばOnTriggerEnterは成立する。

これによりDecoyとPlayerのOnCollisionEnterが成立する形が作れたのでコードも実装していく。

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

public class HPScript : MonoBehaviour
{
[SerializeField]
private float maxHp = 3; //体力

[SerializeField]
private float hp;

// HP表示用UI
[SerializeField]
private GameObject HPUI;

// HP表示用スライダー
private Slider hpSlider;

void Start()
{
hpSlider = HPUI.transform.Find("HPBar").GetComponent<Slider>();
hpSlider.value = 1f;
}

void Update()
{
hpSlider.value = hp / maxHp;

if (hp <= 0)
{
// HP表示用UIを非表示にする
HideStatusUI();
}

}
//HPUIを非表示にする
public void HideStatusUI()
{
HPUI.SetActive(false);
}

private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Weapon")
{
Debug.Log("hit Player");

Debug.Log("hit Damage");
hp -= other.gameObject.GetComponent<rodManager>().powerEnemy;

//hp -= other.gameObject.GetComponent<rodManager>().powerEnemy;
}

//体力が0以下になった時{}内の処理が行われる
if (hp <= 0)
{
Destroy(gameObject); //ゲームオブジェクトが破壊される
}
}

void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
Debug.Log("hit Enemy");

Debug.Log("hit Damage");
hp -= 1;

}
}
}

これでdamageを受けることが可能になったのでアイテムを使用しての回復を考える。

PortionスクリプトをiconオブジェクトにアタッチしてItemButtonのOnclickにメソッドを読み込ませる。下記がアタッチしたスクリプト。

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

public class Portion : MonoBehaviour
{
[SerializeField]
HPScript hPScript;

public void UsePortion()
{
hPScript.hp += 1;

}
}

さらに一度使ったら再使用できないようにボタンのinteractableと表示の色を変更する。

public void UsePortion()
{
hPScript.hp += 1;

button.interactable = false;

this.gameObject.GetComponent<Image>().color = Color.grey;

//Color mycolor = GetComponent<Image>().color;
//mycolor = Color.grey;

}

ちなみに下記で失敗したのだが、それは変数として現在のカラーをmycolorとして抽出し、その色を灰色に変えたが、そもそものオリジナルにその色を反映させていなかったので意味がなく、結局GetComponent<Image>().color = Color.grey;に落ち着いた。this.gameObjectはつけてもつけなくても良い。

//Color mycolor = GetComponent<Image>().color;
//mycolor = Color.grey;

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, VR
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.