前提・実現したいこと
色々なサイトを見て、C#でカメラと連動してプレイヤーを動かすプログラムを組んでいたのですが、
スペースキーで継続的に上昇させるコードを入れた所、スペースキーのみ反応しません。
発生している問題・エラーメッセージ
エラーは出ないのですが、実行中にspaceキーを押しても反応しません。
### 該当のソースコード public class PlayerController : MonoBehaviour { float inputHorizontal; float inputVertical; Rigidbody rb; float moveSpeed = 10; float runspeed = 15; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { inputHorizontal = Input.GetAxisRaw("Horizontal"); inputVertical = Input.GetAxisRaw("Vertical"); if (Input.GetKey(KeyCode.Space)) { Rigidbody rb = this.GetComponent<Rigidbody>(); // rigidbodyを取得 Vector3 force = new Vector3(0.0f, 1.0f, 0.0f); // 力を設定 rb.AddForce(force); // 力を加える } } 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 * moveSpeed + new Vector3(0, rb.velocity.y, 0); // キャラクターの向きを進行方向に if (moveForward != Vector3.zero) { transform.rotation = Quaternion.LookRotation(moveForward); } } } 言語 C# ### 試したこと ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー if (Input.GetKey(KeyCode.Space)) { Rigidbody rb = this.GetComponent<Rigidbody>(); // rigidbodyを取得 Vector3 force = new Vector3(0.0f, 1.0f, 0.0f); // 力を設定 rb.AddForce(force); // 力を加える } ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー ↑ ・この文をvoid FixedUpdateのなかに入れてみましたが反応しないです。 Unity Ver 2018.2.1f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/07 09:19