Posts

A while ago (sometime in the fall) I had an idea for a game. From the start I just wanted to create something cool with HTML canvas, and I came up with a vague idea of a type of idle game. The plan was to make something like an RPG game where you couldn’t control anything other than the class/race and name of the character.

From the start it was supposed to be offline, but when my friend Erik came into the project (Erik, likes to have ideas and create graphics) he got me hooked on trying to make it multiplayer online aswell. The project grew a bit and I had to rewrite some of the code. Luckily, since it was writting in JS and jQuery I could use most of the original code on the server, since I choose a stack with NodeJS.

The game uses canvas on the frontend client and communicates in realtime with the server using websockets. It takes the data returned and repaints the canvas to update the world map with player positions, and the “console” with new actions.

There are more features that I will show in a later post, together with explanations of the backend code, but I just wanted to get this out there.

AdventureHistorians

Well. It’s been a while (lol) since I’ve posted something now. Don’t know why, just haven’t had the time or energy, and when the work I was doing on my 3D game stopped I just didn’t have that much to write about.

Gonna try to update with some posts about newish projects I’ve made since last posting here, hopefully in the next couple of days.

Haven’t been posting for a few days now. I’ve been working on cleaning up a few things in the saving code. I’ve also fixed the background of the inventory and the code for picking up items. Going to be posting an explanation in the next couple of days.

The reason I’ve been too busy in the last couple days is because of my other hobby, choir singing. I sing in the student choir of the University of Gothenburg. On Friday we had a gig which went really well and then on Saturday we had our last party of the semester. The theme was “elementary school dance”, so I went as a truck.

11430093_10153386323555139_8536914116607297308_n
A solitary killer, the truck stalks its prey.

 

 

 

 

 

 

 

Weird, I know, but it’s quite funny in Swedish. A “långtradare” is really used to describe a big truck, but when we were younger it could also mean something like “a long kiss”. Funny, right? 😉

I was quite pleased with the outcome, but it was very hard to walk around with. Anyways, that’s why I haven’t been posting. Check back later in the week.

Have a good one!

 

Edit: I posted the image to Reddit and got quite a lot of karma (/r/Sweden). Yay!

This is going to be a short post (I think) about saving data in Unity3D.

Been working on a save-mechanism for the game. Turns out I should’ve started implementing it in conjunction with the inventory system, since I had to change some stuff, but I think it’s coming along pretty well. It saves the position of items if they’ve moved from their initial one, it saves the inventory and it saves the player position.

The inventory saves every thing something new is added or discarded. The new position of items save when the item is still after being discarded. And the position of the player saves in a OnApplicationQuit()-function. There is also a save button in the menu, but I haven’t decided wether or not to keep it yet. It all depens on how much autmatic saving I can do when I get more features added.

I’m saving the data in the PlayerPrefs, but since you can only save a float, int or string, I’m saving the player position like this:

 

[code lang=”js”]
PlayerPrefs.SetFloat("player_position_x", transform.position.x);
PlayerPrefs.SetFloat("player_position_y", transform.position.y);
PlayerPrefs.SetFloat("player_position_z", transform.position.z);

PlayerPrefs.SetFloat("player_rotation_x", transform.rotation.x);
PlayerPrefs.SetFloat("player_rotation_y", transform.rotation.y);
PlayerPrefs.SetFloat("player_rotation_z", transform.rotation.z);
PlayerPrefs.Save();
[/code]

And then when the game starts, I set the player position like this:

[code lang=”js”]
transform.localPosition = Vector3(PlayerPrefs.GetFloat("player_position_x"), PlayerPrefs.GetFloat("player_position_y"), PlayerPrefs.GetFloat("player_position_z"));
[/code]

The code for the item position is basically the same, but with a unique id for each item. It works for now, but it seems a little too good to be true. If it’s still working in a week I’ll write something up about it. The code for the inventory-saving is too messy to explain here, maybe when I’ve cleaned it up a bit.

Next in line is adding some “objectives” to the environment. I have something in mind, but I’ll let that be my secret until I know I can make it work.

Have a good one!

Since I made the last post (yesterday…) I’ve rewritten some of the code for the inventory to allow saving/loading, but in this post I will continue with the code we hade in the last post. If you haven’t read that yet I suggest you do before continueing.

shingle_throw

When we ended the last part we had successfully added an object to our inventory. What we want to do now is set an equipped object when we press our number buttons. So we do a getKeyDown for the number keys:

[code lang=”js”]
function Update() {
if (Input.GetKeyDown(“1”)) {
setEquippedObject(1);
}
}

