code VR

Unity×VR(82)

スポンサーリンク

//tips

//particleのスクリプト起動

Particleをスクリプトで起動する方法も確認しておく。

初期時にオンになっているPlay On Awakeを外し、下記スクリプトをParticleにアタッチ。ParticleOn()メソッドをボタンのonclickに設定した。

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

public class ParticlePlay : MonoBehaviour
{
private ParticleSystem particle;

void Start()
{
particle = GetComponent<ParticleSystem>();
particle.Stop();
}

public void ParticleOn()
{
particle.Play();
StartCoroutine("PartcleStop");
}

private IEnumerator PartcleStop()
{
yield return new WaitForSeconds(2);
particle.Stop();
}
}

//オブジェクトの段階的縮小・拡大

オブジェクトを徐々に小さくしたり、大きくしたりする機能を考える。

下記のスクリプトをcubeにアタッチし、ボタンによりOnStretch()/OnShrink()を実行するようにした。ShrinkEffect()の場合は最小値が0にならないようにloopを1から始めている。

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

public class Shrinkingfunc : MonoBehaviour
{

public void OnStretch()
{
StartCoroutine("StretchEffect");

}

public void OnShrink()
{
StartCoroutine("ShrinkEffect");

}

IEnumerator ShrinkEffect()
{
// 繰り返し回数
int loopcount = 10;

// 更新間隔
float waitsecond = 0.5f;

// スケール設定
// オフセット値
float offsetScale = 1.0f / loopcount;
// 更新値
float updateScale = 1;

// オブジェクトの有効化
transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

for (int loop = 1; loop < loopcount; loop++)
{
// スケール更新
updateScale = updateScale - offsetScale;
transform.localScale = new Vector3(updateScale, updateScale, updateScale);
yield return new WaitForSeconds(waitsecond);
}
// 最終スケール
transform.localScale = new Vector3(offsetScale, offsetScale, offsetScale);
}

IEnumerator StretchEffect()
{
// 繰り返し回数
int loopcount = 10;

// 更新間隔
float waitsecond = 0.5f;

// スケール設定
// オフセット値
float offsetScale = 1.0f / loopcount;
// 更新値
float updateScale = 0;

// オブジェクトの有効化
transform.localScale = new Vector3(0.0f, 0.0f, 0.0f);

for (int loop = 0; loop < loopcount; loop++)
{
// スケール更新
updateScale = updateScale + offsetScale;
transform.localScale = new Vector3(updateScale, updateScale, updateScale);
yield return new WaitForSeconds(waitsecond);
}
// 最終スケール
transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
}

}

コライダーも小さくしようか考えたが、流石に他のプレイヤーとの兼ね合いもあるので従来の大きさのままにしている。場合によっては、コライダーのサイズも一緒に小さくする。

次にオブジェクトのRendererを変更し、透過させるようにする。透過のスクリプトをアタッチする際にはオブジェクトのmaterialのrenderingモードを事前にOpaque以外に設定しておく。今回はFadeに設定して実行した。

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

public class TransparentEffect : MonoBehaviour
{
Color color;

void Start()
{
color = GetComponent<Renderer>().material.color;
OnTransparent();
}

public void OnTransparent()
{
StartCoroutine("TransparentEffectOn");
}

IEnumerator TransparentEffectOn()
{
// 繰り返し回数
int loopcount = 10;

float waitsecond = 0.5f;

float offsetcolor = 1.0f / loopcount;
// 更新値
float updatecolor = 1;

// オブジェクトの有効化
color.a = 1;

for (int loop = 1; loop < loopcount; loop++)
{
// スケール更新
updatecolor = updatecolor - offsetcolor;
color.a = updatecolor;
GetComponent<Renderer>().material.color = color;

yield return new WaitForSeconds(waitsecond);
}
// 最終
yield return new WaitForSeconds(1);
color.a = 1;
GetComponent<Renderer>().material.color = color;
}
}

次はMinimapで作成したレーダーの索敵範囲から外れる方法を模索する。

基本的にはプレイヤーなどのオブジェクトの子要素にminmapに表示するためのminimaplayerをつけた色付きオブジェクトを配置しているが、そのminimaplayerをmaincameraの描写layerでもminimapcameraのlayerでもないものに変えることで、レーダーに引っかからない方法を実現する。

下記スクリプトをminimaplayerをつけた色付きオブジェクトにアタッチし、ボタンで Layerchange()を発動できるようにする。

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

public class ChangeLayer : MonoBehaviour
{
public void Layerchange()
{
this.gameObject.layer = LayerMask.NameToLayer("Water");
}

}

人気の記事

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.