unity3d

Unity 3D Game Engine – Blend Tree

Unity 3D Game Engine – Blend Tree

Blend Tree is useful to mix two or more Animations, for example if you have animation for ‘Running Left’ ‘Running Right’ ‘Runing Foward’ ‘Running Backward’.

1. Animator Window> RMB over an empty space> Create a State> From New Blend Tree>

2. Inspector> inside Motion Slot you will see ‘Blend Tree’ NOT a single Animation!

3. Animator Window> double click ‘Blend Tree’> Inspector:

– Name of Blend Tree

– Blend Type:
— 1D (one dimensional), it uses a single parameter, for example ‘Speed’ (value from 0 to 1)
Usa un parametro singolo per gestire l’animazione

— 2D Simple Directional, it use direction to make transitions
Usa una animazione per ogni direzione, ad esempio walkfoward walkbackward walkleft walkright

— 2D Freeform Directional, it use direction to make transitions
Usa più animazioni per la stessa direzione, ad esempio walkfoward walkbackward walkleft walkright + runfoward runbackward runleft runright

— 2D Freeform Cartesian, it use direction to make transitions

– Motion> ‘+’:
— Add Motion Field
-> DRAG AND DROP an Animation, for example ‘Run’
-> DRAG AND DROP an Animation, for example ‘Walk’
-> DRAG AND DROP an Animation, for example ‘Sneak’
or
-> click over the small circle icon
— New Blend Tree, to add nested Blend Trees (nidificati)

4. Inspector> Play animation preview and change Animator Window> Blend Tree level> Speed slide you will see:
– Speed 0 -> Run
– Speed 0.5 -> Walk
– Speed 1 -> Sneak

5. Inspector> Use the handle in the left to DRAG UP or DOWN the Motion Slots to change animations order.

– Automate Threshold (soglia):
– check it to automate threshold (crea un’interpolazione automatica)
– uncheck
— Speed
— Velocity X / Y / Z
— Angular Speed Rad / Grad

– Adjust Time scale
— Homogeneous Speed
— Reset Time Scale

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Blend Tree

Unity 3D Game Engine – Animator Controller

Unity 3D Game Engine – Animator Controller

Animator Controller

Animator Controller is a ‘state machine’, it is used to mix and blend animations.
Animations are stored inside blocks and you can drive blocks using parameters, parameters are changed by scripts.

The workflow is:

Character GameObject -> Animator Component -> Animator Controller (it is used to define animation clips) -> Animations -> Parameters -> Scripts
-> Avatar (it is used to define rigs and skeleton)

It is said that it is complicated but very useful to make realtime-interactive animations, let’s go on!

1. Select a 3D Model> Inspector> ‘Add Component’> Miscellaneus> Animator (prima di tutto creo Animator Component)

2. Project> RMB> Create> Animator Controller, give it a name

3. DRAG AND DROP Animator Controller over Hierarchy> Inspector> Animator Component> Controller

4. Double LMB over Animator Controller will opes Animator window

Animations

They are the animation clips

5. Project> DRAG AND DROP Animations over Animator Window, example ‘Idle’ (poltrire) and ‘Run’ (correre) animation

6. RMB over your default animation block> Set As default, example ‘Idle’

7. Animator Window> select your animation ‘Idle’> Inspector>
– Speed = 1 to run animation at normal speed, 2 to doble speed etc…
– Motion: the motion clip
– Foot IK: check it to reduce or remove foot slipping (elimina lo slittamento dei piedi)
– Mirror: it flips the animation left to right
– Transitions: list of transitions for this animation

Parameters

Parameters are used to manage behaviours and to take decisions, they can be:

– Float
– Int
– Bool
– Trigger

8. Animator Window> Bottom Left> Parameters> ‘+’> Float> create ‘Speed’ parameter.

Transitions

Transitions lets you move smootly from an animation to another.

9. Animator Window> RMB over ‘Idle’ Box> Make Transition> LMB over ‘Run’ Box

10. LMB over the Transition Arrow

11. Inspector, bottom, Play the Preview of transition

12. Inspector>

– Mute: stop the effect transition, use it for debug pourpuse only, uncheck for build the executable
– Solo: stop the effect of transition that comes from the same state, use it for debug pourpuse only, uncheck for build the executable
– Give a name
– Atomic: check -> the transition is not interruptible by other transitions
– DRAG the Transition Blocks
– Conditions

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Animator Controller

Unity 3D Game Engine – Animation – Animator Component

Unity 3D Game Engine – Animation – Animator Component

With the Animator Component you can drive and mix animations inside Unity 3D Game Engine.

Overview

1. Select a 3D Model> Inspector> ‘Add Component’> Miscellaneus> Animator

Parameters:

– Controller: it is a ‘state machine’, it is used to mix and blend animations.

– Avatar: it is used to define rigs and skeleton. If you have a generic object without skeleton rig, Avatar will not be required.

