code VR

Unity×VR(6)

スポンサーリンク

//tips

//UI表示の追加

Score表示をUI/canvasをベースに追加していく。

今回はrendermodeをworldspaceに設定し、子要素にimageとtextを追加した。

Text部分にscore: 0の表記を行い、これをスクリプトから操作していく。

KillTargetスクリプトに操作内容を追記していく。

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

public class KillTarget : MonoBehaviour
{
public GameObject target;
public ParticleSystem hitEffect;
public GameObject killEffect;
public float timeToselect = 3.0f;
public int score;
public Text scoreText;

private ParticleSystem.EmissionModule hitEffectEmission;
private float countDown;

void Start()
{
score = 0;
countDown = timeToselect;
hitEffectEmission = hitEffect.emission;
hitEffectEmission.enabled = false;
scoreText.text = "Score: 0";

}

void Update()
{
//Transform camera = Camera.main.transform;

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Ray ray = new Ray(camera.position, camera.rotation * Vector3.forward);

float maxDistance = 10;
Debug.DrawRay(ray.origin, ray.direction * maxDistance, Color.green, 5, false);

RaycastHit hit;

if (Physics.Raycast(ray, out hit) && hit.collider.gameObject == target)
{
if (countDown > 0.0f)
{
countDown -= Time.deltaTime;
hitEffect.transform.position = hit.point;
hitEffectEmission.enabled = true;
Debug.Log(countDown);
Debug.Log(hit.collider.gameObject.name);
}
else
{
Instantiate(killEffect, target.transform.position, target.transform.rotation);
score += 1;
scoreText.text = "Score: " + score;
countDown = timeToselect;
SetRandomPosition();
Debug.Log(hit.collider.gameObject.name);

}
}
else
{
hitEffectEmission.enabled = false;
countDown = timeToselect;
Debug.Log(hit.collider.gameObject.name);

}

}

void SetRandomPosition()
{
//float x = Random.Range(-5.0f, 5.0f);
//float z = Random.Range(-5.0f, 5.0f);
target.transform.position = new Vector3(0.0f, 0.0f, 0.0f);
}
}

GameControllerのインスペクターにヒエラルキーからtextオブジェクトをドロップすることを忘れない。

これでEthanを倒すたびにスコアが増加するようになる。

//情報バブルの作成

情報バブルとはカーソルをアバターに合わせると情報が表示されるものである。よく名前が表示されるのを見かける。

今回はwalktargetオブジェクトの座標を表示することにする。

先ほどのcanvas, image,textのセットと同様のものをwalktargetオブジェクトの子要素に加える。

walktarget内のLookMoveToスクリプトを座標を表示できるように編集する。

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

public class LookMoveTo : MonoBehaviour
{
public GameObject ground;
public Transform infoBubble;

private Text infoText;

void Start()
{
if(infoBubble != null)
{
infoText = infoBubble.Find("Text").GetComponent<Text>();
}

}

void Update()
{
Transform camera = Camera.main.transform;
Ray ray;
RaycastHit[] hits;
GameObject hitobject;

Debug.DrawRay(camera.position, camera.rotation * Vector3.forward * 100.0f);

ray = new Ray(camera.position, camera.rotation * Vector3.forward);

hits = Physics.RaycastAll(ray);

for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
hitobject = hit.collider.gameObject;

if (hitobject == ground)
{
if(infoBubble != null)
{
infoText.text = "X:" + hit.point.x.ToString("F2") + ",Z:" + hit.point.z.ToString("F2");

infoBubble.LookAt(camera.position);
infoBubble.Rotate(0.0f, 180.0f, 0.0f);

}

transform.position = hit.point;

}

}
}

}

infoBubble.LookAtにカメラ情報を渡すことで、常にカメラの方向を向くようにしている。カメラの方を向く際にキャンバスが反転してしまうのでy軸に対して180度反転させることで座標もカメラ方向に表示することができる。

 

//ボタンのあるダッシュボード作成

Canvas内にimageを作成し、画像をimageにアタッチ。imageの座標にうまく2つのButtonを重ねることで、ダッシュボードを作成した。

 

人気の記事

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.