Learn how to code the day and night toggle function and connect the button up.
working so that we can finish off this little section. Now we've got daytime and nighttime, and we can see that they buttons 7 and 8 on the element list, so that's what we need to detect. Let's have a quick grab, daytime here, let's steal you for a second, and jump back across into the script. So, this time we're going to do a quick check daytime and then, if it is, we just need to do the same kind of thing. So, grab the virtual button's game object and set it to be inactive, like so. And then, obviously, we need to check from the "My Buttons" that number 8 is, in fact, active, just in case we're toggling through. Oh that's an odd way of spelling it. There we go, guys. And then we're going to go into the house script, into an instance and we're going to call a function, which we haven't created yet, called "Toggle Day and Night", like so, with a Y, if I can grab a Y, there we go. Okay, so all we have to do now is, actually, create this little function. So, if I come down here, let's create a public void, called "Toggle Day and Night", and what I'm going to do, is I'm going to come up to the top, and I'm going to set a private Boolean up at the top. Oops, like so, and I'm going to call this "Daytime", and basically, this will tell us if it's daytime or not, now it starts off being daytime, there we go. So, that's what I need to detect. And thinking about it, I've just realized that I've got these buttons the wrong way around, because we want to hit nighttime, as it's daytime first. And then hit daytime up at the top to actually put the light back. So I'm just going to move these around for a second if I just swap these buttons, here we go, whoa. And I'm going to move that up, over the top, and then grab the nighttime plane, and bring you down, there we go, okay. So now, because it's daytime first, you see. I want to hit the button to change it to night, and then be able to reset it back to day. So, I've just realized I've got it the wrong way round. Okay, so, if it is daytime, then now we can at least detect that. So we can check if that Boolean So that's what we're going to do, we're going to say "if daytime is true, then obviously won't want to change it to be nighttime, else we want this fore loop here and paste this is here. Change second floor for the lights instead, look. Okay so that will turn off all the lights, like so, although we don't actually need the else, all I want to do is basically loop through the lights and set them all to be turned off, because we are going into night. Now, the next thing I had a little bit of a think of exactly how I wanted to do this. Now, to make this look like nighttime, we've got the directional light here. Now the first thing we could do, is just simply turn the light off. But that darkens the buttons a little bit too much, and I don't really want to do that. So, what I thought about doing instead, was grabbing hold of the directional light's light component and going after the color, and making it kind of a dark-gray, nighttime feel, and then after having a little bit of a play, if we kind of went for a really dark blue, to give it a little bit of an atmosphere. So, if we have a look down here, look, we've got the RGBA, so that's a red, green, blue and the alpha, which is the transparency. Now, if I go after 11, 24 and 46, I can dynamically change this in the code. So, let's jump in and let's alter the directional light, so, there's a directional light, let's go and grab a hold of the component which is the actual light attached to it, now once we've got that, we can set the color to whatever we want, by just simply saying that the color is now, I don't know, gray for example, now what that will do is obviously change the color of the light to be gray. But I want to be a little bit more creative, so what we're going to do instead is we're going to go after something called a "color 32", now, a "color 32" has the RGB and alpha channels attached. So it's 11, 23, and 46, that we're actually put in as values. 11, 26 and 43. And the last one is the alpha, now 255 is fully opaque, basically. So, double check, 11, 23, 46. 11, 23, 46, got it completely wrong. But there we go, it's cause I'm thinking and talking at the same time, guys. So, now, what we're doing now is we're toggling between day and night, if it's already day time, then I want you to switch all the light on, in fact, not off, and switch the color of the directional light to be kind of a dark, bluey-purple color. Then, what we want to do is actually get our daytime Boolean from the top, set that to be false, because we're now in nighttime. Okay, so, if we are now in nighttime, if I grab a hold of this and paste it underneath, moving you back again, I'll have to sort this reformatting out, guys, it's frustrating me now. Then all we have to do is turn the lights back off, I mean you can leave them on if you want, but turn the lights back off and change the color to be white again, which is actually 255 by 255 by 255, and that will alow us now to toggle between the day and night cycles! Okay, so let's hookup the final virtual button, and basically give this a bit of a run. I'll copy the name just so that I don't make a typo, there we go, and this time we want to get rid of number 7, and deactivate this one. Okay, cool, so in theory now we should be able to toggle between a day and night cycle, if I just reset my directional light back to being white, so we're already in day, I'll give it a quick run and let's see if these buttons work now. So, here are the buttons and we can see that it's kind of daytime now, there we go, now if I change to be nighttime, there we go, look! The lights are on and it's changed to blue, which is really cool, and now we can change it back to being daytime again, by simply clicking on the other button. There we go! And now, we're back in daytime again! Now, I know that the other buttons are being detected at the minute guys, it's just because the paper's moving around a little bit. But fantastic, we can now use all virtual buttons. Now, once this is built out, and it's actually on a table in front of you guys, it'll work fantastically well, I've already given it a bit of a demo, and change their sensitivity settings, look, on the side, or you can also increase the actual virtual button size a little bit, and you can make the plane be the same size, so cosmetically it will still look the same. Have a little bit of a play with this, try some funky disco light if you want, but, for target 2, that's that done all that's left to do is actually make it now so that we can go through to this 3D scene and then we're ready to just duplicate this up and add the AI, fantastic! Okay guys, I'll see you in my next video!
- Updating to Unity 2018
- Setting up a scene
- Creating image targets
- Creating a multipurpose house script
- Working with virtual buttons in Unity and Vuforia
- Setting up the first-person 3D view