Jeez, spent the longest time trying to figure a bug in Unity where 'spawnpoint' wasn't defined. Didn't quite figure out it was a transform value I'd set up in the thing I was trying to instantiate - I kept thinking I hadn't set up the instantiate properly and was going through all sorts of hoops and variations trying to figure what I did wrong!
Eventually clicked and had to figure a way to plug the player value into the instantiated object.
Now I can have the ghosts spawn after a certain position is passed, for a bit of a 'Argh!' effect - which the characters dialog is set up to give as well!
Philosophy in life. Philosophy in life spent gaming. Table top RPGs, mmorpgs, video games, and more.
Showing posts with label unity. Show all posts
Showing posts with label unity. Show all posts
Friday, August 31, 2012
Thursday, August 30, 2012
Unity: Counting substrings inside a string
Tying into the previous post, it seems unity has no actual command for counting the number of instances of a substring in a larger string. I've been hunting around for ages and having found no actual command, I've decided to show some code that does it. The code is about as tight as you can get it without it actually being a command!
As with the last post, I'm using /n instead of \n. This is because when the interpretor looks at \n it decides to ignore the n part, making the search only one character in length. The previous post lists the way you can easily convert /n to \n. I know, it sounds annoying - if I were reading this, I'd think it was annoying. But given that unity gets uppity if you try to enter strings into the inspector (ie, it just wont do line breaks if you do so), you're probably gunna have to end up switching /n to \n anyway. It's just a simple replace command anyway (okay, fine - if you don't want to look at the other post replace is as simple as: guimessage = guimessage.Replace("/n", "\n"); )
But I make excuses like a man! Now, onto the search code!
As with the last post, I'm using /n instead of \n. This is because when the interpretor looks at \n it decides to ignore the n part, making the search only one character in length. The previous post lists the way you can easily convert /n to \n. I know, it sounds annoying - if I were reading this, I'd think it was annoying. But given that unity gets uppity if you try to enter strings into the inspector (ie, it just wont do line breaks if you do so), you're probably gunna have to end up switching /n to \n anyway. It's just a simple replace command anyway (okay, fine - if you don't want to look at the other post replace is as simple as: guimessage = guimessage.Replace("/n", "\n"); )
But I make excuses like a man! Now, onto the search code!
var stringtosearch = "this is the test string /n with several /n line breaks in it";
var searchingfor = "/n";
var substringspresent=0;
var searchextent=stringtosearch.Length-searchingfor.Length; // makes sure we don't search past the end of the string!
for (i=0;i<=searchextent;i++)
{
var sectionscanned=stringtosearch.Substring(i,searchingfor.Length);
if (sectionscanned==searchingfor) substringspresent++;
}
print (substringspresent);
Unity: Getting new line in inspector entered strings to work
It's weird - in a script I had some text defined with \n to create a new line and that worked out.
But when I defined an array of strings and filled them in in the inspector, it'd just show the \n instead of creating a new line!
So here's a solution:
But when I defined an array of strings and filled them in in the inspector, it'd just show the \n instead of creating a new line!
So here's a solution:
guimessage = guimessage.Replace("/n", "\n");In your strings, have /n instead of \n. It'll replace them and for some reason, they then start working! It puts the special magic back in!
Thursday, August 23, 2012
Unity3D : Making a Health Bar
Jeez, I was around when game magazines made jokes about the idea of a 'Health Bar' being some kind of spa in Europe or something. It was all new fangled back then...
Anyway, I had a heck of a time recently looking up a way of making a health bar in unity. I have found a way - not quite perfect, as the bar doesn't shrink perfectly when at low health. But looks nice enough and if the characters that low, they probably will die before they notice the problem. Maybe at some point in future I'll suddenly discover a better way to code it. Anyway, so this is the basic code:
Go into it and open up the box options. Near the top you'll find 'normal'. Open that up and you'll find a background option. It's here you can drag a graphic in - in this case, probably something 200 pixels wide, 20 tall.
Then make sure you add this script to an object (like your main camera), then in the inspector make sure to drag the GUI.skin you made into the slot for custom GUISkin.
Any problems, leave a comment! :)
Anyway, I had a heck of a time recently looking up a way of making a health bar in unity. I have found a way - not quite perfect, as the bar doesn't shrink perfectly when at low health. But looks nice enough and if the characters that low, they probably will die before they notice the problem. Maybe at some point in future I'll suddenly discover a better way to code it. Anyway, so this is the basic code:
var playerHealth : float = 100.00; // use your health var hereYou also need to make a GUI.skin, which you can do by going to your project overview, clicking create and at the bottom there should be the option for GUI.skin.
var playerHealthMAX : float = 100.00; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage
var customGUISkin: GUISkin;
function OnGUI ()
{
playerHealthBar = playerHealth / playerHealthMAX; // playerHealth as a percentage
if (playerHealth > 0)
{
GUI.Box(Rect(10, 10, 200, 20),"");
GUI.skin = customGUISkin;
GUI.Box(Rect(10, 10, (200 * (playerHealth / playerHealthMAX)), 20),""); // health bar is 200 wide at maximum health
GUI.skin = null; // put the GUI.skin back to default
}
}
Go into it and open up the box options. Near the top you'll find 'normal'. Open that up and you'll find a background option. It's here you can drag a graphic in - in this case, probably something 200 pixels wide, 20 tall.
Then make sure you add this script to an object (like your main camera), then in the inspector make sure to drag the GUI.skin you made into the slot for custom GUISkin.
Any problems, leave a comment! :)
Wednesday, April 13, 2011
Quick tip: Fast Unity particle explosions
I searched around for this and found some other people not sure what to do if you want to use particles as an explosion, but it takes ages for the particles to fully form.
If you set the max size to 1 and the minimum size to .9, they'll already be pretty much fully formed as soon as you call the particles. Looks really nice too! :)
If you set the max size to 1 and the minimum size to .9, they'll already be pretty much fully formed as soon as you call the particles. Looks really nice too! :)
Labels:
explosion,
fast,
fast explosion,
particles,
programming,
tip,
unity
Sunday, April 3, 2011
Basic unity tutorial: Pong
Found a great little unity tutorial to make a pong-like. Beginer level (for people like me!)
http://www.rockpapershotgun.com/2010/01/20/rock-paper-shotgunity-post-12/
Couple of tips - in starting a new project you want to include the physic materials. Also where it has javascript listed for the key controls, you want to change the talking marks in it to just the regular one you get (I suspect the blogging platform auto converted them to fancy open and close talking marks, which just causes a bug in unity).
http://www.rockpapershotgun.com/2010/01/20/rock-paper-shotgunity-post-12/
Couple of tips - in starting a new project you want to include the physic materials. Also where it has javascript listed for the key controls, you want to change the talking marks in it to just the regular one you get (I suspect the blogging platform auto converted them to fancy open and close talking marks, which just causes a bug in unity).
Thursday, March 24, 2011
Screen shots in spaaaaace!
Just a cool shot from another developer who also posts at the indie resource forums!
I think it uses unity.
I think it uses unity.
Subscribe to:
Posts (Atom)