LoginSignup
3
7

More than 5 years have passed since last update.

[Unity]コイン発射(2Dランチャー)

Last updated at Posted at 2017-05-29

概要

Unityでランチャーからゲームオブジェクトを発射させる方法
デモ動画.gif

こんな感じでコインを発射させるスクリプトを書いてみました。
https://github.com/katsuma99/Launcher/tree/master

LaunchUtil.cs
using UnityEngine;

public static class LaunchUtils
{
    public static void LaunchItem(ref Transform item, float power, Vector2 vector, float rotation = 0, float torquePower = 0)
    {
        //発射方向を設定
        Vector2 launchVector = Quaternion.Euler(0, 0, rotation) * vector.normalized;

        //Rigidbody取り出す
        Rigidbody2D r = item.gameObject.GetComponent<Rigidbody2D>();
        if (r == null)
        {
            r = item.gameObject.AddComponent<Rigidbody2D>();
        }

        //力を加える
        r.AddForce(launchVector * power, ForceMode2D.Impulse);

        //回転を加える
        r.AddTorque(torquePower, ForceMode2D.Impulse);
    }
}

解説

LaunchItem()の引数にオブジェクトとパラメータを渡すことで力を加えて発射します。

項目 意味
item 発射するオブジェクト ref Transform
power 発射する力 float
vector 発射する方向 Vector2
rotation   vector方向を基準に回転する角度 float
torquePower オブジェクトを回転させる力 float

処理の流れ

 1. 発射する方向を設定
 2. Rigidbody2Dを追加
 3. 発射する力と回転の設定

Step1.発射する方向を設定

Vector2 launchVector = Quaternion.Euler(0, 0, rotation) * vector.normalized;

vector : 発射する方向
power : 発射する力
normalized : 単位ベクトル
 vectorは長さを見ないで、powerで力を決定する。

Quaternion.Euler(0,0,R) : Z軸にR分回転する
 方向を決定して、そこから回転して微調整する。
 方向は変更せずに、回転をいじることでランダムな向きに発射できる。

Step2.Rigidbody2Dを追加

//Rigidbody取り出す
Rigidbody2D r = item.gameObject.GetComponent<Rigidbody2D>();
if (r == null)
{
    r = item.gameObject.AddComponent<Rigidbody2D>();
}

AddComponent() : gameObjectにコンポーネント(ここではRigidbody2D)追加
 もしRigidbodyを元から設定されてる場合は追加せずに、GetComponent()で取得する。

Step3.発射する力と回転の設定

//力を加える
r.AddForce(launchVector * power, ForceMode2D.Impulse);

//回転を加える
r.AddTorque(torquePower, ForceMode2D.Impulse);

AddForce() : 力を加える
AddTorque() : 回転を加える
 加る方法は、ForceModeで設定する。

モード 効果
Force 連続的な力 投げる
Impulse 瞬間的な力 爆発

ランチャーの作り方

ランチャーの作成.PNG

エディタからプロパティをいじるれるランチャーの作り方
 1. ランチャーにしたいゲームオブジェクトに以下のスクリプトを追加する
 2. LaunchUtil.csをプロジェクト内に入れる

Launcher.cs
using UnityEngine;

public class Launcher : MonoBehaviour
{
    public GameObject item;

    [SerializeField, Range(0, 10)]
    public int power = 1;

    [SerializeField, Range(-90, 90)]
    public int rotation = 0;

    [SerializeField, Range(-90, 90)]
    public float torquePower = 0;

    public void Fire()
    {
        Transform newItem = Object.Instantiate(item, transform.position, Quaternion.identity, transform).transform;
        LaunchUtil.LaunchItem(ref newItem, power, transform.up, rotation, torquePower);
    }

}

Fire() : コイン発射
 コイン以外にも、ランチャープロパティのItemにゲームオブジェクトを指定すると好きなものを発射できる。

一定間隔でコイン発射

一定間隔でコインが発射されるように作ってみました。
 1. 空のゲームオブジェクトLauncherManagerを作成しする
 2. LauncherManagerに以下のスクリプトを追加する
 3. LauncherManagerの子に作成したランチャーを追加する

LauncherManager.cs
using UnityEngine;

public class LauncherManager : MonoBehaviour
{
    [SerializeField, Range(0, 10)]
    public float mTimeOut = 1;

    public int mItemCountMax = 10;
    float mTimeElapsed = 0;

    void Update()
    {
        mTimeElapsed += Time.deltaTime;
        if (mTimeElapsed >= mTimeOut)
        {
            foreach (Transform launcher in transform)
            {
                launcher.GetComponent<Launcher>().Fire();
                if (launcher.childCount > mItemCountMax)
                {
                    Destroy(launcher.GetChild(0).gameObject);
                }
            }
            mTimeElapsed = 0.0f;
        }
    }
}

mTimeOut : 発射する間隔

ゲームを作ってみた

使用例.gif

  • 色々なオブジェクトを色々なランチャーから発射する
  • 発射する方向をランダムに変える
  • オブジェクトをタップできるようにする

 android → https://play.google.com/store/apps/details?id=com.secretcommandlab.taptapdrop
  iOS → https://appsto.re/jp/fMfpjb.i

参考資料

3
7
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
3
7