function setEquippedObject(equippedItemNumber : int) {
equippedObjectNumber=equippedItemNumber;
if (equippedObject) { equippedObject.SetActive(false); }
var tempItem : UnityEngine.GameObject;
tempItem = inventoryList[(equippedItemNumber-1)] as UnityEngine.GameObject;
if (tempItem.name != “Empty”) {
tempItem.gameObject.SetActive(true);
equippedObject = tempItem;
equippedObjectText.GetComponent(UI.Text).text = equippedObject.name;
equippedObjectAction.SetActive(true);
} else {
equippedObjectText.GetComponent(UI.Text).text = “”;
equippedObjectAction.SetActive(false);
}
}

function updateItemTag() {
currentItem.gameObject.tag = “Moving”;
currentItem=null;
}
[/code]

What we basically do in the setEquippedObject-function (which takes the number of the key as a parameter) is to:

  • If there already is an equippedObject, set it to null.
  • Check our inventoryList for the item with the corresponding number.
  • Enable the item.
  • Show it’s name on the screen together with an “action text”.

And if there isn’t an object in that slot we just texts.

Now if we want to do something with the object when the player presses “f”, we do this:

[code lang=”js”]
function Update() {
if (Input.GetKeyDown(“f”)) {
doItemAction(“throw”);
}

}

function doItemAction(action : String) {

switch (action) {
case “throw”:
if (equipItemObject.transform.childCount > 0) {
currentItem = transform.gameObject.GetComponent(CharacterInventory).equippedObject.transform;
currentItem.gameObject.AddComponent(Rigidbody);
currentItem.gameObject.GetComponent(Rigidbody).collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
currentItem.transform.parent = objectsParent.transform;
currentItem.gameObject.GetComponent(Rigidbody).AddForce(currentItem.transform.forward * 300f);
Invoke(“updateItemTag”, 0.1);
transform.gameObject.GetComponent(CharacterInventory).removeInventoryItem();
}
break;
}
}

[/code]

And what we do here is:

  • Check to see that there is anything in our inventory (equipItemObject.transform.childCount > 0).
  • Set the MeshCollider to convex (needed for the rigidbody).
  • Add Rigidbody.
  • Set the collisionDetectionMode to ContinousDynamic (to keep it from going through stuff).
  • Set the parent of the object to a “world wide” object (to keep it organised).
  • Add some force (the actual throwing).
  • Run a function that removes the item from inventory (I’ll get to that one).
  • Wait 0.1, then set the tag to “moving” and set the currentItem to null.

The removeInventoryItem-function looks like this:

[code lang=”js”]
function removeInventoryItem () {

inventoryList[equippedObjectNumber-1] = emptyObject;
var i=0;
equippedObjectText.GetComponent(UI.Text).text = “”;
equippedObjectAction.SetActive(false);

for (var child: UnityEngine.Transform in inventoryUI.transform) {
var tempItem : UnityEngine.GameObject = inventoryList[i] as UnityEngine.GameObject;
child.name = tempItem.GetComponent(Item).itemNameInInventory;
child.GetComponent(UI.Image).sprite = tempItem.GetComponent(Item).itemImageInInventory;
i++;
}
equippedObject = null;
}
[/code]

And does this:

  • Sets the old object to empty.
  • Sets the item text/item action text to null.
  • Iterates through the children of the Inventory-object and ‘resets’ them all.
  • Sets the equippedObject-variable to null.

And there we have it. An (probably inefficient) inventory system with item actions.

I know that this might be hard to follow, but I’m just getting started with these posts and hopefully I’ll get better over time.

Have a good one!

Disclaimer: I do not claim this is the best (or even a good way) to design and implement an inventory system. There is a slight (great) chance that I throughout the authoring of this post decide that ‘this is stupid’ and just scrap the system all-together and start over. If so, you’ll get your money(time) back in the form of virtual cupcakes.

In this post I will try to explain the inventory system for my current lowpoly game project. A short explanation of the gameplay I’m ultimately going for is somewhat of an adventure/mystery game. This is because I  want to keep the project on a difficulty level I can handle, therefor I don’t want to have a combat system and as few advanced models (characters) as possible (This is the first project where I make the models mostly myself).

Right now you can walk around, open a pneumatic house, pick up rocks and look out across the ocean. Epic gameplay!

Here is a demo of the current working version.
(Because of limitations in Chrome, you have to open it in Firefox)

You move around with WASD and look around with the mouse. The pointer is locked in the middle of the screen and invisible.

