Lasers and Why Gravity is Hard to Change

Just to start with, today won’t have an update, just a sneak peak about upcoming features and stuff I want to add, but will be difficult to implement. First of all, the next update will have a vastly better Laser Gun; much like the previous one improved the Force Gun, with a new animation controller, new model, new everything, I’ve now done the same for the Laser Gun.

spikes-73I’ve also been busy adding lots of scenery to each level. So far all of World 1 is complete, and some of World 2 as well. Basically, I’ve added a roof and some scaffolding-type blocks to make the whole place look more facility-like. Along with that I’ve added some decorative textures that light up and look like they’re moving, but you’ll have to wait and see what they’re like.

Gravity is a constant, and it’s really bugging me *lol puns*

But the main reason why this update’s taking a while is because I want to add a new type of gun, one that switches the direction of gravity. Changing gravity for physics object is literally one line’s code worth of work, but I want to be able to turn the character controller round too, which is proving to be much more difficult. I may just make the gun turn gravity for physics objects around for now, but that’s much less fun than being able to drop down onto a faraway ceiling, which is the feature I’m going for here.

The system doesn’t work

Also I’ve been trying to add a timer and score counter for different types of level and game modes (such as a time trial mode), however I’ve run into trouble with how the system will work. Long story short: it doesn’t. I’m not entirely sure how the score should relate to the time, and how timed levels should be, if I implement them. Also I’m unsure whether any actions you take in the game should give the player points, and if so, which actions. Another problem I had was with the display, as the built-in Unity OnGUI stuff is horrible, but I’m unsure of how to implement my own GUI for this. I’ll have a score system eventually, but if you have any ideas on how to go about it, they’d help a lot.

These are some of the many problems I face when adding new features, such as how to add them, how to make sure everything works, but also how fun it ends up being. Things such as lives, which seemed fun to begin with, ended up detracting from the gameplay a lot, stopping the “one more go” approach to solving puzzles. That’s extremely counter-intuitive, when you think about it. I’m also considering getting rid of gravits, as they also seem to make little sense. Feel free to leave our views on where I should take this below.

Random Code: Pause Menu

Have you ever been creating a game when you thought to yourself, “Hey, I want to pause my game”, then you realised that you’re so silly that you don’t know how? Then this is the script for you!

private var paused: boolean = false;
private var mouseX: MouseLook;
private var mouseY: MouseLook;

function Start()
{
    Screen.showCursor = false;
    Screen.lockCursor = true;

    mouseX = GameObject.FindWithTag("Player").GetComponent(MouseLook);
    mouseY = GameObject.FindWithTag("MainCamera").GetComponent(MouseLook);
}

function Update()
{
    if(Input.GetKeyDown(KeyCode.Escape))
    {
        if(!paused)
        {
            paused = true;
            mouseX.enabled = false;
            mouseY.enabled = false;
            Screen.showCursor = true;
            Screen.hideCursor = false;
        }
        else
        {
            paused = false;
            mouseX.enabled = true;
            mouseY.enabled = true;
            Screen.showCursor = false;
            Screen.hideCursor = true;
        }
    }
}

function OnGUI()
{
    Time.timeScale = 1;
    if(paused)
    {
        Time.timeScale = 0;
        if(GUI.Button(Rect(Screen.width / 2 - 75, 200, 150, 25), "Back To Main Menu"))
        {
            Application.LoadLevel("StartMenu");
        }
        if(GUI.Button(Rect(Screen.width / 2 - 75, 250, 150, 25), "Back to Game"))
        {
            paused = false;
            mouseX.enabled = true;
            mouseY.enabled = true;
            Screen.showCursor = false;
            Screen.hideCursor = true;
        }
    }
}

This script isn’t too hard to get to grips with. First, we declare a few variables, and make them private so another script can’t come along and vandalise them. One to store whether we are saved or not, and two for each of the MouseLook scripts attached to the standard Unity first person controller. When these are initialised in the Start function, the script will look for the tags “Player” and “MainCamera” – in my project, these are the the tags assigned to my main camera and the actual player character, each of which have a MouseLook component. It also hides the cursor from view, and locks it to the centre of the screen.

Then, if the player presses the Escape key, it checks whether the game is paused. If it isn’t, then it pauses the game, disables the MouseLook scripts and frees the cursor; if it is paused, the opposite happens. In the function OnGUI(), which is called every time the frame is drawn and is used to draw stuff on-screen using the basic Unity GUI features, the time scale is set to zero when the game is paused, which stops all movement (this basically is the part of the script that actually pauses the game). Then it draws two GUI buttons, centred in the middle of the screen horizontally; one returns the player to the main menu, and the other unpauses the game. Pressing the first button will run Application.LoadLevel(“StartMenu”); which loads a level named “StartMenu”. The latter just unpauses the game.

PauseMenu1

This is the result when you pause the game, a basic menu with two buttons. In any case, I hope this helps, have fun coding!

Day 4 and 5 Progress

I’ve been having a bit of trouble getting the player to pick up the gems, as there must be some kind of error in my scripts or something, and I had hoped to show you that today but unfortunately it’s not yet working. However, I do at least have something to show you: I got the warping between levels partway implemented! There still needs to be some kind of animation and model for the actual portal, and a transition between the two levels, but the actual warping works and that makes me happy enough. platformer_test_2

The small red cuboid in the middle is the player, and the larger red cuboid to the right is a portal. Walk into it, and it will take you to another level I’ve constructed: platformer_test_3

This is “Level 1”, as it’s called in the code, and yes, it is really shiny. Currently it only has loads of gems in it, which you still can’t pick up. Damn it. I will try to find the error that’s preventing that from happening, then I will get back to you with the result. I know this doesn’t look like much work for day 5, but I’ve tried to spend as much time as I can on it, and I am new to this so don’t expect a triple-A game after the end of the month. I’m still learning while doing this, and hopefully my future projects will take off quicker too.

The next step is to fix the scripts, and try to make them more optimised, and then I will make better models for the gems, and some scenery. Maybe soon I will also get some enemies or NPC characters in the game, although that’s likely to come later rather than sooner. I may also do more drawings of possible enemies and characters and show them here.

The download link to the practice version of my game hasn’t changed, here it is:

https://www.dropbox.com/sh/0o38di2thg3x9za/N_CvcxejXT

Also, my friend kindly asked me to promote his blog, so here it is. He does game reviews and the like, so check it out!

http://www.epsilongaming.wordpress.com

Thanks for the support.

-Daniel