unity3d

Unity – Audio EFX – OnClick – GetKey

Unity – Audio EFX – OnClick – GetKey

1. Put a Sphere in the Scene

2. MAIN TOP MENU> Assets> Import New Assets> load a file.mp3

3. Projects> Assets> DRAG AND DROP file.mp3 over the Sphere
NOTICE: you have to put the mp3 in the scene or it does not play! WE NEED ABSOLUTELY AN AUDIO SOURCE!

4. Create a script audioplay.js> DRAG AND DROP audioplay.js over the Sphere
NOTICE: assign the js at the same object that has the mp3 file

5. The Script:

#pragma strict

function Start () {

}

var audioplay : AudioClip;
function OnMouseDown ()
{
        // When you click over the object
        audio.PlayOneShot(audioplay, 0.7);
}

function Update () {

}

6. Select the Sphere> Inspector> Audio Source> Uncheck ‘Play On Awake’ or it plays at first frame when the game stars.

7. Select the Sphere> Inspector> audioplay.js> Audioplay> click small circle icon and assign the file.mp3

DONE!!!

Change the script to GetKey and start audioplay:

#pragma strict

function Start () {

}

var audioplay : AudioClip;

function Update () {

 // You have to use - GetKeyDown - because return TRUE only in the first frame.
 // If you use - GetKey - return multiple TRUE throught frames and audio restarts every frame!
 if (Input.GetKeyDown("up")) 
 audio.PlayOneShot(audioplay, 0.7);

}
By |Unity3D|Commenti disabilitati su Unity – Audio EFX – OnClick – GetKey

Unity – 2D Basics – How to create a Scene

Videogames Development – Unity – 2D Basics – How to create a Scene

Unity is a fully integrated development engine that provides rich out-of-the-box functionality to create games and other interactive 3D and 2D content.

NEW PROJECT

1. MAIN TOP MENU> File> New Project> Setup defaults for: 2D> ‘Create’, Unity will restart in 2D mode.

or

1. MAIN TOP MENU> Edit> Project Settings> Editor
2. Inspector> Editor Settings> Default Behavior Mode> 2D (behaviour = comportamento)

NOTICE: the new project has been saved in C:\Users\a\Documents\New Unity Project

VIEWPORT

MAIN TOP MENU> Window> Layouts> Default

Scene> SCENE TOOLBAR> activare/deactivate ‘2D’ button

CAMERA (GAME AREA)

In the viewport click over the Camera icon> Inspector> Camera> Viewport Rect: X=0 Y=0 to center the game area, W=16 H=9 to setup 16:9 aspect ratio

GAME PREVIEW

Scene> SCENE TOOLBAR> ‘Game’ label> ‘Free Aspect’ rollout> 16:9
Now camera view and game preview have same aspect ratio.

Others Setups:

Free Aspect
5:4
4:3
3:2
16:10
16:9

Standalone (1024×768) setup this:
Edit> Project Settings> Player> Inspector> Settings for PC, MAc LinuX Standalone
Uncheck ‘Default is Full Screen’ and setup ‘Default Screen Width’ and ‘Default Screen Height’

LAYERS

You can use layers to organize your project.

1. TOP RIGHT> ‘Layers’ drop down menu> Edit Layers…> Sorting Layers> ‘+’ button> name the layer ‘Main-Character’

2. TOP RIGHT> ‘Layers’ drop down menu> Sorting Layers> ‘+’ button> name the layer ‘Background’

3. DRAG AND DROP a layer in the stack to change layer order, the top layer in the stack is the foreground, the bottom layer is the background.

It is the opposite of Photoshop layer stack

IMAGES IMPORT

1a. MAIN TOP MENU> Assets> Import New Asset> piggy.png
Assets are multimedia files imported to make a videogame. They can be images, music, 3d objects etc…

1b. Copy from Windows the multimedia files inside ‘Asset’ Folder; to see the ‘Asset’ folder location over Asset folder RMB> Show in Explorer

You can see all Assets in the project in the window ‘Project’> ‘Assets’ folder

It is a good way organize Assets in different folders: Sound – Textures – 3D Models – Scripts etc…

Here you can search with name, or using small icons you can select to show/hide:

– AnimationClip
– AudioClip
– Font
– GUISkin
– Material
– Mesh
– Model
– PhysicalMaterial
– Prefab
– Scene
– Script
– Shader
– Texture

‘Assets Window’> bottom slider to resize

2. Hierarchy> select ‘piggy’> Inspector> Sprite Renderer> Sorting Layer> Main-Character
Now ‘piggy’ is inside the layer ‘Main-Character’

3. TOP RIGHT> ‘Layers’ drop down menu> Sorting Layers> use the ‘padlock’ icon on the left to lock/unlock the layer to prevent accidental selections in viewport.

TAGS

It is useful assign tag to an object, it will be easier to find an object or call it with scripts.
Example: we can tag all enemy with an ‘Enemy’ tag, even they have different size, mesh or skills.
Inspector> Tag

In a script you can find object with a particular tag, see the code:

var enemy = GameObject.FindWithTag ("Your Tag Name");

IMAGES IN GAME

1. Project> Assets> drag and drop the pig iside the viewport
2. Inspector> Sprite Renderer, setup ‘Sorting Layer’ (the layer where the sprite is stored), ‘Order in layer’ (the order in the layer, example: 7 is over 8 and 8 is over 9 etc…)
3. Inspector> Transform> setup Position, Rotation, Scale or use the handles in the viewport

RESET VALUES

Inspector> Transform> Little Gear Icon> Reset