If you ‘look’ at something you get a small text describing what it is you are looking at (eg. “Tree”). This is done with a code similar to this (I removed some other code not needed for the inventory system):

[code lang=”js”]
function Update () {
var ray = Camera.main.ViewportPointToRay (Vector3(0.5f,0.5f,0.5f));
var hit : RaycastHit;

if (Physics.Raycast (ray, hit, 2)){
Debug.DrawLine (ray.origin, hit.point);
rayHitCollider = hit.transform.GetComponent(Collider);
switch (hit.transform.tag) {
case "Untagged":
information_text.GetComponent(UI.Text).text = "";
action_text.GetComponent(UI.Text).text = "";
action = "none";
break;
case "Item":
information_text.GetComponent(UI.Text).text = hit.transform.name;
if (hit.transform.GetComponent(Collider).GetComponent(Item)) {
rayHitCollider = hit.transform.GetComponent(Collider);
if (rayHitCollider.GetComponent(Item).ableToPickUp) {
action_text.GetComponent(UI.Text).text = "Pick up (E)";
action = "pickUpItem";
} else {
action_text.GetComponent(UI.Text).text = "";
}

}
break;
default:
information_text.GetComponent(UI.Text).text = hit.transform.name;
action_text.GetComponent(UI.Text).text = "";
action = "none";
break;
}
} else {
information_text.GetComponent(UI.Text).text = "";
action_text.GetComponent(UI.Text).text = "";
action = "none";
}

if (Input.GetKeyDown ("e")) {
doAction();
}
if (Input.GetKeyDown("f")) {
doItemAction("throw");
}
}
[/code]

The scripts shoots a raycast towards where the player is looking, locked to the center of the screen. If the hit of the raycast has a tag which is not “Untagged”, the text underneath the cursor changes to the name of the object and if the object type (decided by tag) has an option (eg. door->open) there is an actiontext below the cursor as well and a variable called “action” is set. If the object is “Untagged” or the raycast doesn’t hit anything the text is nulled.

If you aim your mouse at something you can interact with and press “E” a function called “doAction()” is called. It looks like this:

[code lang=”js”]
function doAction() {
switch (action) {
case "openDoor":
if (rayHitCollider.GetComponent(Door).doorOpen) {
rayHitCollider.GetComponent(Door).closeDoor();
} else {
rayHitCollider.GetComponent(Door).openDoor();
}
break;
case "pickUpItem":
transform.GetComponent(CharacterInventory).addItem(rayHitCollider.gameObject);
rayHitCollider.gameObject.SetActive(false);
rayHitCollider.gameObject.tag = "Untagged";
rayHitCollider.gameObject.transform.parent = equipItemObject.transform;
rayHitCollider.gameObject.transform.localPosition = Vector3(0,0,0);
rayHitCollider.gameObject.transform.localRotation = Quaternion(0f,0f,0f,0f);
break;
default:
break;

}

}
[/code]

I’m going to focus on the inventory-action. If you are able to pick up the item, determined in the first script (a bool in the “Item” script attached to all items), the “pickUpItem”-switch case is called and a few things happen.

  • A function in the inventory script is called.
  • The item is set to disabled.
  • The item is “Untagged”.
  • The items parent is set to an object called “equipItemObject”, which is basically just the position for the item in the players hand.
  • The position and rotation of the item is set to “forward”.

So now the item is no longer on the ground but instead resides disabled as a child to a game object on the player. This is how my inventory looks in the Unity editor:

Screen Shot 2015-06-02 at 12.44.34

 

It’s an empty gameObject with 8 ‘slots’. When the game starts they an array with 8 empty objects (named “Empty” and with the empty thumbnail) is created and iterated through, changing the 1-8 to empty objects.

The function called in the inventory script on the other hand looks like this:

[code lang=”js”]
function addItem(item : UnityEngine.GameObject) {
var i=0;
for (var child: UnityEngine.Transform in inventoryUI.transform) {
if (child.name == "Empty") {
inventoryList[i] = item;
break;
}
i++;
}

i=0;
for (var child: UnityEngine.Transform in inventoryUI.transform) {
if (i < inventoryList.Count) {
var tempItem : UnityEngine.GameObject = inventoryList[i];
child.name = tempItem.GetComponent(Item).itemNameInInventory;
child.GetComponent(UI.Image).sprite = tempItem.GetComponent(Item).itemImageInInventory;
}
i++;
}
}
[/code]

It takes the item picked up from the ground as a parameter and tries to place it in an empty slot. Then it ‘resets’ all the items and images according to the new array.

