###前提・実現したいこと
現在unityを用いて2Dシューティングの製作に挑戦しています。
その中で自機の製作を現在しているのですがそこで不明なことがあり質問させていただきました。
自分はまだまだunityでの製作経験はほとんどなく、本やさまざまなサイトを参考させていただきながらプログラムを書いているようなレベルです。
今回お聞きしたいのは自機の移動、そして移動制限のかけ方です。
具体的には自機の移動可能な方向は左右のみ、そして画面から出ないようにすることです。
###発生している問題・エラーメッセージ
自機の移動の際移動できる方向を上下左右ではなく左右のみ移動可能にしようとしていたのですが自分で考えて書き換えながら起動してみたらunity側でエラーが吐かれてしまい起動はしても自機が動いてくれないという状況になってしまいました。
以下はunityに表示されたエラー内容です。
MissingComponentException: There is no 'Rigidbody2D' attached to the "player" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "player". Or your script needs to check if the component is attached before using it. UnityEngine.Rigidbody2D.set_velocity (Vector2 value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Physics2D/Physics2DBindings.gen.cs:1209) player.Update () (at Assets/player.cs:14)
###該当のソースコード
c#
1using UnityEngine; 2using System.Collections; 3 4public class player : MonoBehaviour 5{ 6 7 public float speed = 5; 8 void Update() 9 { 10 float x = Input.GetAxisRaw("Horizontal"); 11 float y = 0; 12 Vector2 direction = new Vector2(x, y).normalized; 13 14 GetComponent<Rigidbody2D>().velocity = direction * speed; 15 } 16 17 18}
###試したこと
サイトを参考に見ながら作ったために自分の用意した素材と名前が違うのではないかと思い変更してみたが駄目だった。
###補足情報(言語/FW/ツール等のバージョンなど)
言語はunityなのでc#
unityのverは5.3.5f1です
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。