//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を重ねることで、ダッシュボードを作成した。