実現したいこと
UnityのC#でプレイヤーにスペースキーでジャンプをさせようとしたが、ジャンプをしなかった
発生している問題・分からないこと
unity上ではエラーメッセージがでてこない
エラーメッセージ
error
1エラーメッセージなし
該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { Rigidbody2D rbody; //Rigidbody2D型の変数 float axisH = 0.0f; //入力 public float speed = 3.0f; //移動速度 public float jump = 9.0f; //ジャンプ力 public LayerMask groundLayer; //着地できるレイヤー bool goJump = false; //ジャンプ開始 // Start is called before the first frame update void Start() { // Rigidbody2Dを取ってくる rbody = this.GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { //水平方向の入力をチェックする axisH = Input.GetAxisRaw("Horizontal"); //向きの調整 if(axisH > 0.0f) { //右移動 Debug.Log("右移動"); transform.localScale = new Vector2(0.15f, 0.15f); } else if (axisH < 0.0f) { //左移動 Debug.Log("左移動"); transform.localScale = new Vector2(-0.15f, 0.15f);//左右反転させる } //キャラクターをジャンプさせる if (Input.GetButtonDown("Jump")) { Jump(); } } void FixedUpdate() { //地上判定 bool onGround = Physics2D.CircleCast(transform.position, 0.2f, Vector2.down, 0.0f, groundLayer); if(onGround || axisH != 0) { //地面の上or速度が0ではない //速度を更新する rbody.velocity = new Vector2(axisH * 3.0f, rbody.velocity.y); } if(onGround && goJump) { //地面の上でジャンプキーが押された //ジャンプさせる Vector2 jumpPw = new Vector2(0,jump); //ジャンプさせるベクトルを作る rbody.AddForce(jumpPw, ForceMode2D.Impulse); //瞬発的な力を加える goJump = false; //ジャンプフラグをおろす } } //ジャンプ public void Jump() { goJump = true; //ジャンプフラグを立てる } }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
検索しましたがでてきませんでした
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/01/09 05:41