Context of Practice 3 Unity Workshop: Scripting
Today we had a really interesting Unity workshop about scripting, which is something that I will need to learn about in small details to be able to complete my project sufficiently and allow the mechanics to work successfully. The workshop was taken by Steve from our IT support department, but he also does a lot of freelancing work in the industry and is fairly good at scripting now that he has found an interest in it.
We started by loading up a pre-made space that Steve had made using some assets from Unity's demo level Angry Bots and we started by having a little lot at the mechanics, we had a cube to work with and it moved around the level and what we aimed to get out of this workshop was to make a section of the floor fall down once we had stood on it, make the door open as you get near it and then close once you go away from it, and then make a light come on and then flicker once I get in range of it.
At the moment in the scene you can't walk to far without falling through the floor, so creating empty game objects are a great way of generating colliders for your character so that they can walk across the floors. The character also has a box collider on it, as well as rigid body component so that when it goes near object it does not go through them; this also makes it fall off the world when it reaches the edge and is great for platformers.
At this stage we gave the camera a smooth follow so that when we walked around the level that camera would follow the character around.
Falling Platform
If you have a section of floor that is going to fall fown you want to give it a separate collider. In scripting we use curly brackets to be able to section out the code and you have to remember that if your going to delete one, you also have to delete it's partner, otherwise the script will not work.
Some key points to know when scripting, especially in C# are:
- An integer is a whole number
- A float is a number that can have decimal points and when you enter the number it need to have the letter 'f' at the end to ensure the computer knows its a float
- A bool can be either true or false
- Private scripts cannot be edited in game, but public ones can
- Vector 3 is the movement in the axis
- Animations are noted down as anim
- Void is the same as function in Java Script
- Time.delta time will ensure that the game is running on a frames per second basis and not in real time; this is so slower computers with lag can be compensated for
You have to first create the separate collider for the falling platform so that the others are still fine to walk on, and then you also need to have another box collider over the top of the platform that's bigger so that it will act as a trigger and when your character enters this space the platform will fall.
We can then add animation using the the animator menu at the bottom, this allows you to animate sections of the floor in a rough way that is similar to how you would animate in Maya, but is not quite as efficient, but works well for the simple animation we're making.
We then add an animator onto the platform which has the box colliders attached to it and we need to then create a script that will ensure that the platform only falls when the character has stepped onto it.
We called it falling platform and then opened Mono Develop, which is our script editing software. We where creating a trigger action so we needed to state what the trigger would do when started.
Variables - start with a lower case
Functions - start with a capital
The tag of an object is "in speech marks" and will reference the player directly, and anything that is a statement with need ; at the end, anything that is a function doesn't need anything.
Once the script was done we went back to the animator menu and looked at creating a new parameter, which was in this case going to be a bool. We created this and called it idle and set it to default so that this is what would happen upon the game being loaded. We then made a transition from this to the platform falling animation and then clicked on this transition and changed the conditions to the falling platform and activated it on true.
We then went back into the script to make some final changes before deciding that when the platform falls we need it to destroy itself so that it is not just sitting there taking up space. So just after the final point on the animation time line we created a new event and set the function to destroy so that just as it would falling it would disappear.
Door Opening
We then looked at how we could get the door to open and close when we got close and further away from it. We started by creating a box collider around the door and made sure it was quite wide so that when we walked up to the door it would know we were close and spin open. We duplicated the platform falling script as we can adapt it fairly easily and renamed it, along with renaming the public class name as well. We then changed the name of the object in the script and then duplicated it so we had a section for closing the door as well.
We then created an empty game object and put the door inside of that so that we could add all of our animations onto this instead of the door itself as when we are rotating it to spin, we do not want to rotate the collider, so by doing this the collider stays still as long as it is on the empty that has the door inside it. We then made a new animation in the menu that was fairly basic and had the door do a rotation as it moved down, and then repeated the process for when the door closes.
We then went through the animator process again of making parameters and made the connections so that they were the same as before, remember to change the conditions. This didn't seem to work all that well though and we found that the door didn't want to follow this, but this meant just changing round the connections slightly, and also remembering to turn loop animation off.
We changed the connection slightly so that we only had one command in the script that we could then reverse. Then by connecting them together in this way below we found that the animation now worked, but the animation did play randomly at the beginning first as you loaded the level. We fixed this fairly quickly by adding in an idle state at the beginning but just connected this up to the open door, as it will never need to start open as you load, so this all worked out very well.
Turning on Lights
We then went about creating lights in the scene and wanted to look at how we could move through the range of the light and make it turn on as we entered and turn off as we left it's range. We once again copied the script we had just made, this time the door script and did all of the renaming. This time we got rid of all of the animate sections as we were just going to be turning the light on and off, something you can do in the presets for the light itself. We added in a box collider once again, and this was this script pretty much done. We did go back after, once we tested this worked, and added in an extra line of script so that we could get the light to flicker on a random number when we entered the space, which again worked well.
Overall this was a brilliant, if not difficult, workshop and I feel that I did learn a lot from it, even if I did find it very confusing to wrap my head around some of the code and terminology that's used. Once into the flow I did begin to understand some of it, but if I am going to be using this again, as I predict I will be in my COP work, I need to ensure spelling is correct as this is where I gathered errors in my testing.
No comments:
Post a Comment