Skip to main content

Posts

Showing posts with the label Unity 3D

Handling Music and Sound Effects In Your Games

Initiative  While developing Treva's Adventure I had to figure out a way to handle multiple music tracks and sound effects in a clean manner or suffer horribly.  What was going to help me achieve a simple solution was taking all the different sounds and centralizing them in a single class in order to black box them.   Any other code trying to play a sound wouldn't even know the sound file's name.   All code trying to play a music track would reference a enum that defines all the track names. Defining The Class Creating The Enum When I first started defining types in my enumeration,  I was naming the types to be exactly like the file name.  For a scary sound effect I had found a file named "ghost breath".  So around my code would be scattered lines like SoundManager.Play(SoundEffectType.GhostBreath);  This was fine until I found a sound that better fit the situation it was being used in,  and decided to use "ghost breath" for a...

Creating 2D Animation For A Side Scroller In Unity3D

Update 3-13-14 Unity kinda released some really nice 2D tools, I really don't know how relevant or helpful this is now. I've been working on a 2D side scroller. Being my cheap self I'm not going to bother buying some helpful tool that would probably speed up everything a substantial amount, and created my own method of 2D animation for things in Unity3D. This code does not work with a sprite sheet, but I hope it makes it easy enough for you to incorporate one if you wanted to. Warnings:  The animation is displayed by updating the meshes material's texture each frame (this can mess up at high frame speeds). This code is assuming the 2D sprites are moving along the X and Y axis. When changing directions, it is scaling the transform negatively across it's x axis. Its assuming default direction facing is right. using UnityEngine; using System.Collections; public enum Directions{left,right} public class FlatCharacterAnimation : MonoBehaviour {...

Making Multiple Instances of Windows With Same Window Method

So when developing a small little web-player, I ran into the problem of trying to render multiple instances of the same window function. My plan was for my user interface component to iterate through all the thought components and for each thought, render its contents in a new window. The windows need to be draggable also. My first attempt of this implementation failed. Rect thoughtRect = new Rect(20, 20, 200, 100); void OnGUI(){ for (int i = 0; i < allthoughts.Length; i++){ thoughtRect = GUI.Window(0+i, thoughtRect, DoThoughtWindow, ""+allthoughts[i].GetContent()); } } void DoThoughtWindow(int windowID) { GUI.DragWindow (new Rect(0, 0, 10000, 10000)); } For those who don't see the problem with this, I'll explain. All three windows are using the same Rect  thoughtRect  so all the windows are drawn on top of eachother. You might think this won't pose a problem due to the windows be...

Choose Binding Time Consciously, Awake and Start functions for Initialization

So I ran into a simple but annoying problem when I was trying to hurry up and finish a game I have been working on for a while so I could move on to start a new idea. I was being stupid an hard coding the last features I wanted to include in the game, and of course came across a problem that I spent 30 minutes on that I could of easily avoided if I was thinking. Here is how the problem began. I needed a way to keep up with all the Light projectors in my scene and figure out which ones are close to me. This sounded easy enough, but alas I managed to screw it up. The code I wrote  used an array that was set on the awake  method using GameObject.FindGameObjectsWithTag("TagName"); In my scene, the player is already in my scene, and the lights are not. When my scene is played, a script called GameInit calls the city generator which in turn calls the multiple factories, one being the factory that generates all the lights in my game. Although it looks like when my scen...

Optimization Tip #1 - Windows As Textures

A lot of games back before the super consoles of PS3 or the XBox 360, companies had to go to extra lengths to save on faces to boost the performance. Technology throughout the past 10 years has grown tremendously advanced for such a short period of time. We are though, still not to the point of having the ability to run the graphics of a PS3's prestige on an everyday tablet and not have major issues. All developers have to optimize their games of course, especially those who are developing for android or IOS. One way everyone has seen is UV mapping entire buildings with a texture. Doors and windows are flat and look like some one cut pictures out of a magazine and glued them on a cardboard box as part of an art project. Now I'm not denouncing the method, its actually kind of crazy to think of trying to model a building in crazy detail. A lot of what makes a realistic looking model is the detail of the texture, instead of the complexity of the it's m...

Importing Models and Animation From Blender

