0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

海外のフリー2Dゲーでよく目にする様な銃のリロード機能の実装(Unity C# )

Last updated at Posted at 2025-02-27

リロードの機能をまあカンタンやろ(ハナホジ)とか思いながら書いてたら、丸々一日費やす羽目になったので投稿しようと思い至りました。

追記:すみません、参考にさせて頂いたサイトがあったのですが、載せていませんでした。
参考にさせて頂いたサイト:https://qiita.com/toRisouP/items/e6d4f114d434ee588044

銃本体のスクリプト

private int InsTimer = 0;       //弾を呼び出す間隔

public GameObject InsTarget;  //呼び出す弾オブジェクト
public GameObject AngleSource; //弾がVector3.upなため、プレイヤーオブジェクトの角度を元に生成

private int FireRate = 0;        //発射速度(N発毎秒)

private float ReloadTime = 0;   //リロード時間(小数でも指定できる)

private int BulletCount = 0;     //ゲーム内で実際に変動する残弾数

private int BulletCap = 0;    //弾が最大何発か

private bool FireReady = true;  //発射可能にするかを指定

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    Application.targetFrameRate = 60; //別にマネージャーを作ってフレームレートを設定した方が良い

    InsTimer++;

    float Angle_Y = AngleSource.transform.localEulerAngles.y; //プレイヤーの角度を取得して 弾生成時の角度を指定



    if (FireRate > 0 && FireReady == true && BulletCount >= 1)
    {
        if (InsTimer >= 60 / FireRate && Input.GetKey(KeyCode.Space))
        {

            Instantiate(InsTarget, transform.position, Quaternion.Euler(0, Angle_Y, 0));

            BulletCount -= 1;

            InsTimer = 0;

        }
    }   //射撃

    //リロード

    if (Input.GetKeyUp(KeyCode.R) && FireReady == true)
    {

        FireReady = false;
        Invoke(nameof(Reload), ReloadTime);
    }
}


private void Reload()
{
    BulletCount = BulletCap;
    FireReady = true;      
}  //リロード時に呼び出されるコルーチン


public void SetWeaponNoOnClick1()
{
    FireRate = 2;
    ReloadTime = 2f;
    BulletCount = 11;
    BulletCap = BulletCount;
} //武器の情報(ボタンをクリックしたときなどのイベント時に記録)

弾の移動 

public float MoveSpeed = 50f; //移動速度

public int LifeTime = 120;  //消去までの時間

private int counter = 0;   //カウンター

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void FixedUpdate()
{
    counter++;

    transform.Translate(Vector2.right*MoveSpeed*Time.deltaTime);

    if(counter>=LifeTime)
    {
        Destroy(gameObject);    
    }

}

スクリプティング以外にやること

1.銃オブジェクトとプレイヤーオブジェクトと弾オブジェクトを用意。
2.プレイヤーの子供として銃オブジェクトを配置。
3.銃オブジェクトに銃本体のスクリプトをアタッチ。
4.弾オブジェクトに弾の移動スクリプトをアタッチ。
5.弾をプレハブ化。
6.銃オブジェクトにアタッチされたスクリプトのInsTargetにプレハブ化した弾、AngleSourceにプレイヤーオブジェクトをアタッチ。
7.ボタンオブジェクトを配置。
8.ボタンのNavigationをnoneに設定。(スペースに勝手に反応するため。)
9.ボタンのOnClick()を追加し、銃オブジェクトをアタッチしてからSetWeaponNoOnClick1()を選択。
10.プレイしてみて、ボタンを押した後でスペースを押した時に弾が出るか確認する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?