– Apply Root Motion:
check -> the character move around (se attivo il personaggiosi anima e si muove nello spazio 3D, necessario se il personaggio si muove in un mondo 3D)
uncheck -> the character shand in place (se inattivo il personaggio si anima ma resta sul posto, utile se ad esempio sarà il fondale a scorrere)

– Animate Physic:
check -> animation will be executed in time with the physic engine, it is check if you use a Rigid Body component with your character (lo attivo se ho un Rigid Body attaccato al mio personaggio che deve interagire con la fisica dell’ambiente circostante)
uncheck -> you will leave unchecked if you do not use physic

– Culling Mode: (Modalità abbattimento/ottimizzazione)
Based on Renderers -> the animation will run only if it is rendered (animazione cicla solo se deve essere renderizzata)
Always Animate -> the animation will run ever (animazione cicla sempre anche se il personaggio non è renderizzato)

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Animation – Animator Component

Unity 3D Game Engine – Android – Save Data – Local

Unity 3D Game Engine – Android – Save Data – Local

How to save points, user preferences and other datas inside an android device.

IMPORTANT!!!

Unity> MAIN TOP MENU> File> Build Settings> Player Settings…> Other Settings> Configuration> Write Access> External (SD Card), this setup will create inside Android Device using Windows 7 -> Phone/Android/data/com.Company.LuceDigitale

It this equal the write inside AndroidManifest.xml ->

This scene is a simple Tap Counter, let me introduce instructions, briefly:
a. if you Tap over the objects in the scene, counter will increase.
b. if you tap over the button ‘Save Data’, counter value will be saved inside android device.

In the game we there are 2 counters (GUI Text):
1. the realtime player score text
2. the saved old score text

Inside Hierarchy create the game Objects:

– Sphere (GameObject)
– GUI Text (GameObject) – name it ‘Old Score Text’
– GUI Text (GameObject) – name it ‘Player Score Text’
– Main Camera, attach the ‘TapCounter.js’:

TapCounter.js


#pragma strict
 
    // Only ANDROID NOT PC NOT IOS
    // Attach this script to the Main Camera
     
// Hierarchy DRAG E DROP over var GUI Text in Inspector
var scoreText : GUIText;
var oldScoreText : GUIText;
// touch counter, private because the users is not be able to change this value
private  var score : int;
private  var oldScore : int;
 
// Ray Cast Setup
var speed : float = 4;
var hit = new RaycastHit();
 
function Start () {
        // The counter initial value is 0
        score = 0;
        scoreText.text = "No Touch:";
        // load old saved score data
        oldScore = PlayerPrefs.GetInt("Player Score");
}
 
function Update () {

	// refresh old score data
    oldScoreText.text = "Old Score : "  + oldScore;
 
    // se c'è un tocco Input.touchCount AND la fase del tocco è quella iniziale TouchPhase.Began
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
    // traccia i raggi dalla Camera dal punto del tocco
    var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
 
        // se raycast colpisce l'oggetto
        if (Physics.Raycast (ray, hit)) {
        // fai partire la funzione che incrementa il contatore
        UpdateScore ();
        }
 
    }
}
 
function UpdateScore () {
    // score var increment of +1
    score += 1;
    // scoreText in assigned inside Inspector on GUI Text
    // change .text property and write it on the display 
    scoreText.text = "Touched: "  + score;
}

// Save Data On Click START #####################################
function OnGUI()
{
  if (GUI.Button(Rect(10,10,200,50),"Save Data"))
  { 
	// setta un valore intero - SetInt - di nome 'Player Score' di valore 'score'
	PlayerPrefs.SetInt("Player Score", score); 
	// ottiene il vecchio valore salvato in precedenza - deve avere lo stesso nome del salvataggio precedente
	// se non esiste restituisce il valore 0 oppure ""
	// questa funzione mi serve per fare confronti tra il punteggio vecchio e quello nuovo
	oldScore = PlayerPrefs.GetInt("Player Score");
	// salva il valore ottenuto dalla partita corrente 'Player Score'
	PlayerPrefs.Save();	
  }
}
// Save Data On Click END #####################################

Hierarchy> Main Camera> Inspector> TapCounter.js DRAG AND DROP:
‘Player Score Text’ (GUI Text) over var ‘score Text’
‘Old Score Text’ (GUI Text) over var ‘Old Score Text’

For italian people:

Notare


if (GUI.Button(Rect(10,10,200,50),"Save Data"))
  { 
	// setta un valore intero - SetInt - di nome 'Player Score' di valore 'score'
	PlayerPrefs.SetInt("Player Score", score); 
	// ottiene il vecchio valore salvato in precedenza - deve avere lo stesso nome del salvataggio precedente
	// se non esiste restituisce il valore 0 oppure ""
	// questa funzione mi serve per fare confronti tra il punteggio vecchio e quello nuovo
	oldScore = PlayerPrefs.GetInt("Player Score");
	// salva il valore ottenuto dalla partita corrente 'Player Score'
	PlayerPrefs.Save();	
  }

