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

Sunday, 3 March 2013

Easy Property Syncing over Network in Unity

A while back I was experimenting with network code in Unity. To make syncing variables easier I made something I called a 'NetworkMonoBehaviour'. This makes it easy to do exactly what the titles says, easy property syncing over a network in Unity. So here's how it works:

Properties

So we start with a field and, since we like clean code, we encapsulate it with a property.

Networking

Though now, if we change it, it will only be changed locally. This isn't what we want in a network game. So I wrote some code that makes it possible to do this:
This will change the _speed field locally and tell all connected players to do the same. It will only do it if the speed actually changed to minimize network usage. This is made possible by using my NetworkMonoBehaviour. Instead of deriving from MonoBehaviour you derive from NetworkMonoBehaviour with the classes you want to sync. They are still MonoHebaviours, but now indirectly. This makes it possible to used the a few very useful methods in this class. NetworkChangePropertyIfChangedNetworkChangePropertyAndCallMethodIfChanged these are the most used, and their names should explain what they do.
If you just want to use it, and don't want to know how it works, you can stop reading here. Just download the NetworkMonoBehaviour here.

NetworkMonoBehaviour

This is the entire NetworkMonoBehaviour class:

All of these methods will work with int, float, string, Vector3 and Quaternion. Otherwise an exception will be thrown.
The class is actually pretty simple. After checking if the value actually changed, I check what type the variable is (int, float, string, ...) Then I cann the correct RPC. These RPC's are methods that get called on all connected peers, and I made one for every type, which changes a field based on it's name.


Voila, now you can easily sync your properties over a network.
Thanks for reading!

Tuesday, 5 February 2013

First-Person Adventure Kit (Unity)

Lately I've been making a lot of unfinished project. Most only go as far as making a basis for the game, so I've decided to put some of this online so other people (you!) can use it and hopefully learn something of it. You can use those code as-is, or alter it in any way you want. You can also use it in any game you want, even ones you sell.

What does it do?

So, first things first. What does this project do?
It's a very simple set-up to make a first-person adventure game. It contains a player script, which can walk and look around. On top of that it can interact with object in the world, and that's where it gets interesting.
But first, here's a link where you can try it out. Walk around with awsd (or zqsd for azerty) interact with leftmouse button (the wheel can be turned backward with the right button.)

How it works

Player

The player should be pretty straight-forward. It's a simple script which does the walking and the looking. It has basic first-person controls.

Variables

You can set it's walk-speed (m/s), jump strength (3.5 will make you jump just over one meter) and it's look distance (in meters.)

Pushing

By default a Character Controller can't push Rigidbodies in the world, so I added a few lines of code so it can push, the 'Pushing Power' variable sets how hard the player can push. (Look in OnControllerColliderHit for how this works.)

Objects

Now the interesting part. How does the interacting work. I've tried to make it so it's very easy to add new interactions without having to modify any code, only add a new script.

So, let's take the little green cube as en example.

Interactable Object

First of all, each and every object you want to interact with needs to have an "Interactable Object" script attached to it. This is used to gather all the interactions and tells the player-script that this object can be interacted with. It also cashes the interactions so only the first time you look at the object they need to be collected. This is also the script the player communicates with.

Interactions

If your object is an Interactable Object you need to add interactions. Interactions are custom scripts (I already made a few as an example) which define what your object does when the player interacts with the object. Instead of deriving directly from Monobehaviour they derive from Interaction (which is a monohebaviour.)
...

If you do this, you can override a bunch of methods and by that define what will happen when you interact with the object. You can add multiple interactions to the object and they will all work simultaneously. If you want an interaction to only work when something else has happened first, you can do this with custom code you write yourself.

Looking at the Interaction class you can see there's a lot that can be done with this:
So if you derive from this class and override one or more of these methods it'll work like magic! As you can see, by default the methods are empty, so if you don't override them they'll just do nothing.

Rotator

Rotator is a very, very simple class I wrote to make the wheel rotate when you interact with it. The wheel is the round red/green/blue thing on the blue box.
So, while I'm holding the use-button and looking at the object the HoldingUse method will be called. This will make the object rotate. While I'm holding the inspect button it'll rotate the other way. Very simple, almost no code and it just works!

