Wednesday 26 November 2014

Coding an Automatic Door on Unity


Now that we had the light switch working, our next objective was to get a door coded to open automatically when we are are near. This is one thing that I was worried about, at this stage I feel that I should have some sense on how Unity and Coding works, but still it seems lost to me, though by doing the work that has been sent to us, along with the help of the tutors, I'm sure soon, that it will all make sense.

This lesson was all about following what was done by our teacher in order to make sure that the code we had created was working perfectly. We started off by following his every step, every dash, every bracket and every full stop.

Getting the door to open.
We first had to get the door to Open, so by selecting the doors that was already made for us, we added a new component which was a new script. Here we named it MyDoor1 and MyDoor2. It was important that we differentiated them from each other, this is because we don't want the doors to move in one direction, they both have to move opposite each other so that it looks like they are sliding.

As I mentioned before we followed our teacher Ewan carefully and made sure that we stuck to the script just as he had put it. After we had created the script which is depicted above, we had to go back into unity and drag both the doors on the different drop down menu's now featured in the script. By linking this with a collider, like we had done in the light switch, it means that when we get within a certain area of the doors they will open automatically.

Getting the door to close afterwards.
There was a problem with the doors though, it meant that every time that we went to walk through it, it didn't close, it stayed open as the code had commenced. What we needed to do was to go back into our code that we had down earlier and make the door close after you had left the collider (trigger).

This was actually more simple than I thought it would be. It involved us just trying to copy the code that we had already done on the doors opening and pasting it again as its own code. The next thing, using the collider was to say that as soon as the player had left it, we would close the door. Just by changing the numbers to reverse and changing the factors from true to false and false to true, we were set.
The end result.
We then had the finished result,which was actually quite fun to create. This something that came out quite stunning as you cant believe that computer game designers put this much effort into doing something so small such as opening a door.

As mentioned above, I'm finding coding a bit of a struggle, though today has really taught me new and exciting things which I would love to put into my own level.

No comments:

Post a Comment