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

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

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

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

Q&A

解決済

1回答

423閲覧

マウスの座標の条件設定がしたい

legne

総合スコア3

Unity3D

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

0グッド

0クリップ

投稿2022/10/24 09:18

前提

ここに質問の内容を詳しく書いてください。
(例)
TypeScriptで●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
unityでマウスで入力できるフリックキーボードを作成しています。
マウスをクリックした座標によって入力を変える機能を実装中に以下のエラーメッセージが発生しました。

実現したいこと

クリックした場所の条件を設定したい。

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

Assets\FlickScript.cs(87,16): error CS0019: Operator '<' cannot be applied to operands of type 'bool' and 'float'

該当のソースコード

FlickScript.cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class FlickScript : MonoBehaviour 7{ 8 Vector3 startTouchPos; 9 Vector3 endTouchPos; 10 11 float flickValue_x; 12 float flickValue_y; 13 float position_x; 14 float position_y; 15 public Text text; 16 17 Rigidbody player_rb; 18 19 [SerializeField] float jumpPower = 10.0f; 20 21 void Start() 22 { 23 player_rb = GetComponent<Rigidbody>(); 24 } 25 26 void Update() 27 { 28 int NameCount = text.text.Length; 29 if (Input.GetMouseButtonDown(0) == true) 30 { 31 startTouchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z); 32 } 33 if (Input.GetMouseButtonUp(0) == true) 34 { 35 endTouchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z); 36 FlickDirection(); 37 GetDirection(); 38 } 39 40 if (Input.GetMouseButtonUp(0)) 41 { 42 43 44 } 45 46 if (text.text.Length >= 10) 47 { 48 text.text = ""; 49 } 50 } 51 52 void FlickDirection() 53 { 54 flickValue_x = endTouchPos.x - startTouchPos.x; 55 flickValue_y = endTouchPos.y - startTouchPos.y; 56 Debug.Log("x座標は" + startTouchPos.x); 57 Debug.Log("y座標は" + startTouchPos.y); 58 Debug.Log("z座標は" + startTouchPos.z); 59 Debug.Log("x スワイプ量は" + flickValue_x); 60 Debug.Log("y スワイプ量は" + flickValue_y); 61 } 62 63 void GetDirection() 64 { 65 position_x = startTouchPos.x; 66 position_y = startTouchPos.y; 67 if (flickValue_x > 500.0f) 68 { 69 transform.position -= new Vector3(0, 0, 3); 70 Debug.Log("あ"); 71 //text.text = text.text + "あ"; 72 73 } 74 75 if (flickValue_x < -500.0f) 76 { 77 transform.position += new Vector3(0, 0, 3); 78 } 79 80 if (flickValue_y > 500.0f) 81 { 82 player_rb.velocity += new Vector3(0, jumpPower, 0); 83 } 84 85 if (Input.GetMouseButtonUp(0)) 86 { 87 if(-5f < position_x < 5f && -5f < position_y < 5) 88 { 89 if (flickValue_x - flickValue_y < 50.0f && flickValue_x + flickValue_y < 50.0f && flickValue_x < -50.0f) 90 { 91 text.text = text.text + "い"; 92 } 93 else if (flickValue_x - flickValue_y < 50.0f && flickValue_x + flickValue_y > 50.0f && flickValue_y > 50.0f) 94 { 95 text.text = text.text + "う"; 96 } 97 else if (flickValue_x - flickValue_y > 50.0f && flickValue_x + flickValue_y > 50.0f && flickValue_x > 50.0f) 98 { 99 text.text = text.text + "え"; 100 } 101 else if (flickValue_x - flickValue_y > 50.0f && flickValue_x + flickValue_y < 50.0f && flickValue_y < -50.0f) 102 { 103 text.text = text.text + "お"; 104 } 105 else 106 { 107 text.text = text.text + "あ"; 108 } 109 } 110 } 111 } 112}

試したこと

エラーメッセージについて調べたが、勉強不足で解決法がわからなかった。

補足情報(FW/ツールのバージョンなど)

unity 2020.3.34f1(64bit)

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

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

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

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

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

guest

回答1

0

ベストアンサー

このエラーは「bool型とfloat型なんて、比較できねーよ」というエラーです。

おそらく、-5f < position_x < 5fでエラーが出ていると思いますが、-5f < position_xの結果はtruefalseといったbool型であり、その値を次の比較に使うことになるので、true < 5fといった処理になり、前述のエラー通りになります。
-5f < position_x && position_x < 5fとしましょう。

投稿2022/10/24 10:07

fiveHundred

総合スコア9801

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問