Q&A
前提・実現したいこと
初めてのUnityでアクションゲームを制作途中です。
より自由度の高いものを作りたいと考え、ジャンプを実装しました。
実装したいものは、自然なジャンプと落下です。
ボタンを押す長さでジャンプの長さが変わり、離すと落下する、というものです。
ジャンプの長さをインスペクタで調整できるようになればなお嬉しいです。
全ての問題に対してではなく、一つ一つの問題に対してだけでも回答をいただけるとすごくうれしいです。
発生している問題というより調べても分からなかった事でありますが、よろしくお願いします。
発生している問題・エラーメッセージ
①ジャンプ自体は機能していますが、ジャンプボタンを連打すれば空中でも更にジャンプ出来てしまう。
②壁にぶつかっている状態で走りながらジャンプすると、アニメーションは再生されているが、上方向に飛べていない。
③ジャンプした後すぐに移動すると、空中で移動ができてしまう。(落下しない)
④ジャンプの長さをインスペクタ上で変えれるようにするにはどうすればいいのか。
該当のソースコード
現時点でのコードです。言語はC♯です。
using System; using UnityEngine; using UnityEngine.UI; // プレイヤー public class Player : MonoBehaviour { [SerializeField] private Vector3 velocity; // 移動方向 [SerializeField] private float walkSpeed; // 移動速度 public GameObject cameraRootObj; float inputHorizontal; float inputVertical; Rigidbody rb; private Animator animator; public float jumpTime = 1f; float time; public float height = 2f; float fixedGravity; float initVelocity; float elapsedTime = 0f; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { int x = 0; //移動関連 // WASD入力から、XZ平面(水平な地面)を移動する方向(velocity)を得ます velocity = Vector3.zero; if (Input.GetKey(KeyCode.W)) { x++; velocity += new Vector3(cameraRootObj.transform.forward.x, 0, cameraRootObj.transform.forward.z); GetComponent<Animator>().SetBool("walk", true); } if (Input.GetKey(KeyCode.A)) { x++; velocity += new Vector3(-cameraRootObj.transform.right.x, 0, -cameraRootObj.transform.right.z); GetComponent<Animator>().SetBool("walk", true); } if (Input.GetKey(KeyCode.S)) { x++; velocity += new Vector3(-cameraRootObj.transform.forward.x, 0, -cameraRootObj.transform.forward.z); GetComponent<Animator>().SetBool("walk", true); } if (Input.GetKey(KeyCode.D)) { x++; velocity += new Vector3(cameraRootObj.transform.right.x, 0, cameraRootObj.transform.right.z); GetComponent<Animator>().SetBool("walk", true); } if (x == 0) { GetComponent<Animator>().SetBool("walk", false); } // 速度ベクトルの長さを1秒でmoveSpeedだけ進むように調整します velocity = velocity.normalized * walkSpeed * Time.deltaTime; // いずれかの方向に移動している場合 if (velocity.magnitude > 0) { // プレイヤーの位置(transform.position)の更新 // 移動方向ベクトル(velocity)を足し込みます transform.position += velocity; } // いずれかの方向に移動している場合 if (velocity.magnitude > 0) { // プレイヤーの回転(transform.rotation)の更新 // 無回転状態のプレイヤーのZ+方向(後頭部)を、移動の方向(velocity)に回す回転とします transform.rotation = Quaternion.LookRotation(velocity); // プレイヤーの位置(transform.position)の更新 // 移動方向ベクトル(velocity)を足し込みます transform.position += velocity; } inputHorizontal = Input.GetAxisRaw("Horizontal"); inputVertical = Input.GetAxisRaw("Vertical"); velocity.y += Physics.gravity.y * Time.deltaTime; int j = 0; //ジャンプボタンの実装 // マウスがクリックされた場合 if (Input.GetKey(KeyCode.C)) { time = jumpTime / 2; fixedGravity = -2 * height / (time * time);//① initVelocity = 2 * height / time;//② j++; //Animatorコンポーネントを取得し、"jumpTrigger"をtrueにする GetComponent<Animator>().SetBool("jump", true); } if (j == 0) { GetComponent<Animator>().SetBool("jump", false); } } void FixedUpdate() { //カメラの方向から、X-Z平面の単位ベクトルを取得 Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; //方向キーの入力値とカメラの向きから、移動方向を決定 Vector3 moveForward = cameraForward * inputVertical + Camera.main.transform.right * inputHorizontal; //移動方向にスピードを掛ける。ジャンプや落下がある場合は、別途Y軸方向の速度ベクトルを足す。 rb.velocity = moveForward * walkSpeed + new Vector3(0, rb.velocity.y, 0); // キャラクターの向きを進行方向に if (moveForward != Vector3.zero) { transform.rotation = Quaternion.LookRotation(moveForward); } } }
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。