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

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

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

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

Unity

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

Q&A

解決済

2回答

1486閲覧

「Unity」「C#」2Dアクションゲームでのジャンプの実装について

TENNEKO

総合スコア6

C#

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

Unity

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

0グッド

0クリップ

投稿2020/08/12 07:22

前提・実現したいこと

Unityを用いて上下左右に無限にジャンプが出来るような
2Dアクションゲームを作っています。
そのジャンプ機能の実装中にエラーが発生しました。

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

All compiler errors have to be fixed before you can enter playmode!

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerManager : MonoBehaviour 6{ 7 [SerializeField] LayerMask blockLayer; 8 9 public enum DIRECTION_TYPE 10 { 11 STOP, 12 LEFT, 13 RIGHT, 14 } 15 DIRECTION_TYPE direction = DIRECTION_TYPE.STOP; 16 float speed; 17 Rigidbody2D rigidbody2D; 18 19 float jumpPower = 400; 20 21 void Start() 22 { 23 rigidbody2D = GetComponent<Rigidbody2D>(); 24 } 25 26 void Update() 27 { 28 float x = Input.GetAxis("Horizontal"); 29 30 if (x == 0) 31 { 32 direction = DIRECTION_TYPE.STOP; 33 } 34 else if (x > 0) 35 { 36 direction = DIRECTION_TYPE.RIGHT; 37 } 38 else if (x < 0) 39 { 40 direction = DIRECTION_TYPE.LEFT; 41 } 42 43 if (Input.GetKey("up")) 44 { 45 if (Input.GetKeyDown("space")) 46 { 47 Jump(); 48 } 49 } 50 if (Input.GetKey("right")) 51 { 52 if (Input.GetKeyDown("space")) 53 { 54 RightJump(); 55 } 56 } 57 if (Input.GetKey("left")) 58 { 59 if (Input.GetKeyDown("space")) 60 { 61 LeftJump(); 62 } 63 } 64 if (Input.GetKey("down")) 65 { 66 if (Input.GetKeyDown("space")) 67 { 68 DownJump(); 69 } 70 } 71 } 72 73 void FixedUpdate() 74 { 75 switch (direction) 76 { 77 case DIRECTION_TYPE.STOP: 78 speed = 0; 79 break; 80 case DIRECTION_TYPE.RIGHT: 81 speed = 3; 82 transform.localScale = new Vector3(1, 1, 1); 83 break; 84 case DIRECTION_TYPE.LEFT: 85 speed = -3; 86 transform.localScale = new Vector3(-1, 1, 1); 87 break; 88 } 89 rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); 90 } 91 92 void Jump() 93 { 94 rigidbody2D.AddForce(Vector2.up * jumpPower); 95 } 96 void RightJump() 97 { 98 rigidbody2D.AddForce(Vector2.right * jumpPower); 99 100 void LeftJump() 101 { 102 rigidbody2D.AddForce(Vector2.left * jumpPower); 103 } 104 void DownJump() 105 { 106 rigidbody2D.AddForce(Vector2.down * jumpPower); 107 } 108 109 110 bool IsGround() 111 { 112 Vector3 leftStartPoint = transform.position - transform.right * 0.3f; 113 Vector3 endPoint = transform.position - transform.up * 0.1f; 114 Vector3 rightStartPoint = transform.position + transform.right * 0.3f; 115 116 // 確認用 117 Debug.DrawLine(leftStartPoint, endPoint); 118 Debug.DrawLine(rightStartPoint, endPoint); 119 120 return Physics2D.Linecast(leftStartPoint, endPoint, blockLayer) || Physics2D.Linecast(rightStartPoint, endPoint, blockLayer); 121 } 122}

試したこと

空中ジャンプ関連のコードをコメントアウトすると正常に動くことを確認しました。

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

Unityのバージョンは2019.3.12です。

最終的には地面の上にいるときのジャンプと空中でのジャンプのモーションを分けようと思っているので接地判定を設けました。
しかし、その分岐の前を作る前にエラーが出てしまったために設置判定はまだ使われていません。

Unityでゲームを作り出してから間もない初心者なので分かりやすく教えていただけると嬉しいです。

※このコードをもとに改変して作成しています↓

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerManager : MonoBehaviour 6{ 7 [SerializeField] LayerMask blockLayer; 8 9 public enum DIRECTION_TYPE 10 { 11 STOP, 12 LEFT, 13 RIGHT, 14 } 15 DIRECTION_TYPE direction = DIRECTION_TYPE.STOP; 16 float speed; 17 Rigidbody2D rigidbody2D; 18 19 float jumpPower = 400; 20 21 void Start() 22 { 23 rigidbody2D = GetComponent<Rigidbody2D>(); 24 } 25 26 void Update() 27 { 28 float x = Input.GetAxis("Horizontal"); 29 30 if (x == 0) 31 { 32 direction = DIRECTION_TYPE.STOP; 33 } 34 else if (x > 0) 35 { 36 direction = DIRECTION_TYPE.RIGHT; 37 } 38 else if (x < 0) 39 { 40 direction = DIRECTION_TYPE.LEFT; 41 } 42 43 if (IsGround() && Input.GetKeyDown("space")) 44 { 45 Jump(); 46 } 47 } 48 void FixedUpdate() 49 { 50 switch (direction) 51 { 52 case DIRECTION_TYPE.STOP: 53 speed = 0; 54 break; 55 case DIRECTION_TYPE.RIGHT: 56 speed = 3; 57 transform.localScale = new Vector3(1, 1, 1); 58 break; 59 case DIRECTION_TYPE.LEFT: 60 speed = -3; 61 transform.localScale = new Vector3(-1, 1, 1); 62 break; 63 } 64 rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); 65 } 66 67 void Jump() 68 { 69 rigidbody2D.AddForce(Vector2.up * jumpPower); 70 } 71 72 bool IsGround() 73 { 74 Vector3 leftStartPoint = transform.position - transform.right * 0.3f; 75 Vector3 endPoint = transform.position - transform.up * 0.1f; 76 Vector3 rightStartPoint = transform.position + transform.right * 0.3f; 77 78 // 確認用 79 Debug.DrawLine(leftStartPoint, endPoint); 80 Debug.DrawLine(rightStartPoint, endPoint); 81 82 return Physics2D.Linecast(leftStartPoint, endPoint, blockLayer) || Physics2D.Linecast(rightStartPoint, endPoint, blockLayer); 83 } 84}

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

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

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

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

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

guest

回答2

0

ベストアンサー

中括弧が無い箇所があります。

RightJumpの関数を}で閉じて下さい。
画像の赤丸部分です。
イメージ説明

投稿2020/08/12 10:08

Hawn

総合スコア1222

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

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

TENNEKO

2020/08/12 10:40

エラーが直りました!ありがとございます!
TENNEKO

2020/08/12 11:32

追加で質問よろしいですか? rigidbodyへの力の加え方がいくつかあるとネットで調べたところ出てきたのですがrigidbody3D限定でしょうか?それとも、2Dにはそれ限定のやり方があるのでしょうか?試しに追加したらエラーはいたので……。 void Jump() { rigidbody2D.AddForce(Vector2.up * jumpPower, ForceMode.Impulse); } このサイトを見ました↓ https://www.f-sp.com/entry/2016/08/16/211214
Hawn

2020/08/12 16:48 編集

>rigidbody3D限定でしょうか?それとも、2Dにはそれ限定のやり方があるのでしょうか? 2Dにはそれ限定のやり方があります。 ForceMode2Dを使って下さい。 void Jump() {   rigidbody2D.AddForce( Vector2.up * jumpPower, ForceMode2D.Impulse ); }
TENNEKO

2020/08/13 23:58

親切にありがとうございました!
guest

0

google翻訳
プレイモードに入る前に、すべてのコンパイラエラーを修正する必要があります!

ということなんで、まずは出たコンパイルエラーのエラーメッセージを提示しましょう。
エラーメッセージはいらぬ翻訳、省略せず出たそのママを提示してください

投稿2020/08/12 07:35

y_waiwai

総合スコア87749

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

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

TENNEKO

2020/08/12 07:58

All compiler errors have to be fixed before you can enter playmode! UnityEditor.SceneView:ShowCompileErrorNotification() でした。すみません。
fiveHundred

2020/08/12 09:48

だから、その翻訳が「プレイモードに入る前に、すべてのコンパイラエラーを修正する必要があります!」なんですよ。 Consoleウィンドウに他のエラーが出ているはずなので、それを記載してください。
TENNEKO

2020/08/12 10:21 編集

Assets\Scripts\PlayerManager.cs(122,2): error CS1513: } expected ありました。すみません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問