LOCK INSPECTOR

Inspector> on the right the lock icon> the current object will not change even you select another object

GAME OBJECTS and COMPONENTS

Inside Unity every object in the scene is a GameObject, a GameObject is the sum of all Components (the stack of scripts, physic, modifiers you can see in the Inspector window).

Add Components: Select an Object> Inspector> ‘Add Component’ button or Select an Object>MAIN TOP MENU> Component
Modify Components: Select an Object> Inspector> in the Component Slot click in the small gear icon to Reset / Remove / Move Up-Down / Copy

PREFAB – Prefabricated Objects

NOTICE: Only Prefab can be cloned and instatiated

Create a Prefab:
1. If you do not have a prefab folder create it Project> Create> Folder> give it the name ‘Prefabs’
2. Hierarchy> DRAG AND DROP the GameObject inside ‘Prefabs’ folder in Projects> Assets> Prefabs
3. Hierarchy> Delete the old Gameobject from the scene
4. Project> Assets> Prefab> DRAG AND DROP the new Prefab in the scene

Instantiate a Prefab:
1. Project> Assets> Prefab> DRAG AND DROP the new Prefab in the scene
2. Project> Assets> Prefab> DRAG AND DROP another Prefab in the scene
3a. Project> Assets> Prefab> CLICK the original Prefab and from Inspector> change Scale value> all instanced Prefabs in the scene will change
OR
3b. Select in the viewport a Prefab> Inspector> change scale value> Inspector ‘Apply’, to apply the change to all Prefabs in the scene.

Break Prefab Chain:
Select in the viewport a Prefab> MAIN TOP MENU> GameObject> Break Prefab Instance> now the Prefab is property independent

BACKGROUND

You can use the same techniques to load a background image, and store it in ‘Background’ layer

SAVE THE SCENE

MAIN TOP MENU> Save Scene As…> type a name for your Scene

NOTICE:
Unity will put the Scene inside Project> Assets
If you do not save the Scene your composition work will be lost.

SAVE THE PROJECT

MAIN TOP MENU> File> Save project

NOTICE:
Unity Project in the main container, it contents all Assets (Scenes, images, sfx, code etc…)

By |Unity3D|Commenti disabilitati su Unity – 2D Basics – How to create a Scene

Unity – Physics 2D – Basics

Videogames Development – Unity – How to add Physics 2D

ADD PHYSICS

Setup the Physics 2D engine

MAIN TOP MENU> Edit> Project Settings> Physics 2D> Inspector> Gravity

Create a Sphere over a Box, the Sphere will fall and will collide overe the box

1. Hierarchy> select Sphere
2. MAIN TOP MENU> Component> Physics 2D> Rigidbody 2D
3. Hierarchy> select Sphere> Inspector> Rigidbody 2D> setup parameters
4. Hierarchy> select Sphere> Inspector> ‘Add Component Button’> Physics 2D> Polygon Collider
5. Hierarchy> select Sphere> Inspector> Polygon Collider 2D> setup parameters

6. TOP CENTER> click over ‘Play’ button, the Sphere will fall
7. TOP CENTER> click over ‘Play’ button, to stop simulation

8. Hierarchy> select Box
9. MAIN TOP MENU> Component> Physics 2D> Polygon Collider 2D
10. Hierarchy> select Sphere> Inspector> Polygon Collider 2D> setup parameters
11. Viewport> MAIUSC+LMB click over the wireframe of the Polygon Collider to change it

12. MAIN TOP MENu> Assets> Create> Physics 2D Material
13. ‘Assets’ window> select New Physics2D Material> Inspector> setup Friction / Bounciness
> Bounciness >

14. Hierarchy> select Sphere> Polygon Collider 2D> Material> New Physics2D Material
15. Hierarchy> select Box> Polygon Collider 2D> Material> New Physics2D Material

16. TOP CENTER> click over ‘Play’ button, now there is collision
17. TOP CENTER> click over ‘Play’ button, to stop simulation

By |Unity3D|Commenti disabilitati su Unity – Physics 2D – Basics

Unity – Particle System

Videogames Development – Unity – How to add a particle system

ADD PARTICLE SYSTEM

1. MAIN TOP MENU> GameObject> Create Other> Particle System

MODIFY PARTICLE SYSTEM

1. Hierarchy> select Particle System> Inspector> setup parameters

By |Unity3D|Commenti disabilitati su Unity – Particle System

Unity – Build a Game – Basics

Videogames Development – Unity – How to build a videogame

A videogame project can be built for:

– Web Player
– Flash Player

– Google Native Client

– PC Standalone
– Mac Standalone
– Linux Standalone

– Windows Store Apps

– IOS
– Android
– Black Berry
– Windows 8 Phone

– XBox 360
– PS3
– Wii

BUILD A GAME

Load a Project

1. MAIN TOP MENU> File> Build Settings…> Choose your platform

2. MAIN TOP MENU> File> Build Settings…> ‘Player Setting’ button> Inspector setup ‘PlayerSetting’, the most important are:

– Company Name
– Product Name
– Default Icon> ‘Select Button’> choose a Asset in the list
– Default Cursor

– Resolution and Presentation> Supported Aspect Ratios
– Icon
– Spalsh Image
– Other Settings

3. MAIN TOP MENU> File> Build Settings…> ‘Buid’ or ‘Build and Run’> Choose a folder to save the game.

4. Close the ‘Build Settings’ window using the red cross on top right.

Open the folder where the games has benn saved and… great! Now you can play your own game!

By |Unity3D|Commenti disabilitati su Unity – Build a Game – Basics