Animation in Unity

Most of this lesson was spent on my portfolio, and fixing things up for it, however the main focus of today’s lesson was supposed to be working out how to animate an object within Unity.

I decided to animate a door to open, because I didn’t have any 3D models of my own that could be animated easily in Unity. I made 3 cubes in Unity, and made the middle one door shaped to be the door. I then opened Unity’s animation window, and made a new animation. I made keyframes for opened, and closed, and this ran okay. I then ticked ‘loop animation’ off.

Door animation.gif

Animation attempt screen snap

At this point, I would have attached code to make it only open once a player went near it, however, firstly I forgot how to make a player in Unity 3D, and secondly I couldn’t work out the code I needed to make. Both of these excuses are pathetic, but I spent a little too long on fixing up my portfolio and didn’t leave myself with enough time.

Advertisements

Solo coding lesson

In today’s lesson, I was set off on the task of working out the answers to four questions about coding.

What does the dot do in a piece of code?

The dot is used to specify smaller and finer details, for example house.room.bathroom.sink.tap

What are the curly brackets used for?

They are used to contain a specific section of code.

Which is better, Allman or 1 TBS?

In my opinion, 1 TBS indents are better because they seem more logical to me, but overall it doesn’t matter because the code runs exactly the same regardless.

1tbs and allman comparison.png

Why does code have tabs in?

The tabs are used to indent the lines of code, purely to make it easier to read. They are also useful to show levels of brackets, but overall they are just a cosmetic thing to make it look good.

 

I was then set off on the task of making some code, that would make a cube or model interactive. I tried task 4, which was to make the cube spin across the x axis when you press down the space bar. I started off with some code that made the cube spin around the y axis automatically, and then added in code borrowed from when I made a flashlight but changed it so that it would be the space bar. I was struggling to make it work, because it wouldn’t keep spinning, it just only moved in tiny rotation movements everytime you pressed the space bar. I got stuck and sought help from Unity’s coding help website, and managed to work out a way to get it to work using booleans.

SPace rotate boolean vers.

I then adapted this to make it work without the help of booleans, and it shortened down my code a bit, but still worked effectively.

Space rotate no bool vers.

Spin thing lalal

 

Solo game: Toggling shooting off and on

So far, this has been my most difficult task yet, and I have just managed to fix it up.

The first code I made was just to make sure the base idea of the code would work. I originally made the code so that it would change the colour of the enemy sprite, just to check that the cone of vision worked.

Toggle code colour thingy

 

This worked properly, and caused the enemy to change from a cyan colour, to a dark blue colour when it saw the player…

Enemy colour test.gif

I then edited the code from colour change, to an ‘if’ statement using booleans where firing is toggled off and on.

Toggle code shoot goof.PNG

This was where I really slipped up, because I was confused about what I needed to do next. It wasn’t as obvious a fix as my last goof, but this time my mistake was that I didn’t know to literally drag the behaviour scripts from the enemy’s inspector view into the respective slots. And once I had figured that out, it still wasn’t working, but it was because for some reason, the ‘projectile’ code turned itself off in the projectile prefab.

Once I turned that on, it worked like a dream.

Shooting working final

At this current point I am over the moon with my game. I have included all of the code I need so far, and all of it is running super smoothly! The only things I would change about my game from this point on are purely cosmetic things, maybe make a newer map or jazz up the collectable item, maybe even add a new level, because I already have all of the necessary code. I think the most likely change I will make is the cosmetic changes, a new map is probably the most outstanding thing I’d change because it doesn’t look great.

 

Solo game: Week 3

My goals for this week were to:

  • Add kill collision that resets the level
  • Make the collectable item the goal
  • Add shooting to the enemy

The first thing I did was add some new collision code to the character control script I made.

Collision reset code

This makes the level restart whenever the main character runs into either the walls or the enemy.

 

The next thing that I did was to add some code which makes the collectable code take you to the next level. I don’t currently have a next level, so instead it takes you to the same level, but I definitely added in different code that makes a win state.

Item next level


 

Making the enemy shoot took a long time, but I eventually worked it out. Firstly, I made the projectile code that I added to the fire.

AutoFire code

After I added the code, I added a collider and a rigidbody, and I made it a prefab so that it would be saved with these settings. I then made the auto fire code so that it would automatically shoot.

