Showing posts with label Unity. Show all posts
Showing posts with label Unity. Show all posts

Tuesday, September 8, 2015

Unity Editor scripting: How to create levels

So in creating the levels I came across a problem.

It was really difficult to set up a level. Moving all of the tiles about was really difficult as they are inside a game object. Each parent objects has 4 sprites, a ceiling a floor, a top and a middle. These can all be assigned different Y axis positions.
What I really needed was a way to select multiple parent GameObjects then somehow manipulate them all in one go. So the way I’ve achieved this is by editor scripting.

To do this I created a script it that goes into a special folder called Editor in the Assets directory, if the folder isn’t there create it.

At the top of the script we need to add.

using UnityEditor;


We need to add the attribute for the menu item;

[MenuItem("Adz/Floor/UP/FloorUP")]

This allows us to change the IDE of unity to have our special function available as a menu item.
To run the function we select multiple GameObjects then click the FloorUp function.

Multiple GameObjects selected with new Menu items in IDE 

Here is the code for the FloorUp function, it loops through all of the items selected in the scene view, in order of the transform’s X position and increases their y by .1f.

   static void FloorUP()
        var items = Selection.gameObjects.OrderBy(go => go.transform.position.x);

        foreach (var item in items)
        {
            anchor p = item.GetComponentInChildren<anchor>();
            p.FloorTileY += .1f;
        }
    }

After clicking the menu item FloorUp we see the effect on the selected GameObjects, it gives us a ramp going upwards.

After the editor script FloorUp is run

So then all I did was create similar functions for the ceiling, Top, Middle sprites then I could easily create levels for the Shuffa ball: Endless roller game.



Wednesday, August 5, 2015

Rhino animations and transitions

To the Rhino sprite I want to add 3 things.

  • A sprite to represent the dust as the wheel turns on the ground.
  • A Dust explosion when the ball takes off and lands.
  • The third is more difficult I want the whole Rhino sprite to be angle differently when it takes off and lands.

Rhino Revving, taking off and landing

Revving Sprite

This blog will be about adding the revving sprite to the Rhino ball.

Again I’m going to use Spriter by BrashMonkey, and the Dust.scml from the Game effects artpack.

It has 2 animations the Rev sprite and the landing sprite, I’ll export them to separate PNG files like I did previously. Then drag them onto the hierarchy to create the animation.

If we were to attach to the wheel as a child the rev sprite would rotate with the ball. We could attach the sprite to the Rhino head as that is doesn’t move much but as with the flame sprite I’m going to use the Update function to position relative to the centre of the ball.
Unlike the flame sprite we need to apply an offset as the rev needs to appear on the left of the Rhino ball. To find the offset make the Rev temporarily a child of the Rhino ball then move to the ideal position, store the transform position as this is what we’ll add in the Update function. Remembering to move the Dust_000 from the RhinoBall hierarchy.

adjusting the transform of the Rev sprite

adding the sprite to the Rhino script

So the Rev sprite now works and is well positioned but the problem is when the Rhino jumps and is airborne there shouldn’t be dust revving. So as with the flame in the previous blog we will RayCast down from the centre of the ball to see when we lose contact with the ground.
If we lose contact we set active of the Rev GameObject to false, in the future we will transition from the Rev sprite to the take-off sprite but this will do just now.

Wednesday, July 22, 2015

Creating scrollable background tiles with Inkscape

In this blog I will go through how I easily created the background tiles for my game Shuffa ball:Endless roller. In playing about with Inkscapes layers I came up with this idea for exporting multiple backgound tiles with no need to line things up.

In unity it seems to me 100px is 1 unit in unity, so I created a rectangle 400px by 1000px, the colour is not important as the rectangles will be below what we want to export later and therefore won't be seen.


Exporting the rectangle now calling it 1.PNG, this will allow all others to have the same name which will make changing the names of the others easier later.

Next we will create duplicates of this rectangle; with the rectangle selected press CTRL+D to duplicate, do this several times. All of the rectangles are in the same place, we want them side by side so we use the Remove Overlaps in the Align and Distribute tab (Ctrl+Shift+A).




So left drag over the rectangles which will select them all then press the Remove Overlaps button.
This should expand to look like below with all aligned with no overlap.


Click on each rectangle and click export PNG, change the name from 2->8.PNG then export them. We do this now as later on we will be batch exporting the 8 tiles and it makes it easier.

The next thing we do is add the actual image we want to export so add a new layer (Ctrl+Shift+L). 
Call this layer Background, whatever we draw in this level is what we want to be exported. 
Rename Layer 1 as tiles.

After finishing the drawing on the background layer, we want to hide this level. To do that click on the little eye icon in Layers tab which will hide the background level.

This should leave all of the tiles visible on the tiles Layer, so select all of the tiles.


Then when you click again on the eye on the background level, you will see your background drawing with the tiles selected underneath.

The next stage is to batch export the selected objects. Make sure "Hide all except selected" is NOT selected. If you did that you will get the grey rectangles exported.


Click the export and we should see 1->8.PNG exported.


You can see these background tiles in action by visiting my Game Jolt page that has a playable demo,
or look at this video.




Monday, July 20, 2015

Shuffa ball: Endless Roller


After writing Shuffa ball it gave me the idea for another game. It will be an endless runner style game but with a ball, so I’m calling it Endless Roller.
It will use the same physics as Shuffa ball, but with a play area much, much wider. Endless in fact.

The process of writing this game I’ll document in this blog, most decisions regarding the game I haven’t made yet.

GamePlay

The gameplay is made up of multiple obstacles, when you touch and slide down on an area of the screen; the obstacles come down with you. Then, when you release they snap back into place, applying a force to the ball.  For the obstacles I use object pooling, a number are created at start-up but are inactive. Then when needed they are set as active off screen to the right and move into the frame as the camera follows the ball. Then when they go off screen to the left they are set as not active ready to be reused later.




The ball tries to maintain a constant velocity, if it is going slower than X then force is added; if faster that is OK, that speed will naturally be lost through bouncing against the obstacles.
I haven’t yet decided on the purpose of the game.
What stops the game, how do you die or run out of time.
What is the goal, is it a distance or completing an action.
Hi scores are they points or time to complete.

Backgrounds

Here it will tile the various backgrounds, as we move to the right it will take the background tile A and place it next to the E.
To do this we need to have a trigger point in each tile that says if the tile goes off screen to the left move it all the way to the right, which will be off camera so we won’t see it happen.


The ball will be followed by the main camera. The tiles in the background will move at a slower speed than the foreground, this is called parallax scrolling.
We can create multiple levels of background, the furthest from the camera being the slowest, nearer the camera progressively faster.

Like Shuffa ball, Endless roller will be written in Unity, here is how the backgrounds look in the Unity editor.



Click here to see the background scrolling in action.

I have a playable demo of the game so far. It is on Game Jolt, I'll be updating this every now and again as this blog progresses.