DebugInteraction

Lastly I wrote a little DebugInteraction. This script shows when which method is called. All methods that are called once (StartXXX, EndXXX Use, LongUse, Inspect and LongInspect) are written to the console. Those that are called every frame keep how long they are being called and it's shown in the top-left of the screen (as you can see in the web-player.)

Download

Now you know the basics go check out more of the code and play around with it a little! You can download the project right here.
Note: I have custom input settings, so if you use it in your own project you might need to change these a little.


If you want some of the code explained more just comment here or email me, and I'll be happy to help!

Have fun coding!

Tuesday, 6 November 2012

iOS Gyro Controller for Unity

Hi,


While playing around with Unity Remote, to control my game with my iPad, I noticed that the gyroscope doesn't work. This sucks, so I tried to find a solution. I found one from the guys of Quick Fingers, though it needed some tweaks, so I'm writing an updated version here. You can read the original tutorial here, mine will be shorter and more to the point.

Needed Soft/Hardware

  • Unity3D and a mac
  • An iOS device with Gyroscope (any new iPhone, iTouch or iPad)
  • Control for iOS
  • Osculator (trail, 20second wait every 10minutes)

Steps

  • Install and start  Osculator and open the QuickFingers file
  • On your iOS device open Control, in 'Interfaces' add a new one and enter this URL:  http://www.quickfingers.net/unitygyro/layout.js
  • In Unity setup your Input Settings like this: 

  • Now you can use this in Unity, here's how to convert it to a quaternion:
float gyroX = Input.GetAxisRaw("GyroX") * 360.0f;
float gyroY = Input.GetAxisRaw("GyroY") * 360.0f;
float gyroZ = Input.GetAxisRaw("GyroZ") * 360.0f;
Quaternion rotation = Quaternion.Euler(gyroX, gyroY, gyroZ);

Now you can use rotation as the rotation of your camera yo create a window into your game. Kind of, since no image is send to the iPad... Still, it's great for testing and a lot of fun!

Wednesday, 13 June 2012

Choosing an iOS game engine

Hi,

I'm planning on making an iPad game, so I was looking around for an engine to use. I looked through a whole lot of engines, tried a bunch and selected three. If you are looking to do some development yourself, read this and you'll know what engine is perfect for you.
All engines in this post have the ability to compile for iOS, though they all can also publish to other platforms. I'm here to select one for an iPad game, but you might find the perfect one for an other platform too.


Cocos2D for iPhone

As the name suggest this one is for 2D games. It's really powerful and uses Objective-C as a programming language so it's the only truly native engine. Objective-C isn't easy though, so this engine is probably for people who already know some programming. It can make apps for both iPhone and iPad (and Mac too if you're interested in that.)

Cocos2D is for (experienced) programmers who want to make iPhone or iPad apps.


PhoneGap

This is another 2D engine. The big difference though, is that it doesn't make native apps. It uses HTML5 and Javascript to make the app. Thanks to this you'll be able to make iPhone and iPad apps, but also play your game in the browser, on android and pretty much anything else. Javascript is really easy so if you just want to make awesome games without having to learn a complex programming language, this is for you.

PhoneGap is for anyone who wants to make games, with or without programming knowledge. It's absolutely perfect for people with web development knowledge, since you'll be able to use the language you already know.


Unity 3D

Unity is awesome, it's definitely the best free 3D engine out there. You can program in c#, java and boo, all are easy so you can select the one you already know or just select the one that looks coolest. Unity is free to use for desktop and browser apps, though will cost you a bit when you want to publish to iOS. You can always make and try out your game on your pc and then later buy the iOS license. I have worked a lot in Unity and it's just really fun to make game in it.

Unity is for anyone who wants to make 3D games for any platform.



I hope this post saved you the time looking for an engine and that you now know which one you'll be using.

Update: Another great option might be Haxe. I wrote a new post about this and Dart, might also be a good option if you're making iOS games: http://anoddcupoftea.blogspot.be/2012/09/web-apps-without-javascript.html

Update: I added one to the list, Sparrow. http://anoddcupoftea.blogspot.be/2012/11/choosing-ios-engine-update-sparrow.html

Have fun making games!