Make A Light Switch Or Button In Unity3D 2021 (script below)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys i'm going to show you how to make a light switch or a simple button first you're going to create 3d game object cube scale this down this is going to represent our button this is our main object that our scripts are one script and our components will be attached to so i'm gonna make a background just for cosmetic reasons now it looks more like a button go to our main object i'm going to call this button drag that here i'm going to edit the box collider i'm going to extend this out this is our zone that our player needs to be in in order to activate the button click is trigger all right so now we need to create our script right click create c sharp script i already made the script so i'm going to go over it i named it light on off i'm gonna also provide it in the comments below public game object this is our text that we're gonna display on our screen it's gonna say use or whatever just gonna check if the players in our trigger this is our light or any object that we're going to activate when the scene starts it's going to check if our player is in the zone every update it's going to check if the player is in the zone pressing f and if it does it's going to activate our light it's going to activate our audio source it's going to activate the animation in our animator controller on trigger enter when the player is in the zone only the player it's going to display the text check if the player is in the zone exit it's going to remove the text it's going to check if the player is in the zone simple script so now so now we got to go to our button we're going to drag that script in we're going to add an audio source this will be like a click or just a button noise check play on awake we don't need that drag an mp3 file or whatever all right so now we're going to create a canvas it could be any canvas doesn't matter right click ui canvas right click on the canvas ui text this will be our text that will be shown i'm going to write use i'm going to make it white so we can see it better maybe now we're going to deactivate the text go to our button we're going to drag that text into our text to display so now we need to create our object that we're going to activate i'm going to use a spotlight put it right behind the button all right so we're going to deactivate this object and then we're going to go to our button object and we're going to drag that in our light or obj all right so now go to our button i'm going to go to our animation tab create i'll call it button i'm gonna click record and go to our one second mark i'm going to push the button inward go to our first key control c to copy can go to our second mark v so now we'll play a nice smooth animation looks like a buttons being pushed stop recording now when we did that it should have automatically made an animator component so we're gonna go down here go to our controller i'm gonna delete this i'm gonna call it idle no motion nothing right click create new state so in our script we called it switch so we have to name this switch right click transition to idle the animation clip we just made i called the button so i'm going to put this here so now everything should be working how it should so we're going to go test it [Music] all right so i go near the button it displays our text when i exit it doesn't show it so if i should press f it's going to display our light and it plays our animation and our sound effect i'm going to show you guys a few examples in my game next so this is my game that i'm working on it's a horror game set in the antarctic i'm just going to show you guys a few examples of what this script can do got a basic light switch here maybe you want to turn on a radio all it is is activating and deactivating a game object so you could have an empty game object with an audio source and you can drag that right into this script got more light switches it also doesn't have to be just one light it could be multiple all you have to do is group it up together in one single prefab and you can turn on multiple lights these lights are off you got a light switch over here i turned on all lights with one switch so i hope this tutorial helped you guys out if it did please subscribe leave a comment down below and take care
Info
Channel: Big Bird Games
Views: 1,946
Rating: undefined out of 5
Keywords: light, switch, button, interactive, unity, make, how, to, big, bird, games, game, create, lightswitch, trigger, tutorial, example, coding, maker, creator, fps, 3d, easy, simple, short, bigbirdgames, bigbird, 2021, new, editor, fast
Id: fnqTu7Mhr40
Channel Id: undefined
Length: 7min 31sec (451 seconds)
Published: Thu Mar 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.