NOTE - If nothing is showing up in your preview section, then you have exported the model wrong. Remember you need to have selected in Blender what to be exported. If you have done this, try reversing the faces in the model and re exporting. So after you have your model from Blender rigged , animated , and exported, you can bring it in to the unity editor.  Scale factor is by default 0.01, to change that, try looking at this link .  Anyways, your going to want to drag you model to your scene and adjust the scale factor till you have the desired size (scale factor 1 is rarely what you ever want it, its huge). To start defining your animation, click the + button that's highlighted purple in the picture. You'll get something like this where you can name your animation, define starting and ending frames, and wrap mode. Remember back when you were making the animation for your model? you had to define frames and you had a certain amount of frames ...

How To Import and Use Fonts

-Made by Chrome Fx Films To use different fonts in your game, your going to have to acquire some fonts. You can go somewhere like 1001 free fonts  and download ones you like and want to use. When you download the file, you'll need to extract the file if its in  a .zip The text file should have the extension .ttf. If you text files have a FFIL extension, simply rename the font file (yourname.ttf). Drag the text file into unity to import. By default the Character variable should be labeled Dynamic . The Dynamic setting means  that Unity won't pre-generate the texture, so if you look at your material containing your font, It probably will be blank. (read more here) Now your over all objective should be getting your font to look like this (unless you prefer dynamic): Where the characters of the font are not jumbled up and visible. Now usually all you have to do to achieve this is change the  Character  variable fr...

How To Make a Hellish Looking Sky Box

I came across this problem while constructing my scene of Hell in a little project I've been working on, and could not find a reasonable sky box on the web for what I want. Maybe I was not looking hard enough, but I ended up making nice substitute. If you think the sky box looks familiar, then your right. The Sky box I'm using is already packaged with Unity3D! To import the sky boxes Unity has made for you,  simply go to Assets>Import Package>Skyboxes.  The sky boxes will appear in your projects tab under a folder named "Standard Assets". To make this sky box, first you must find the folder containing all the sky box materials and open it up. In it will be a list of sky boxes for your disposal. To get this skybox, I decided to tweak the "StarryNight Skybox" (But the "MoonShine Skybox" looks pretty cool also!).  Select the sky box and view it under the inspector tab. Underneath the properties there will be a tint color variable allowin...

How Do You Make a Skybox in Unity3D?

So we have a nice hellish looking scene in our Unity 3D game, and our default blue background really doesn't belong here   You can change the color of the background.. Or you could add a skybox for more detail and awe your players with the scenery. An example of our Scene with a skybox : So, you may be asking your self  "How do I Make a Skybox ?" .  Before I begin, Unity has already created many nice presets for everyone's use. You can simply go to  Assets>Import Package>Skyboxes. The skyboxes will appear in your projects tab under a folder named "Standard Assets". So to begin with making your own Skybox, you should start out making a new material in your projects tab Now change your new material's shader properties from diffuse to RenderFX>Skybox You material should now show 6 slots(up, down, left, right, front and back) in which to put your textures in to build your skybox like so, Now you s...

How To Make A Gun Shot Sound (SFX On Unity 3D)

When it comes to audio in Unity, there are four components: Audio Clip , Audio Source , Audio Listener , and Audio Re-verb Zone . Audio Clips are the actual audio file imported into your game. Unity supports file formats: .aif, .wav, .mp3, and .ogg. When imported, you can compress them greatly, with the price of loosing some quality. You can do this by first selecting the audio clip, view it in the inspector. Under the Audio Importer component, you can switch the audio format from Native to the audio clip, to a compressed format applied by Unity. You can change how compressed the file is by dragging the bar at the bottom, then hitting apply. You can get plenty of free good SFX from a site called  freesound.org . All you have to do is create an account for free , and download all the sounds you want. I found a nice gun shot sound here . Simply download and load into your Project. Audio Source actually plays the audio clip in your scen...

How To Sync Up Unity Projects

