質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

4762閲覧

UnityでiBeaconの距離を利用してオブジェクトを移動させたい

wasabi06

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2016/11/29 18:23

###前提・実現したいこと
UnityでiBeaconの距離を測定し、その距離を変数としてプレイヤーを操作するゲームを作ろうとしています。初心者です。

###発生している問題・エラーメッセージ

Unityの「iBeacon」という有料アセットでiBeaconの検知をしているのですが距離の変数をtransform.position = new Vector3(kyori,0,0);
と入れても実行されません。
ソースはサンプルをいじっただけですので理解できてないところも多々あります。申し訳ありません。

###該当のソースコード
使用言語:C# 文字数過多のためBeacon操作以外は省略してます
using System;
using System.Collections;
using System.Collections.Generic;

internal class Example : MonoBehaviour {
省略
// GameObject for found Beacons
[SerializeField]
private GameObject go_ScrollViewContent;

[SerializeField] private GameObject go_FoundBeacon; List<GameObject> go_FoundBeaconCloneList = new List<GameObject>(); GameObject go_FoundBeaconClone; private float f_ScrollViewContentRectWidth; private float f_ScrollViewContentRectHeight; private int i_BeaconCounter = 0; public Text Xtext; //改変部分 public Text Ytext; public float kyori; public float tes; public double acc; // Receive private List<Beacon> mybeacons = new List<Beacon>(); private void Start() { //bluetoothの起動とチェックの操作。省略 } }; BluetoothState.Init(); } private void SwitchToStatus() {

省略 statusのオンオフ
}

private void SwitchToMenu() {

省略 Menuのオンオフ
}
private void setBeaconPropertiesAtStart() {
RestorePlayerPrefs();
if (bm_Mode == BroadcastMode.unknown) { // first start
bm_Mode = BroadcastMode.receive;
bt_Type = BeaconType.iBeacon;
if (iBeaconServer.region.regionName != "") {
Debug.Log("check iBeaconServer-inspector");
s_Region = iBeaconServer.region.regionName;
bt_Type = iBeaconServer.region.beacon.type;

else if (bt_Type == BeaconType.iBeacon) { s_UUID = iBeaconReceiver.regions[0].beacon.UUID; s_Major = iBeaconReceiver.regions[0].beacon.major.ToString(); s_Minor = iBeaconReceiver.regions[0].beacon.minor.ToString(); } } } InputDropdown.value = (int)bt_Type; bs_State = BroadcastState.inactive; SetBeaconProperties(); SetBroadcastMode(); SetBroadcastState(); Debug.Log("Beacon properties and modes restored"); } // Beacon Properties public void btn_changeUUID() {

省略 入力画面の色とかの設定
}

private void SetBeaconProperties() {

省略 ここでUUIDなどを入れ、上でセットしてます
}
省略
}
}

// BroadcastMode public void btn_switch() { //Debug.Log("Button Switch pressed"); if (bm_Mode == BroadcastMode.receive) bm_Mode = BroadcastMode.send; else bm_Mode = BroadcastMode.receive; SetBroadcastMode(); SavePlayerPrefs(); } private void SetBroadcastMode() { 省略 BroadCastModeをセットしてます } // BroadcastState public void btn_StartStop() { //ここのボタンでBeaconの検知を開始していると思う //Debug.Log("Button Start / Stop pressed"); /*** Beacon will start ***/ if (bs_State == BroadcastState.inactive) { // ReceiveMode if (bm_Mode == BroadcastMode.receive) { iBeaconReceiver.BeaconRangeChangedEvent += OnBeaconRangeChanged; // check if all mandatory propertis are filled if (s_Region == null || s_Region == "") { input_Region.image.color= Color.red; return; } if (bt_Type == BeaconType.Any) { iBeaconReceiver.regions = new iBeaconRegion[]{new iBeaconRegion(s_Region, new Beacon())}; } else if (bt_Type == BeaconType.EddystoneEID) { iBeaconReceiver.regions = new iBeaconRegion[]{new iBeaconRegion(s_Region, new Beacon(BeaconType.EddystoneEID))}; } else { if (s_UUID == null || s_UUID == "") { input_UUID.image.color= Color.red; return; } if (bt_Type == BeaconType.iBeacon) { iBeaconReceiver.regions = new iBeaconRegion[]{new iBeaconRegion(s_Region, new Beacon(s_UUID, Convert.ToInt32(s_Major), Convert.ToInt32(s_Minor)))}; }

省略
}
iBeaconServer.region = new iBeaconRegion(s_Region, new Beacon(s_UUID, Convert.ToInt32(s_Major), Convert.ToInt32(s_Minor)));
}
}
}
// !!! Bluetooth has to be turned on !!! TODO
iBeaconServer.Transmit();
Debug.Log ("It is on, go sending");
}
bs_State = BroadcastState.active;
img_ButtonBroadcastState.color = Color.red;
} else {
if (bm_Mode == BroadcastMode.receive) {// Stop for receive
iBeaconReceiver.Stop();
iBeaconReceiver.BeaconRangeChangedEvent -= OnBeaconRangeChanged;
removeFoundBeacons();
} else { // Stop for send
iBeaconServer.StopTransmit();
}
bs_State = BroadcastState.inactive;
img_ButtonBroadcastState.color = Color.green;
}
SetBroadcastState();
SavePlayerPrefs();
}
private void SetBroadcastState() {
省略
}

