Thursday, August 25, 2016

Unity's Event trigger component

So I was adding my buttons to the game using UI event sytem in Unity.
The Fire and Flip where easy but when it came to the Thrust button I hit a snag. The button click is not enough I need it to respond when the button stays pressed.
To do this I needed to add Event Trigger component to the button.
Then in the component click add new event type for Pointer Down and Pointer Up. 

Here is the code for the 2 functions:

    public void ThrustDown()
        IsThrust = true;

    public void ThrustUp()
        IsThrust = false;

Then in the FixedUpdate() check if IsThrust then increase the speed, also I’ve added the code to slow down the player over time with the Speed * .99f:
        if (IsThrust)
            Speed += thrustAmount;

            if (Speed > MaxSpeed)
                Speed = MaxSpeed;

        Speed = Speed * .99f;

Sunday, August 21, 2016

Mutant aliens

When an alien takes a sheep up to the top of the screen it shears the sheep and becomes a fast moving alien mutant. 
What I want is for the mutants to go after the player. To do that we subtract the 2 positions vectors, giving us the difference between the 2 points. I wanted to use that to then set the direction of the mutant. I discovered there is a function normalized that does just that, so here is the code from the FixedUpdate() to do that. By placing it in the FixedUpdate it will continually change course towards the player.

if (IsMutant)
    var delta = player.transform.position - transform.position;
    direction = delta.normalized;


Aliens take sheep to top then change into Mutants

Saturday, August 20, 2016

Attack Waves

In defender all of the baddies don’t all arrive at once there are a number of waves per level. So to do that when instatiating the baddies I'll have a delay between them.

In a previous blog on the development of Rhino roll I used Editor scripting to help create the levels. So I’ll do the same here again. 

Here is my code to create an array of objects in the editor script.

    static void ObjectsToArray(string objType)
        var Code = "private Vector2[] " + objType + " = new Vector2[] {";
        var objs = Selection.gameObjects.OrderBy(go => go.transform.position.x);
        bool IsFirst = true;

        foreach (var item in objs)
            if (!IsFirst)
                Code += ", ";
                IsFirst = false;

            Code += string.Format("new Vector2({0:0.000f}, {1:0.000f})", item.transform.position.x, item.transform.position.y);
        Code += "};";


Here is the code to add the Menu Items to the IDE of Unity and call the function 

    static void SheepToArray()

    static void LandersToArray()

The idea is to move the landers aboout, duplicate new ones et.c. then select them all and call the LandersToArray function, like below.

Here is the code automatically generated by the function call. Saves so much time when designing levels, in my opinion.

    void Level1Wave1()
        Vector2[] Landers = new Vector2[] { new Vector2(-48.800f, -0.140f), new Vector2(-42.200f, 1.560f), new Vector2(-11.880f, -1.120f), new Vector2(-5.680f, -0.440f), new Vector2(-0.080f, 2.320f), new Vector2(0.020f, -1.850f), new Vector2(3.500f, 1.560f), new Vector2(24.200f, 0.960f) };
        Vector2[] LanderDirections = new Vector2[] { new Vector2(-0.240f, -0.074f), new Vector2(-0.933f, -0.997f), new Vector2(0.489f, -0.842f), new Vector2(0.702f, -0.403f), new Vector2(0.700f, 0.146f), new Vector2(-0.497f, -0.331f), new Vector2(-0.186f, 0.392f), new Vector2(-0.868f, 0.386f) };

        DoLanders(Landers, LanderDirections, .02f);

Then in the fixed update add the code to invoke these functions after a given time.

        if (CurrentWave < Waves.Length)
            if (Waves[CurrentWave] == 0)
                var wave = string.Format("Level{0}Wave{1}", CurrentLevel, CurrentWave);
                Invoke(wave, 0f);

As before here is a video to demonstrate. It has 2 attack waves from the aliens. I haven't done any fancy animations for the materialisation yet.

Friday, August 19, 2016

Wool is the Fuel

A couple of years ago I created my 1st game, it was Wool is the Fuel.  The idea of the game was that Space ships are powered by wool, so the aliens come to earth and shear the sheep. Originally I was going to have cows, as those aliens can’t resist a bit of cow mutilation, however I thought that wasn’t very child friendly, so as sheep shearing isn’t as murdery I’d go with that.

It was written using XNA and took a number of months. I then rewrote it in Unity in 2 weeks but I scrapped the game as it really was quite rubbish.

Now having written a number of games I’m returning to the idea of Wool is the Fuel. I’m going to combine Wool is the fuels with a game I always loved Defenders. The idea in Defender is that you have to protect your colony of humans from a bunch of aliens. The aliens arrive in waves and after you have cleared all the waves you have completed a level. As the level number goes up so does the difficulty.

So instead of humans, you have to protect your sheep. Also no sheep will be harmed in the playing of this game they cannot die, even after falling from a great height.

My Rhino Roll game has the code for the parallax scrolling backgrounds, so I will reuse that but allow the changing of direction. To do this I add a Collier to the Left and right of the Main camera, when it hits a tile it moves it over to the opposite direction.
When moving right the left collider in green moves A tile to right

When moving left the right collider in green moves E tile to left

As with the Rhino Roll game I am going to have multiple layers of background so you can see the parrallax scrolling.
For the backgrounds I'm using the Delta essentials pack from Spriter by BrashMonkey.

 Videos of Scrolling in action