code VR

Unity×VR(75)

スポンサーリンク

//tips

//Rigidbodyがアタッチされているオブジェクトの瞬間移動

調べていたらRigidbodyが付いたものをtransform.positionで移動させない方が良いということがわかった。

Rigidbodyの衝突計算は、Update前のタイミングで行われており、transform.positionで動かした場合、衝突計算を再計算して余計な負荷がかかるからのようです。

この場合、rigidbody.positionという機能が用意されているので、こちらを使用する。rigd.position = transform.position + transform.forward * 3.0f;で現在座標から三マス前方に瞬間移動する。

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

public class PlayerFlash : MonoBehaviour
{
public float speed =3.0f; //プレイヤーの動くスピード

private Vector3 Player_pos; //プレイヤーのポジション
private Rigidbody rigd;

[SerializeField]
ReceiveEvent receiveEvent;

void Start()
{
Player_pos = GetComponent<Transform>().position; //最初の時点でのプレイヤーのポジションを取得
rigd = GetComponent<Rigidbody>(); //プレイヤーのRigidbodyを取得
}

void FixedUpdate()
{
if (receiveEvent.onflash)
{
transform.rotation = Quaternion.LookRotation(receiveEvent.directionfix);

//rigd.velocity = transform.forward * speed;
rigd.position = transform.position + transform.forward * 3.0f;

receiveEvent.onflash = false;
}
}
}

一度使用したボタンはクールタイムが発生する機能も追加する。MyDragExit()のタイミングで座標の向きを送る処理は終えているのでここに処理を追加する。

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

public class ReceiveEvent : MonoBehaviour
{
Vector3 originpos;
Vector3 movepos;
Vector3 heading;
Vector3 direction;
public Vector3 directionfix;

public bool onflash = false;
bool cooldown = false;
float countTime = 0;

private void Start()
{
Transform transform = GetComponent<RectTransform>();
originpos = transform.position;
}

void Update()
{
if (cooldown)
{
countTime += Time.deltaTime; //スタートしてからの秒数を格納

if(countTime >= 3.0f)
{
cooldown = false;
countTime = 0;
}
}
}

public void MyDragUI()//Drag
{
if (cooldown != true)
{
transform.position = Input.mousePosition;
}
}

public void MyDragExit()//pointerupへ
{
movepos = transform.position;
Dragsimulate();
transform.position = originpos;
cooldown = true;
}

public void Dragsimulate()
{
heading = movepos - originpos;
var distance = heading.magnitude;
direction = heading / distance;
Debug.Log(direction);

directionfix = new Vector3(direction.x,0,direction.y);//2dから3dへ変換
Debug.Log(directionfix);
onflash = true;
}
}

このように三秒のクールダウン時間を設けた。クール時間が分かりづらいのでImage TypeをFilledにして、Fill Methodで表示方法をわかりやすくしていく。

ImageにアタッチされているReceiveEventスクリプトにFill Methodを扱うコードを追加していく。

maxtimeをフィールドに追加し、GetComponent<Image>().fillAmount = countTime / maxtime;をUpdateメソッドに挿入する。filledの種類は円状に再生されるradial360にした。

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

public class ReceiveEvent : MonoBehaviour
{
Vector3 originpos;
Vector3 movepos;
Vector3 heading;
Vector3 direction;
public Vector3 directionfix;

public bool onflash = false;
bool cooldown = false;

float countTime = 0;

[SerializeField]
int maxtime = 3;

private void Start()
{
Transform transform = GetComponent<RectTransform>();
originpos = transform.position;
}

void Update()
{
if (cooldown)
{
countTime += Time.deltaTime; //スタートしてからの秒数を格納
GetComponent<Image>().fillAmount = countTime / maxtime;

if (countTime >= 3.0f)
{
cooldown = false;
countTime = 0;
}
}
}

public void MyDragUI()//Drag
{
if (cooldown != true)
{
transform.position = Input.mousePosition;
}

}

public void MyDragExit()//pointerupへ
{
movepos = transform.position;
Dragsimulate();
transform.position = originpos;
cooldown = true;
}

public void Dragsimulate()
{
heading = movepos - originpos;
var distance = heading.magnitude;
direction = heading / distance;
Debug.Log(direction);

directionfix = new Vector3(direction.x,0,direction.y);//2dから3dへ変換
Debug.Log(directionfix);
onflash = true;
}
}

次にUIボタンのpointerdownで3dオブジェクトを表示させ、UIの動きを反映させ、pointerupで操作した3dオブジェクトの場所にプレイヤーを移動させる機能を作る。

プレイヤーの子要素にカプセルを作成し、透過するマテリアルをアタッチしておく。また、異なる動作をさせるのでボタンも他のものを用意し、プレイヤーの動作スクリプトは新しく作成する。

ボタンのReceiveEventスクリプトにはimageを触った際に発動するスクリプトを追加しておく。public void MyDragIn()//pointerdownとして挿入した。新規動作スクリプトに連携させるため中身はまだ記載しない。imageのEventtriggerのpointerdownを設定しておく。

UI操作でGhostを移動させるのでDragsimulateの一時的な処理ではなく、updateメソッドを使用した継続的な座標の書き換えが必要なことがわかる。

混乱するのでReceiveEventスクリプトではなく新規スクリプトを作成することにする。

人気の記事

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.