Unity3D Tutorial - Creating a Flashlight

in #utopian-io7 years ago (edited)

Quick Information

Difficult: Beginner Normal Hard
Requirements: Unity3D, Standard Assets

Quick Guide

We open our Unity Envioment with Standart Assets.

  1. We create a Cube as Floor.

  2. We resize the Cube

  3. We put a Player Prefab to the Floor.

  4. We add a "Spotlight".

  5. We put the "Spotlight" to our Camera Object and klick up-right "reset".

  6. We add our Code.

  7. We run it

Code

Make sure that your created C# Script has the name Flashlight!


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

//Created by Platur

public class Flashlight : MonoBehaviour {

//Our Lightsource
public Light flashlightSource;
//Our state
private bool state;

// Use this for initialization
void Start () {
    //We get the Light of the Gameobject where the script are
    flashlightSource = this.GetComponent<Light>();

    //We change our state to the state of the Light Compnonent
    state = flashlightSource.enabled;
    
}

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

    //If we Press Keycode F we change the state
    if (Input.GetKeyUp(KeyCode.F)) {
        //How is our state?
        if (state == true)
        {
            //Its on so we disable the Light
            flashlightSource.enabled = false;
            state = false;
        }
        else
        {
            //Its off so we enable the Light
            flashlightSource.enabled = true;
            state = true;
        }
    }
}

}


Do you like what i write?

I would like to have a new Follower and maybe a upvote :)

Sort:  

I just resteemed your post to over 36,000 followers. Follow @a-0-0 if you want me to resteem more of your posts.

interesting to learn friends

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 98646.90
ETH 3511.62
USDT 1.00
SBD 2.98