private void OnBeaconRangeChanged(Beacon[] beacons) { //ここで検知したBeaconを配列に入れている? foreach (Beacon b in beacons) { var index = mybeacons.IndexOf(b); if (index == -1) { mybeacons.Add(b); } else { mybeacons[index] = b; } } for (int i = mybeacons.Count - 1; i >= 0; --i) { if (mybeacons[i].lastSeen.AddSeconds(10) < DateTime.Now) { mybeacons.RemoveAt(i); } } DisplayOnBeaconFound(); } private void DisplayOnBeaconFound() { removeFoundBeacons(); RectTransform rt_Content = (RectTransform)go_ScrollViewContent.transform; foreach (Beacon b in mybeacons) { // create clone of foundBeacons go_FoundBeaconClone = Instantiate(go_FoundBeacon); // make it child of the ScrollView go_FoundBeaconClone.transform.SetParent(go_ScrollViewContent.transform); // get resolution based scalefactor float f_scaler = ((RectTransform)go_FoundBeaconClone.transform).localScale.y; Vector2 v2_scale = new Vector2(1,1); // reset scalefactor go_FoundBeaconClone.transform.localScale = v2_scale; // get anchorposition Vector3 pos = go_ScrollViewContent.transform.position; // positioning pos.y -= f_ScrollViewContentRectHeight/f_scaler * i_BeaconCounter; go_FoundBeaconClone.transform.position = pos; i_BeaconCounter++; // resize scrollviewcontent rt_Content.sizeDelta = new Vector2(f_ScrollViewContentRectWidth,f_ScrollViewContentRectHeight*i_BeaconCounter); go_FoundBeaconClone.SetActive(true); // adding reference to instance go_FoundBeaconCloneList.Add(go_FoundBeaconClone); // get textcomponents Text[]Texts = go_FoundBeaconClone.GetComponentsInChildren<Text>(); // deleting placeholder foreach (Text t in Texts) t.text = ""; Debug.Log ("fond Beacon: " + b.ToString()); switch (b.type) { //ここでBeaconの情報を表示する操作をしている case BeaconType.iBeacon: Texts[0].text = "UUID:"; Texts[1].text = b.UUID.ToString(); Texts[2].text = "Major:"; Texts[3].text = b.major.ToString(); Texts[4].text = "Minor:"; Texts[5].text = b.minor.ToString(); Texts[6].text = "Range:"; Texts[7].text = b.range.ToString(); Texts[8].text = "Strength:"; Texts[9].text = b.strength.ToString() + " db"; Texts[10].text = "Accuracy:"; Texts[11].text = b.accuracy.ToString().Substring(0,10) + " m"; transform.position = new Vector3 ((float)b.accuracy, 0, 0);//改変部分 ここで位置情報を変更しようとしても無視されてしまいます ここの場合普通にVector3(1,1,1)などと入れても無視されてしまいます。 // accuracyがBeaconとの距離です Texts[12].text = "Rssi:"; Texts[13].text = b.rssi.ToString() + " db"; break; case BeaconType.EddystoneUID:

省略
}
}
}

private void removeFoundBeacons() { 省略 終了時にBeaconのデータを消去してます } // PlayerPrefs private void SavePlayerPrefs() { 省略。要素のセーブをしてます } private void RestorePlayerPrefs() { if (PlayerPrefs.HasKey("Type")) bt_Type = (BeaconType)PlayerPrefs.GetInt("Type"); if (PlayerPrefs.HasKey("Region")) s_Region = PlayerPrefs.GetString("Region"); if (PlayerPrefs.HasKey("UUID")) s_UUID = PlayerPrefs.GetString("UUID"); if (PlayerPrefs.HasKey("Major")) s_Major = PlayerPrefs.GetString("Major"); if (PlayerPrefs.HasKey("Minor")) s_Minor = PlayerPrefs.GetString("Minor"); if (PlayerPrefs.HasKey("BroadcastMode")) bm_Mode = (BroadcastMode)PlayerPrefs.GetInt("BroadcastMode"); else bm_Mode = BroadcastMode.unknown; } void Update(){ //改変部分 if (acc > 0) { transform.position = new Vector3 (1, 1, 1); } if(acc < 0) { transform.position = new Vector3 (-1, -1, -1); }if (acc == 0) { transform.position = new Vector3 (-1, 1, 1); } acc = 0; acc = (double)mybeacons [1].accuracy; //Beaconの配列から距離を持ってきた Xtext.text = "x=" + acc; //確認のため表示 表示された数字は正しい数字でした kyori = (float)acc; //accuracyはdoubleなのでvector3に入れるためにfloatにしています transform.position = new Vector3 (kyori, 0, 0); //ここで移動させようとしていますが実行されません ちなみに上のif文でもずっと0の文が実行されています。

ここではVector3にkyoriを入れず、Vector3(1,1,1)などと入れれば一応実行されます。
Ytext.text = "x=" + acc*2; //確認用に表示 どうやら四則演算は出来るがif文やtransform系のものが実行されないようです。
}

###試したこと
小数点を切り捨てて実行してみたり、いろんな場所にVector3の操作を入れたりしました。
ネットで調べましたがマイナーなアセットらしく良いものが見つかりませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

OnBeaconRangeChangedはExampleオブジェクトが生成されたらすぐに呼ばれるようなものなのですか?
kyoriの元を辿るとmybeacons[1].accuracyに辿り着きます。
ですが、OnBeaconRangeChangedが呼ばれないとmybeacons は空なのでmybeacons[1]にはアクセスできず即エラーになると思います。(unityの場合はゲームが停止するのではなく、そのオブジェクトが動かなくなるだけだと思いますが)

まずエラーメッセージが出ていないか確認してください。

投稿2016/11/30 01:16

ishi9

総合スコア1294

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問