LoginSignup
1
1

More than 3 years have passed since last update.

Unityゲーム制作 ~FlashLightの紹介~

Last updated at Posted at 2020-01-15

---ゲーム内で使用するライトのON/OFFのコード---

Zキーを押している間だけライトを消すシステムを今回はobjectを消すという手法で行いました。

まずUnityのHierarchyに、ライト本体を入れなくてはいけません

Asset storeからライト本体を探してきて(Flashlight)ダウンロードします。
https://assetstore.unity.com/packages/3d/props/electronics/flashlight-18972
7.png

そのあと、本体の中に光の部分(自分の場合 UnityEffectのSpot Light)を使って作っておきます。
1.png

こんな感じ
無題.png

次にこのコードを打ち込んで保存します。

GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{

    public GameObject Sphere;
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Z))
        {

            Sphere.SetActive(true);

        }

        if (Input.GetKeyUp(KeyCode.Z))
        {

            Sphere.SetActive(false);

        }

    }
}

その後、HierarchyでCreate→Create Emptyを作成し名前をGameManagerとし、先ほど作ったScriptをアタッチしてください。

2.png
そして、ライトの光の部分(自分の場合Spot Light)のobjectをSphereにドラック&ドロップするだけで完成します。

カメラの位置を調整・ゲームをスタートし、Zキーを押すとライトの光がつくと思います、離すと消えます。

以上ライトのON/OFFのスクリプト導入でした。お疲れさまです!!('ω')ノ

1
1
3

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