Tutorials

Creating a flashlight GameObject in Unity3D

I’ve been playing around with Unity 3D lately, and I put together a short tutorial video about some of what I’ve learned. Enjoy!

Here’s the code used in the video:

[code language=”csharp”]
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Light))]
public class Flashlight : MonoBehaviour {
public GameObject source;
public AudioClip toggleSound;
Light _light;

// Use this for initialization
void Start () {
_light = GetComponent<Light>();
}

// Update is called once per frame
void Update () {
if(Input.GetButtonDown ("Flashlight")) {
_light.enabled = !_light.enabled;
AudioSource.PlayClipAtPoint(toggleSound, transform.position);
}

transform.Rotate(source.transform.rotation.eulerAngles – transform.rotation.eulerAngles);
}
}
[/code]

Leave a Reply