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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

210閲覧

ステージ端でキャラクターの移動がカクカクしてしまう

Wings-12

総合スコア18

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2019/03/13 20:50

お世話になります。

現在タッチスクリーンをタップしたらキャラクターがそのタッチした場所に移動するスマホゲームを作っています。
以下の処理を処理を作りたいのですが、問題が起きており困っています。

作りたい処理:エリア(ステージ)端へキャラクター(以下Player1と記述)を移動しても
Player1がカクカクせずにスムーズに移動できるようにすること。

問題:エリア端にキャラクターを移動したときに
Player1がカクカク(エリア端で引っかかったような挙動)をすることです。

参照gif:
イメージ説明

作成したスクリプト:

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Common; 5using System; 6 7/// <summary> 8/// Player1を操作するクラス 9/// 主に移動処理を書いている。 10/// </summary> 11public class Player1Controller : MonoBehaviour 12{ 13 // Player1のバトル開始座標 14 const float player1_startPositionX = -410.0f; 15 const float player1_startPositionY = -92.0f; 16 17 // タッチパネルをタップした位置を取得する変数 18 Touch touch; 19 Vector3 touchPosition; 20 21 // Player1を約1マス分前へ表示 22 Vector3 oneSquareForward = new Vector3(60.0f, 0.0f); 23 24 // Use this for initialization 25 void Start() 26 { 27 // このtransform自身のlocalPositionを初期化 28 this.transform.localPosition = new Vector3( 29 player1_startPositionX, 30 player1_startPositionY, 31 0.0f 32 ); 33 } 34 35 // Update is called once per frame 36 void Update() 37 { 38 if (Input.touchCount > 0) 39 { 40 // タッチパネルをタップした位置を取得 41 touch = Input.GetTouch(0); 42 43 touchPosition = Camera.main.ScreenToWorldPoint(touch.position); 44 45 touchPosition.z = 0.0f; 46 47 // Player1が青エリア内のみ移動 48 if (touchPosition.y < BattleArea.myUpperSide && 49 BattleArea.myLowerSide < touchPosition.y && 50 touchPosition.x + oneSquareForward.x < BattleArea.myRightSide && 51 BattleArea.myLeftSide < touchPosition.x + oneSquareForward.x) 52 { 53 this.transform.position = 54 touchPosition + oneSquareForward; 55 } 56 } 57 } 58} 59

それほど重い処理ではないと思いますし、なぜエリア端でカクカクした動きになってしまうかが分かりません。
どうかアドバイスをお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

このコードだと、if文を満たさない場合(つまりタッチ位置がエリア外の場合)、
transform.positionは変更されないのでその間は移動しないことになります。
その後タッチ位置がエリア内に戻ったタイミングで「その時のタッチ位置」を元に再度移動するので、カクカクする(ワープする)ように見えるんだと思います。

if文にelseを付け足して、適切な移動をするようにコードを加えれば動くと思います。

投稿2019/03/14 01:56

sakura_hana

総合スコア11425

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

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

Wings-12

2019/03/14 21:35

ありがとうございました。 アドバイスに従い、以下のコードを書いたら解決しました。 ```lang-C# // 青エリア外側右上の移動処理 else if (BattleArea.myRightSide - oneSquareForward.x <= touchPosition.x && BattleArea.myUpperSide <= touchPosition.y) { player1Position.x = BattleArea.myRightSide; player1Position.y = BattleArea.myUpperSide; this.transform.position = player1Position; } // 青エリア外側左上の移動処理 else if (touchPosition.x <= BattleArea.myLeftSide - oneSquareForward.x && BattleArea.myUpperSide <= touchPosition.y) { player1Position.x = BattleArea.myLeftSide; player1Position.y = BattleArea.myUpperSide; this.transform.position = player1Position; } // 上記以外の青エリア外側上の処理 else if (BattleArea.myUpperSide < touchPosition.y) { player1Position.x = touchPosition.x + oneSquareForward.x; player1Position.y = BattleArea.myUpperSide; this.transform.position = player1Position; } // 青エリア外側下の処理 else if (touchPosition.y <= BattleArea.myLowerSide) { player1Position.x = touchPosition.x + oneSquareForward.x; player1Position.y = BattleArea.myLowerSide; this.transform.position = player1Position; } // 青エリア外右側の処理 else if (BattleArea.myRightSide - oneSquareForward.x <= touchPosition.x) { player1Position.x = BattleArea.myRightSide; player1Position.y = touchPosition.y; this.transform.position = player1Position; } // 青エリア外左側の処理 else if (touchPosition.x <= BattleArea.myLeftSide - oneSquareForward.x) { player1Position.x = BattleArea.myLeftSide; player1Position.y = touchPosition.y; this.transform.position = player1Position; } ```
sakura_hana

2019/03/15 04:49

今更気付きましたが player1Position = touchPosition + oneSquareForward; player1Position.x = Mathf.Clamp(player1Position.x, BattleArea.myLeftSide, BattleArea.myRightSide); player1Position.y = Mathf.Clamp(player1Position.y, BattleArea.myLowerSide, BattleArea.myUpperSide); this.transform.position = player1Position; にするとif文すら要らなくなるんじゃないかと思います。 (Mathf.Clamp(A, B, C) は「AがBより小さかったらB, Cより大きかったらC, どちらでもなければAのまま」の数値を返すメソッドです)
Wings-12

2019/03/15 11:59

感謝申し上げます。 書いて頂いたソースコードを私が書いた上記のソースコードの代わりに書いたら、 期待通り動作しました。 sakura_hanaさんは本当にすごいです。前回に引き続き本当にありがとうございました。 私もsakura_hanaさんみたいにUnityが使えるように精進します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問