Unity3D Tutorial - Creating a Flashlight
Quick Information
Difficult: Beginner Normal Hard
Requirements: Unity3D, Standard Assets
Quick Guide
We open our Unity Envioment with Standart Assets.
We create a Cube as Floor.
We resize the Cube
We put a Player Prefab to the Floor.
We add a "Spotlight".
We put the "Spotlight" to our Camera Object and klick up-right "reset".
We add our Code.
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 :)
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