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

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

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

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

Unity

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

Q&A

0回答

759閲覧

AddForceを使ってまっすぐ前に飛ばしたい

pokkuru

総合スコア8

C#

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

Unity

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

0グッド

0クリップ

投稿2021/06/01 03:57

前提・実現したいこと

AddForceを使ってオブジェクトを真っ直ぐに飛ばしたいです。
y軸には動いてくれますがx軸には動いてくれないのでそれをなおしたいです。
該当のソースコードには実験のため変数yを5としていますが最終的には0にします。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class player2d : MonoBehaviour 7{ 8 public float speed; 9 public float normalSpeed = 10; 10 public float shiftSpeed = 15; 11 public float normalJump = 450; 12 public float shiftJump = 540; 13 private bool jumpUpEnd = false; 14 [SerializeField]public float jumpTime; 15 private Rigidbody2D rb; 16 public float jumpForce; 17 public float jumpHeight; 18 public float jumpLimitTime; 19 bool jump = false; 20 bool isCrouch = false; 21 float x; 22 float y; 23 bool runJumpEnd = false; 24 Animator animator; 25 public ContactFilter2D contact; 26 27 Vector2 runJumpVector; 28 29 30 // Start is called before the first frame update 31 void Start() 32 { 33 rb = GetComponent<Rigidbody2D>(); 34 animator = GetComponent<Animator>(); 35 contact = GetComponent<ContactFilter2D>(); 36 } 37 38 // Update is called once per frame 39 private void Update() 40 { 41 bool isTouch = rb.IsTouching(contact); 42 Vector2 liscale = gameObject.transform.localScale; 43 44 if (gameObject.transform.position.y < -20) 45 { 46 int sceneIndex = SceneManager.GetActiveScene().buildIndex; 47 SceneManager.LoadScene(sceneIndex); 48 } 49 50 if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 51 { 52 speed = shiftSpeed; 53 jumpForce = shiftJump; 54 } 55 else 56 { 57 speed = normalSpeed; 58 jumpForce = normalJump; 59 } 60 61 if (Input.GetButton("Jump") && !jumpUpEnd && jumpTime < 0.2f) 62 { 63 64 jumpTime += Time.deltaTime; 65 66 if (jumpTime >= 0.2f) 67 { 68 jumpTime = 0.2f; 69 } 70 rb.velocity = new Vector2(rb.velocity.x * speed, jumpForce); 71 jump = true; 72 } 73 74 if (Input.GetButtonUp("Jump") && !jumpUpEnd) 75 { 76 //二回ジャンプできなくする 77 jumpUpEnd = true; 78 } 79 80 if (isTouch) 81 { 82 jumpTime = 0.0f; 83 rb.velocity = new Vector2(rb.velocity.x * speed, rb.velocity.y); 84 runJumpEnd = false; 85 } 86 87 if(isTouch && Input.GetKey(KeyCode.DownArrow)) 88 { 89 animator.SetInteger("CharaState", 4); 90 GetComponent<BoxCollider2D>().enabled = false; 91 isCrouch = true; 92 } 93 94 if(Input.GetKeyUp(KeyCode.DownArrow) || !isTouch) 95 { 96 GetComponent<BoxCollider2D>().enabled = true; 97 } 98 99 if(Input.GetKeyUp(KeyCode.DownArrow) && isTouch) 100 { 101 animator.SetInteger("CharaState", 0); 102 } 103 104 if(Input.GetKeyUp(KeyCode.DownArrow)) 105 { 106 isCrouch = false; 107 } 108 109 //ダッシュジャンプ処理 110 111 if (Input.GetKeyDown(KeyCode.V) && Input.GetKey(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.V) && liscale.x > 0 && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow) && !runJumpEnd) 112 { 113 x = 40; 114 y = 5; 115 runJumpVector = new Vector2(x,y); 116 rb.AddForce(runJumpVector, ForceMode2D.Impulse); 117 runJumpEnd = true; 118 } 119 } 120 121 private void OnCollisionEnter2D(Collision2D other) 122 { 123 if (other.gameObject.CompareTag("Ground")) 124 { 125 jumpUpEnd = false; 126 jump = false; 127 animator.SetInteger("CharaState", 0); 128 } 129 130 if (other.gameObject.CompareTag("moveground")) 131 { 132 jumpUpEnd = false; 133 jump = false; 134 animator.SetInteger("CharaState", 0); 135 } 136 137 if(other.gameObject.CompareTag("spike")) 138 { 139 int sceneIndex = SceneManager.GetActiveScene().buildIndex; 140 SceneManager.LoadScene(sceneIndex); 141 } 142 } 143 144 145 private void OnCollisionExit2D(Collision2D other) 146 { 147 if (other.gameObject.CompareTag("Ground")) 148 { 149 jump = true; 150 } 151 152 if (other.gameObject.CompareTag("moveground")) 153 { 154 jump = true; 155 } 156 } 157 158 void FixedUpdate() 159 { 160 bool isTouch = rb.IsTouching(contact); 161 float horizontalKey = Input.GetAxis("Horizontal"); 162 163 if ((horizontalKey <= 0.01 && horizontalKey >= -0.01 && jump == false) || Input.GetKeyUp(KeyCode.DownArrow) && jump == false || Input.GetKeyUp(KeyCode.UpArrow) && jump == false || Input.GetKeyUp(KeyCode.RightArrow) && jump == false || Input.GetKeyUp(KeyCode.LeftArrow) && jump == false) 164 { 165 if (!Input.GetKey(KeyCode.V)) 166 { 167 rb.velocity = Vector2.zero; 168 } 169 } 170 171 if(Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.LeftArrow) && jump == false) 172 { 173 rb.velocity = Vector2.zero; 174 } 175 176 if (jump == true) 177 { 178 animator.SetInteger("CharaState", 3); 179 } 180 181 if (horizontalKey != 0 && jump == true) 182 { 183 animator.SetInteger("CharaState", 3); 184 } 185 else if (horizontalKey != 0) 186 { 187 188 189 Vector2 liscale = gameObject.transform.localScale; 190 191 if ((liscale.x > 0 && horizontalKey < 0) || (liscale.x < 0 && horizontalKey > 0)) 192 { 193 liscale.x *= -1; 194 gameObject.transform.localScale = liscale; 195 } 196 animator.SetInteger("CharaState", 1); 197 } 198 else if (jump == true) 199 { 200 animator.SetInteger("CharaState", 3); 201 } 202 else 203 { 204 animator.SetInteger("CharaState", 0); 205 } 206 207 rb.velocity = new Vector2(horizontalKey * speed, rb.velocity.y); 208 209 if(jump == true) 210 { 211 rb.velocity = new Vector2(horizontalKey * speed, rb.velocity.y); 212 213 } 214 if(isTouch && rb.velocity.x > 6) 215 { 216 rb.velocity = new Vector2(6, rb.velocity.y); 217 } 218 219 if (isTouch && rb.velocity.x < -6) 220 { 221 rb.velocity = new Vector2(-6, rb.velocity.y); 222 } 223 224 if(!isTouch && rb.velocity.x > 4) 225 { 226 rb.velocity = new Vector2(4, rb.velocity.y); 227 } 228 229 if (!isTouch && rb.velocity.x < -4) 230 { 231 rb.velocity = new Vector2(-4, rb.velocity.y); 232 } 233 234 if(isCrouch && rb.velocity.x > 0) 235 { 236 rb.velocity = new Vector2(0, rb.velocity.y); 237 } 238 239 if (isCrouch && rb.velocity.x < 0) 240 { 241 rb.velocity = new Vector2(0, rb.velocity.y); 242 } 243 244 245 246 //Debug.Log(horizontalKey); 247 } 248 249 250} 251

試したこと

C#

1if (Input.GetKeyDown(KeyCode.V) && Input.GetKey(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.V) && liscale.x > 0 && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow) && !runJumpEnd) 2 { 3 x = 40; 4 y = 5; 5 runJumpVector = new Vector2(x,y); 6 rb.AddForce(runJumpVector, ForceMode2D.Impulse); 7 runJumpEnd = true; 8 }

上の部分が作動しているかをDebug.Logを使って調べたところ、作動はしていることがわかりました。

補足情報

主に直したい部分はソースコードの //ダッシュジャンプ処理 と書いてあるところあたりです。

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

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

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

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

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

bboydaisuke

2022/03/01 14:21

長いソースコードを見せられても、何をしようとしているのか説明がなければ他の人には何をしているのかわからないものです。特に正しく動いていないものは。 なので、何をしようとしているのかの説明が必要です。 例) - 2D プラットフォーマーを作ろうとしている - 左右キーで左右に歩く - スペースでジャンプする - スペースを長く押しているとジャンプが高くなる、短く押すと低くジャンプする - 下キーでしゃがむ - Shift キーでダッシュ ...等々 コードを見て、上キーと V キーが何をするものなのか理解するまでに限界を迎えました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問