Come funziona?

0. IMPORTANTE: per abilitare l’app alla scrittura su SD seguire la seguente procedura:
Unity> MAIN TOP MENU> File> Build Settings> Player Settings…> Other Settings> Configuration> Write Access> External (SD Card)

questo creerà all’interno del dispositivo Android la cartella (vista da Win 7)
-> Phone/Android/data/com.Company.LuceDigitale

Equivale di fatto in un progetto Android a scrivere su AndroidManifest.xml
->

1. PlayerPrefs.SetInt(“Player Score”, score);
-> Imposta in RAM (non viene ancora scritto nulla), un valore intero ‘Player Score’, del valore ‘score’ che è appunto un numero intero

2. oldScore = PlayerPrefs.GetInt(“Player Score”);
-> Ottiene il vecchio valore salvato di ‘Player Score’ se presente, se non esiste restituisce 0 oppure “”
Questo passaggio è utile se è necessario confrontare i vecchi valori con i nuovi, per stabilire ad esempio una classifica o se ho superato un check point.

3. PlayerPrefs.Save();
-> Salva su SD i valori impostati dal comando del punto 1.

Per testare avviare l’app sul dispositivo Android, tocco ripetuto sull’oggetto in scena, tocco sul bottone per salvare il punteggio attuale. Spegne il dispositivo, riavviare e ricaricare l’app, il valore di ‘Old Score Text’ dovrebbe essere quello salvato in precedenza.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Save Data – Local

Unity 3D – Android – Native Microphone – JavaScript

Unity 3D – Android – Native Microphone – JavaScript

How to access Android Microphone.

Record and Play

Hierarchy> Main camera> Inspector> Add Component

– ‘Audio Listener’

– ‘Audio Source’ setup:
– ‘Mute ‘ -> uncheck
– ‘Play On Awake’ -> uncheck
– ‘Loop’ -> uncheck

– ‘GetAudio.js’


#pragma strict

function Start() {
}

function OnGUI()
{
  if (GUI.Button(Rect(10,10,60,50),"Record"))
  { 
    //static function Start(deviceName: string, loop: bool, lengthSec: int, frequency: int)
    audio.clip= Microphone.Start ( null, false, 3, 44100 ); 
  }
  if (GUI.Button(Rect(10,70,60,50),"Play"))
  { 
    // play audio clip you have just recorded
    audio.Play();
  }
}

How does it work?

1. if you will touch GUI.Button(Rect(10,10,60,50),”Record”
2. it records an audio clip of 3 seconds, ONLY one time, no loop (static function loop: bool -> false)

3. if you will touch GUI.Button(Rect(10,70,60,50),”Play”
4. it plays the audio clip you have just recorded, ONLY one time, no loop, because into Inspector>’Audio Source’ component is set to ‘Loop’ -> uncheck

Record – > Effects -> Play

Super Easy! You need only attach audio filters components to ‘Audio Source’.

Hierarchy> Main Camera> Inspector> ‘Add Component’> Audio> Audio Echo Filter, setup the filter

Inside Hierarchy you will see:

Main Camera>

– ‘Audio Listener’

– ‘Audio Source’ setup:
– ‘Mute ‘ -> uncheck
– ‘Play On Awake’ -> uncheck
– ‘Loop’ -> uncheck

– ‘Audio Echo Filter’

– ‘GetAudio.js’


#pragma strict

function Start() {
}

function OnGUI()
{
  if (GUI.Button(Rect(10,10,60,50),"Record"))
  { 
    //static function Start(deviceName: string, loop: bool, lengthSec: int, frequency: int)
    audio.clip= Microphone.Start ( null, false, 3, 44100 ); 
  }
  if (GUI.Button(Rect(10,70,60,50),"Play"))
  { 
    // play audio clip you have just recorded
    audio.Play();
  }
}

Record -> Chipmunks Voice -> Play

Hierarchy> Main camera> Inspector> Add Component

– ‘Audio Listener’

– ‘Audio Source’ setup:
– ‘Mute ‘ -> uncheck
– ‘Play On Awake’ -> uncheck
– ‘Loop’ -> uncheck
– ‘Pitch’ -> 1.58

– ‘GetAudio.js’

Record -> Satan Voice -> Play

Hierarchy> Main camera> Inspector> Add Component

– ‘Audio Listener’

– ‘Audio Source’ setup:
– ‘Mute ‘ -> uncheck
– ‘Play On Awake’ -> uncheck
– ‘Loop’ -> uncheck
– ‘Pitch’ -> 0.69

– ‘Audio Echo Filter’

– ‘GetAudio.js’

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Android – Native Microphone – JavaScript