Projectile code

This next part took me a long time to work out, because I wasn’t entirely sure where which code needed to be, however I eventually worked out that the auto fire code needed to be Prefab goofattached to the enemy, and that the ‘projectile prefab‘ needed to have a prefab of the projectile in it. This was a really simple fix in hindsight, but at the time I was completely stumped.

Shooting right update

I now have an enemy that shoots only to the right. My next step is to add code that makes the enemy shoot in the direction that it is moving, and to add code that toggles the shooting off and on whenever the enemy sees the main character, however this will be in a seperate post because this too was a long and arduous process.

Solo Game: Week 2

My targets today were to

  • Make a map (sub goal: add collision to map)
  • Add movement to the main character
  • Add a goal object
  • Add enemy and enemy collision
  • Enemy movement

I made a very quick map, which I will later replace with a more visually pleasing one, but for now this simple map will do. I then brought in the map, main character and enemy. I typed out and attached the walking code to Mercy, and added colliders to her, the enemy, and the map so that things couldn’t go though eachother. I also made a super quick representation of the collectable item so that there was a goal object in the map, which I will later replace with what I really want it to look like.

I then made a waypoint path for the enemy to follow. I did this by first making a code that uses arrays to make a list of “waypoints” that the enemy follows to trace a path. This code makes the enemy move on a particular path using vector transforms and arrays to keep them in the order that they need to be in.

 

Waypoint code updated

For my actual code, I added some code at the end that allowed me to see the path that I had traced. This code allows you to see the path with a coloured line to represent the path it takes. It is more of a cosmetic feature that makes it easier for the programmer to see, but it is very useful.

Waypoint gizmo code

Enemy waypoints

 

I managed to make the enemy trace a path along the bottom of the map to make it a little harder to get to the goal. This is going to be more useful when I add kill collision to the enemy, but for now it shows the path that you shouldn’t go near and it is useful.

Waypoints

 

Solo Game: Week 1

Today I started designing elements and assets for my assessment game. My game is going to be based around collecting pickups to ward away enemies, it’s about a child who is lost in the forest and spirits follow her and put her in danger. Collecting items keeps the spirits asleep for longer, and every item you collect adds more time to how long the enemies are asleep, however when they are awake they seek and follow the main character actively.

My main tasks for today were to:

  • Design the main character
  • Design the spirit sprites
  • Design the collectable item
  • Get the main character moving

I started by making some design sketches in my notebook based loosely on what I wanted, and I then had the idea to adapt the main character to look like a monster, even though she is only a scared little girl, I thought that the design choice would be interesting. I then designed the spirits, and got slightly carried away and made a doodle page of design choices for the levels. Then, I re-created the main character and the spirits digitally in Photoshop.

The next step I will take is to add code to the main character to make her move.

Update: I added movement in the second lesson, but the information needed to be added in this post, so this update is a week late!

The code I used to make her move is a simple movement code that uses co-ordinates to work out vectors, then multiplies these vectors (which in this code is direction) by the force (power) that I chose. The part underneath just updates every second to make the character move.

Character movement updoot

 

Trigonometry and Class Diagrams

Today I learned how trigonometry is used for movement in coding and how to make class diagrams.

In coding, trigonometry is used to work out the distance an object travels based on X and Y axis. Only right angled triangles are used in coding. The Sine is the ratio of the length of the opposite side and hypotenuse, and it relates to ‘Y’ movement. The Cosine is the ratio of the length of  the adjacent side and hypotenuse, and it relates to ‘X’ movement. The Tangent is the ratio of the length of the opposite side to the adjacent side, and it’s used to work out angles based on movement. It is most useful for working out angle changes, and under this scenario the opposite side is the ‘Y’ axis, and the adjacent angle is the ‘X’ axis.

Radians are an alternative way of measuring angles, similarly to degrees, except radians are used in coding. 1 radian is the angle covered by wrapping the circle’s radius around it’s own circumference. Pi rad is equal to 180 degrees, and 2Pi rad is the full circle (360 degrees). C# has a built in degrees to radians converter, and to use it you need the code ‘Mathf.Deg2Rad’, which also works the other way.

Class files/diagrams are a way of mapping out stats or properties an object will need. Classes are like recipes or blueprints, they are instructions you follow to make a thing. Class files also explain the actions (functions).