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

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

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

Q&A

0回答

991閲覧

Unity2D タッチ&スワイプでキャラクターが移動しない

退会済みユーザー

退会済みユーザー

総合スコア0

0グッド

0クリップ

投稿2018/12/10 17:57

AssetStoreにある2DRoguelike tutorialを参考にローグライクゲームを制作していたのですが、いざiphone6plusにビルドし起動してみるとプレイヤーキャラクターのタッチ移動が上手く動作しませんでした。

Player.cs

using UnityEngine; using System.Collections; using UnityEngine.UI; //Allows us to use UI. using UnityEngine.SceneManagement; namespace Completed { //Player inherits from MovingObject, our base class for objects that can move, Enemy also inherits from this. public class Player : MovingObject { public float restartLevelDelay = 1f; //Delay time in seconds to restart level.          //ここに食料、壁、音声の値の記述があるが長過ぎるため省略 private Animator animator; //Used to store a reference to the Player's animator component. private int food; //Used to store player food points total during level. #if UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE private Vector2 touchOrigin = -Vector2.one; //Used to store location of screen touch origin for mobile controls. #endif //Start overrides the Start function of MovingObject protected override void Start () { //Get a component reference to the Player's animator component animator = GetComponent<Animator>(); //Get the current food point total stored in GameManager.instance between levels. food = GameManager.instance.playerFoodPoints; //Set the foodText to reflect the current player food total. foodText.text = "Food: " + food; //Call the Start function of the MovingObject base class. base.Start (); } //This function is called when the behaviour becomes disabled or inactive. private void OnDisable () { //When Player object is disabled, store the current local food total in the GameManager so it can be re-loaded in next level. GameManager.instance.playerFoodPoints = food; } private void Update () { //If it's not the player's turn, exit the function. if(!GameManager.instance.playersTurn) return; int horizontal = 0; //Used to store the horizontal move direction. int vertical = 0; //Used to store the vertical move direction. //Check if we are running either in the Unity editor or in a standalone build. #if UNITY_STANDALONE || UNITY_WEBPLAYER //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction horizontal = (int) (Input.GetAxisRaw ("Horizontal")); //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction vertical = (int) (Input.GetAxisRaw ("Vertical")); //Check if moving horizontally, if so set vertical to zero. /*if(horizontal != 0) { vertical = 0;//斜め用に制限解除 }*/ //Check if we are running on iOS, Android, Windows Phone 8 or Unity iPhone #elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE //Check if Input has registered more than zero touches if (Input.touchCount > 0) { //Store the first touch detected. Touch myTouch = Input.touches[0]; //Check if the phase of that touch equals Began if (myTouch.phase == TouchPhase.Began) { //If so, set touchOrigin to the position of that touch touchOrigin = myTouch.position; } //If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero: else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) { //Set touchEnd to equal the position of this touch Vector2 touchEnd = myTouch.position; //Calculate the difference between the beginning and end of the touch on the x axis. float x = touchEnd.x - touchOrigin.x; //Calculate the difference between the beginning and end of the touch on the y axis. float y = touchEnd.y - touchOrigin.y; //Set touchOrigin.x to -1 so that our else if statement will evaluate false and not repeat immediately. touchOrigin.x = -1; //Check if the difference along the x axis is greater than the difference along the y axis. if (Mathf.Abs(x) > Mathf.Abs(y)) { //If x is greater than zero, set horizontal to 1, otherwise set it to -1 horizontal = x > 0 ? 1 : -1; } else { //If y is greater than zero, set horizontal to 1, otherwise set it to -1 vertical = y > 0 ? 1 : -1; } } } #endif . . .

実際の画面ではタッチ&スワイプをするとキャラクターの方向のみがその場で変わり、移動ごとに消費するFoodという値が減少します。プラットフォームをiosにせずunity上で方向キーを使って動かした際は問題なく動くので、タッチに関する記述に不足があると考えているのですが、何が不足しているかわかりません。
見よう見まねで作っているため、質問文自体にも至らない点が多いかと思います。及ばない部分は改めますので、ご教授いただけると嬉しいです。

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

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

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

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

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

sakura_hana

2018/12/11 02:46

Unity上での操作時、キー入力を一瞬だけ(1フレームだけ)行うとどうなりますか?(エディタでは毎フレーム入力が入りますが、スマホだと1フレームだけの入力になる為、そこに差異がありそうな気がします)(検証が難しい場合はInput.GetAxisRaw ("Horizontal")をクラス変数に保存、毎フレームの値と見比べる条件分岐を入れる等してみてください)
退会済みユーザー

退会済みユーザー

2018/12/11 05:26

ご質問ありがとうございます。1時間ほど検証してみましたが、Unity上では正常に動作しており、スマホで発生しているおかしな挙動は確認できませんでした。
sakura_hana

2018/12/11 05:51

となると、ここに書かれていないソースが原因かなと思います。「"移動"が開始していない」「"移動"は開始しているがエラーで途中終了している」「"移動"は開始しているが(移動距離が極端に短い等で)想定以上に早く正常終了している」などが考えられます。移動処理にDebug.Logを仕掛ける等して何が起こっているか確認してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問