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

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

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

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

Unity

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

Q&A

解決済

2回答

1826閲覧

マウスの方向にオブジェクトを飛ばすことに関して

TENNEKO

総合スコア6

C#

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

Unity

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

0グッド

0クリップ

投稿2020/08/26 04:39

編集2020/08/26 04:42

前提・実現したいこと

Unity2Dで2Dアクションゲームを作成しています。
マウスをクリックしたらその方向にクナイが真っ直ぐ飛んでいくようにしたいです。

※プレハブからコピーを作成するのではなくこのオブジェクト本体を飛ばしたいです。

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

c#

1Assets\Scripts\HookManager.cs(43,34): error CS1955: Non-invocable member 'Vector2' cannot be used like a method.
Assets\Scripts\HookManager.cs(12,17): warning CS0108: 'HookManager.rigidbody2D' hides inherited member 'Component.rigidbody2D'. Use the new keyword if hiding was intended.
Assets\Scripts\HimoManager.cs(26,9): warning CS0618: 'LineRenderer.SetColors(Color, Color)' is obsolete: 'Use startColor, endColor or colorGradient instead.'
Assets\Scripts\PlayerManager.cs(84,9): warning CS0162: Unreachable code detected
Assets\Scripts\PlayerManager.cs(7,32): warning CS0649: Field 'PlayerManager.blockLayer' is never assigned to, and will always have its default value

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class HookManager : MonoBehaviour 6{ 7 [SerializeField] Transform Player; 8 9 Vector3 prev; 10 FixedJoint2D fixedJoint2D; 11 Rigidbody2D ita; 12 Rigidbody2D rigidbody2D; 13 14 float speed; 15 16 void Start() 17 { 18 rigidbody2D = GetComponent<Rigidbody2D>(); 19 fixedJoint2D = GetComponent<FixedJoint2D>(); 20 ita = GameObject.Find("Ita Portrait").GetComponent<Rigidbody2D>(); 21 GetComponent<FixedJoint2D>().enabled = false; 22 speed = 10.0f; //クナイの速度 23 } 24 25 private void OnTriggerEnter2D(Collider2D collision) 26 { 27 if (collision.gameObject.tag == "Ita Portrait") 28 { 29 Debug.Log("クナイが刺さった"); 30 fixedJoint2D.connectedBody = ita; 31 GetComponent<FixedJoint2D>().enabled = true; 32 } 33 } 34 35 void Update() 36 { 37 var pos = Camera.main.WorldToScreenPoint(transform.localPosition); 38 var rotation = Quaternion.LookRotation(Vector3.forward, Input.mousePosition - pos); 39 40 if (Input.GetMouseButtonDown(0)) 41 { 42 transform.localRotation = rotation; //ここでクナイの向きを変える 43 rigidbody2D.AddForce(Vector2(rotation) * speed, ForceMode2D.Impulse); //ここでマウスの方向に力を加える 44 Debug.Log(rotation); 45 } 46 } 47 48 bool throwing() 49 { 50 return false; 51 } 52}

初心者なので色んなサイトのコードを繋げて書いています。そのため、ぐちゃぐちゃなコードになっていると思います。
マウスの座標のデータの扱いが理解できていないくて対処ができないため、質問させていただきます。

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

Unity:2019.3.12

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2020/08/26 05:46

Vector2の中身はどうなっていますか? エラー文の error CS1955: Non-invocable member 'Vector2' cannot be used like a method. は、Vector2がメゾットとして存在していないと警告しているので、 タイプミス等、何らかのミスがありそうですが
TENNEKO

2020/08/26 07:15 編集

Vector2の中身はどのようにして見ることが出来ますか? void Updat内のコードはほとんど理解していないので。すいません。
guest

回答2

0

Vector2の引数は2つ必要ですね

与えられた x、y 成分で新規のベクトルを作成します
https://docs.unity3d.com/ja/2017.4/ScriptReference/Vector2-ctor.html

rotationは回転の角度を表すだけなので、そのままベクトルに入れてもダメなわけです。
Input.mousePositionとクナイの位置から飛ばしたい方へのベクトルを求めてそれを入れましょう

投稿2020/08/26 08:14

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

TENNEKO

2020/08/27 01:48 編集

ゲームを開始することは出来るようになったのですが、上手く飛ばせません。 using System.Collections; using System.Collections.Generic; using UnityEngine; public class HookManager : MonoBehaviour { [SerializeField] Transform Player; Vector3 prev; FixedJoint2D fixedJoint2D; Rigidbody2D ita; Rigidbody2D rigidbody2D; //クナイのRigidbody2D Transform myTransform; //クナイのTransform Vector2 K_NewPosition; public float K_XPosition; public float K_YPosition; float speed; void Start() { myTransform = GetComponent<Transform>(); rigidbody2D = GetComponent<Rigidbody2D>(); fixedJoint2D = GetComponent<FixedJoint2D>(); ita = GameObject.Find("Ita Portrait").GetComponent<Rigidbody2D>(); K_NewPosition = new Vector2(0.0f, 0.0f); GetComponent<FixedJoint2D>().enabled = false; speed = 10.0f; //クナイの速度 } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Ita Portrait") { Debug.Log("クナイが刺さった"); fixedJoint2D.connectedBody = ita; GetComponent<FixedJoint2D>().enabled = true; } } void Update() { var pos = Camera.main.WorldToScreenPoint(transform.localPosition); var rotation = Quaternion.LookRotation(Vector3.forward, Input.mousePosition - pos); if (Input.GetMouseButtonDown(0)) { transform.localRotation = rotation; //ここでクナイの向きを変える K_NewPosition.x = K_XPosition; K_NewPosition.y = K_YPosition; rigidbody2D.AddForce(new Vector2(K_XPosition, K_YPosition) * speed, ForceMode2D.Impulse); //ここでマウスの方向に力を加える } myTransform.position = K_NewPosition; } bool throwing() { return false; } }
退会済みユーザー

退会済みユーザー

2020/08/28 03:06

K_XPositionとK_YPositionはどこで数値を入れていますか?
TENNEKO

2020/08/29 07:37

K_NewPosition.x = K_XPosition; K_NewPosition.y = K_YPosition; という形でvoid Update()内で入れています。
guest

0

自己解決

勉強不足を実感しました。

投稿2021/03/14 19:12

TENNEKO

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問