Want to work on the same Unity Project over multiple computers? Even have multiple people work on one project all at once? Well theirs a free easy method that works! Step 1: Go to dropbox.com . Make an account and download their soft ware. You have 2GB of space for free! It's not much to expand on that either, but I've yet to run across any space issues. Step 2: Open up Unity. Go to file>New Project. Under project location, Browse, and create a new folder under your dropbox folder. Make that where your project lies. Step 3: Go share! You can install dropbox at any computer and log in with your account. Your assets will be loaded into the dropbox folder. To open you Unity project from dropbox, simply go to file>open project, then find the folder that contains your unity project and hit open. You can also log into your dropbox account, and send other drop box users an invitation to share your folder, you have to know their email. When they accept their drop box ...

Making A Laser Pointer

Want a frieking lazer pointer? BOOM. Attach a Line Renderer component to the object you have the script below attached to. (Component> Miscellaneous>Line Renderer) Code: function Update () { var lineRenderer : LineRenderer = GetComponent(LineRenderer); lineRenderer.useWorldSpace = false; lineRenderer.SetVertexCount(2); var hit : RaycastHit; Physics.Raycast(transform.position,transform.forward,hit); if(hit.collider){ lineRenderer.SetPosition(1,Vector3(0,0,hit.distance)); } else{ lineRenderer.SetPosition(1,Vector3(0,0,5000)); } } @script RequireComponent(LineRenderer) Thank  3dDude for the script. Original Source here .

Limiting Rotation of an Object

I made this script for my Beach Defender Game . It was to keep the player from rotating more than I want his to, because I'm a mean person like that. Any way it took me awhile to figure out how to not make them turn to far to the left, right, up, down.. You know. Here's the script. Pretty simple. Put under Update function. Feel free to suggest different ways. Or ask any questions. var upDown = Input.GetAxis("Vertical") *Time.deltaTime * (rotateSpeed-10); var leftRight = Input.GetAxis("Horizontal") * Time.deltaTime * (rotateSpeed-10); //making it turn up or down if(transform.localEulerAngles.x < 25 ){ transform.Rotate(upDown, 0, 0); } else if (transform.localEulerAngles.x > 295){ transform.Rotate(upDown, 0, 0); } //making it turn left or right if(transform.localEulerAngles.y < 65 ){ ...

Simple Reload Script

Sooo I'm working on a simple Top Down Shooter game. And This was the script I came up with for the reloading part of the script. Very simple. I think you can use it with any weapon really. You reload with space. You  need to put your own method of Firing in this script. Comment if you have any questions private var reloading = false; var reloadTime = 2.0; private var st : int; var clipAmmo = 50; var clipSize = 50; var stockAmmo = 100; function Update () { if (Input.GetKeyDown ("space")){ if(clipAmmo<clipSize && reloading == false && stockAmmo !=0){ SaveTime(Time.time); reloading = true; } } if(st < Time.time && reloading == true){ Reload(); } } //Making it show that you are reloading. function OnGUI(){ if(reloading == true){ GUI.Box(Rect(250,200,100,25),"Reloading"); } } function Reload(){ if(clipAmmo < clipSize){ var neededAmmo = clipSize-clipAmmo; if(stockA...

Beach Defender

Just uploaded the game I've been working on for the past 2 months on Kongregate. Pretty awesome. I have been working on and off on it for a while and now I can get back to posting on this blog. Play the game here !

Debug Rays =D

I use them for looking at what enemies are looking or aiming at. Since it's Debug it only shows up in the scene view and not in your actual game play. They  can be very useful sometimes. Here's an example of what they look like. The red lines are the debug rays.

Question about a component?

There is alot in Unity to learn. So the makers of Unity decided to be awesome and put full explanations of what each component does. When ever your in question of what something does. Just click the question mark in the top right corned of the component in the inspector pane.

Where To Host Your Unity 3D Game

Since I've been busy with a new game and other stuff I haven't bothered much to explain stuff. Been trying to learn some more but haven't quite gotten anything useful yet. Anyway, if you have a ready webplayer game you want people to play but you don't know where to host it, Kongregate is a nice place.

'Building' Your Game

**A brief overlook** Unless your games only going to be made up of 1 scene, your going to need to mess with your build settings. Start with going to: File>Build Settings... or by pressing Ctrl+Shift+B Scenes In Build contains all the scene that will be made in the game when you click build. The number next to the scene name is how you would reference in the script. Platform window contains all the platforms you can build for. XBox360 and PS3 need contracts though. I don't know how to obtain those. The default platform when you start a new project is PC and Mac Standalone.