(Pre pick-up)Screen Shot 2015-06-02 at 12.44.48 (3)

 

(Post pick-up)Screen Shot 2015-06-02 at 13.52.52 (3)

A rock. In my inventory. Ready for throwing.

There we have it for the first part of the explaining of my inventory system. I’ll try to get the next one, where I’ll go over actions you can take with the item in hand and how to place it back on the ground, later this week.

Thanks for reading and comment with any questions or if you have any tips on how I could improve, both this code and in general.

Have a good one!

 

 

Edit: I noted a bug in the code that removes the item from the inventory when thrown. If you try the demo and throw the rock away, then pick it up, the “Action-text” will be nulled. It’s fixed now, but I won’t upload a new version of the demo just yet.

I’ve been fiddling with the inventory system for a little while now. Recently I’ve been trying to add functionality to picked up items, but now I also got around to fixing the rest of the inventory system.

I’ve gone for a “locked” 8-slot inventory, controlled by the number bar (as is pretty much standard). Empty slots have a beige background, full slots have the image of the item (duh). Right now the only items you can pick up are rocks, but I could technically just add the “Item”-script to any item and it would be pickupable (that’s not a word!). I have to script something to check if the item is too large for the screen as when you equip an item it shows up in the center, and if you were to pick up more than 8 items I think the game would crasch (haven’t tested that yet).

Anyway, it’s progress. Before I move on I’ll have to finish the code for the scale of equipped items and an Inventory-limit. After that I think it’s time to look at some more functionality on the houses. Stay tuned!

(I’ll try to get some images in here later)

For the past week I’ve been trying to implement a working inventory system. Since I’m new to almost everything in Unity I usually try and find my own way before implementing something that is either to complicated for me to understand yet and/or a bit bloated for my need.

So I’m going with a set 8 slot inventory that is always visible at the bottom of the screen (kind of like Minecraft, but without the bigger inventory).
inventory_shingle2
This is done with an inventory (empty gameobject), that has 8 differents ‘slots’ (also empty game objects, with images). When you pick something up the item gets placed in a “equipped item” and disabled, and the image of an empty inventory slot gets switched for the one of the picked up item.
inventory
The ‘equipped item’ is a game object that’s a child of the main camera and when you press the number buttons (1-8), the corresponding disabled child gets enabled.
shingle_throw (Don’t mind the unreadable font)

From here you can choose to press “F” to throw the item. If you do a few things happen:

  • The items meshcollider is turned to true.
  • A rigidbody is added and it’s collisionDetectionMode is turned to CollisionDetectionMode.ContinuousDynamic (had problems with items falling through the ground before this).
  • A force is added to transform.forward.
  • The items parent is set to null.
  • An Invoke-snippet is used to wait .1 seconds before changing the items tag to “Moving” instead of “Null” as it had while being an equipped item. The items in my game are tagged “Item” when they are on the ground, but I have to remove this when they are equipped, otherwise you can pick them up from your own hand. The reason for the .1 second delay is because of the next action.
  • When the items tag is changed to “Moving” an update function in the “Item”-script starts checking to see if the items velocity is equal to not moving. If so it removes the rigidbody and makes the mesh collider not convex anymore. It also sets the tag back to “Item” so that it can be picked up again.

And there you have it, throwable rocks.

For a while I have been working on a game in Unity. It’s not my first time, but it is my first bigger project, and definitely the first one where I’m also making most of the models/graphics. It’s not something I have I am sufficiently good at, but I want to learn and what better way than with a project?

It started with me following a tutorial for making a lowpoly landscape.
WA99ICl

I was so happy with the results that I used it as a starting point for this project. I then tried my hand at making characters, houses and trees. They all turned out much better than expected, so I just continued.

I won’t go into detail about what I’ve done thus far, but there will be new more detailed posts about future progress. Here are a few gifs.


Feel free to follow my progress here or on reddit.

Well, it’s that time again. Remake of the website. Okay, remake is a strong word seeing as I haven’t really had a website before. I’ve had a contact page, crudely coded/ugly. Seeing as I work as a developer I should really code the site myself, or atleast the WP-theme, but I just can’t be bothered. It’ll have to do with a generic free theme for now.

In Sweden we have a saying: “Skomakarens barn går i trasiga skor”. “The children of the shoemaker have broken shoes” (very roughly translate… can shoes be broken?), which is kind of what I’m getting at here. It’s more fun to do other projects.

I’m stressing, have to proofread this some time.