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

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

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

Q&A

解決済

1回答

384閲覧

青エリア内をタッチしたときのみキャラクターがタッチしたところに移動するようにしたい

Wings-12

総合スコア18

0グッド

0クリップ

投稿2019/02/20 21:23

お世話になります。

現在タッチスクリーンをタップしたらキャラクターがそのタッチした場所に移動するスマホゲームを作っています。
問題は、下記参考画像の青エリアの右辺付近をタップしたときに、
Player1が他の濃い青エリア内をタップしてもタップところに移動しないことです。

参考画像:
![イメージ説明]

作成したスクリプトの一部:

C#

1void Update() 2 { 3 if (Input.touchCount > 0) 4 { 5 // 青エリア内のタッチしたところにPlayer1を描画 6 if (this.transform.position.y < BattleArea.myUpperSide && 7 this.transform.position.x < BattleArea.myRightSide) 8 { 9 Touch touch = Input.GetTouch(0); 10 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position); 11 touchPosition.z = 0.0f; 12 13 // 青エリア上辺以上をタップしてもPlayer1を描画しない 14 if (touchPosition.y < BattleArea.myUpperSide && 15 this.transform.position.x < BattleArea.myRightSide) 16 { 17 this.transform.position = touchPosition; 18 } 19 } 20 } 21 }

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace Common 6{ 7 public static class BattleArea 8 { 9 // 自エリア(ステージ)における各辺の座標 10 public const float myUpperSide = 217.0f; 11 public const float myRightSide = 217.0f; 12 } 13}

スクリプトのどこに問題があるか分かりません。
アドバイスをよろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

質問頂きありがとうございます!

C#

1void Update() 2 { 3 if (Input.touchCount > 0) 4 { 5 // 青エリア内のタッチしたところにPlayer1を描画 6 if (this.transform.position.y < BattleArea.myUpperSide && 7 this.transform.position.x < BattleArea.myRightSide) 8 { 9 Touch touch = Input.GetTouch(0); 10 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position); 11 touchPosition.z = 0.0f; 12 this.transform.position = touchPosition; 13 14 } 15 } 16 } 17

恐らく、if (touchPosition.y < BattleArea.myUpperSide &&
this.transform.position.x < BattleArea.myRightSide)
{このif文が無駄なので、これを省けば動くと思います。

投稿2019/02/21 00:12

編集2019/02/21 00:13
bochan2

総合スコア2050

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

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

Wings-12

2019/02/21 12:35

ご回答ありがとうございます。 実は自己解決しまして、 ```C# if (Input.touchCount > 0) { // タッチパネルをタップした位置を取得 Touch touch = Input.GetTouch(0); Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position); touchPosition.z = 0.0f; // 取得したタップ位置が青エリア以内ならPlayer1の座標を更新 if (touchPosition.y < BattleArea.myUpperSide && touchPosition.x < BattleArea.myRightSide) { this.transform.position = touchPosition; } } ``` で期待通り動作しました。 ご協力ありがとうございました。 ただ別件ですが、 このコメントを書くときにコードをMarkdownを使って見やすくしたかったのですが、 やり方をご存知でしたら教えて頂けないでしょうか。(すみません。やり方を探しましたが、見つかりませんでした。)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問