gamification

Unity3D – 2D Games – Pixel Perfect 2D Game

Unity3D – 2D Games – Pixel Perfect 2D Game

Un rendering ‘pixel perfect’ lo abbiamo quando la risoluzione in uscita di un gioco e la dimensione di uno sprite corrispondono in pixel.
Immaginiamo di avere un progetto in WebPlayer di 800×600 pixel, di importare uno sprite di 400×600, se lo sprite ricopre metà del display allora è un rendering ‘pixel perfect’

Unity3D isn’t designed for pixel perfect rendering, this cause artifacts, and pixel blurring.

Perfect user interface buttons

To remove artifacts you have to do:

1. Import Texture 512×512
Migliora l’interpolazione di Unity3D

2. Texture> Inspector>
– Filter Mode: Bilinear
Pulisce l’interno della texture

Format TrueColor
Elimina la compressione del colore per far scomparire l’effetto squadrettato tipo JPG

3. Material> Shader Transparent/Cutout/Soft Edge Unlit
Rende più morbido il bordo della maschera alfa

Perfect spritesheet

To remove artifacts you have to do:

– You will likely need a texture with “power of 2” dimensions, e.g. 256 – 512 – 1024px.
– Texture filter mode should be “point”
– Turn off texture compression
– If you are using Unity ‘Sprite’ texture type, take care with your sprite pixels to units setting
– Take care with your orthographic camera scale relative to your screen dimensions

If you want the orthographic camera to match your screen resolution, then set the camera Size to be half of your screen height. So, in this case, if your window is 600 pixels, set your camera Size value to be 300.

For italian people:

– Importare texture multiple di 2px ‘128 – 256 – 512 – 1024px’
-> per evitare l’interpolazione applicata da Unity automaticamente per portarle a dimensioni multiple di 2

– Project> my-spritesheet> Inspector> Filter Mode> Point
-> RENDE I PIXEL PIU’ DEFINITI perchè toglie i filtri bilineari e trilineari

– Project> my-spritesheet> Inspector> Default> Format> Truecolor
-> elimina la compressione dei colori della texture

– Project> my-spritesheet> Inspector> Pixels to Units
-> definisce la corrispondenza tra il numero di pixel e le unità Unity3D ad esempio: 100px per 1 unità Unity3D
Ottimizzare questo parametro per gestire la dimensione degli sprite a video.

– Main Camera> Projection> Orthographic
-> per utilizzare una camera senza deformazioni prospettiche.

– Main Camera> Size
-> definisce quante unità Unity3D in altezza sarà la vista della camera, ad esempio: 15 significa che la camera in altezza inquadrerà lo spazio di 15 unità.
Ottimizzare questo parametro per gestire la dimensione degli sprite a video.

– MAIN TOP MENU> Edit> Project Settings> Quality> Texture Quality> Full Res

Se si vuole che la camera ortografica corrisponda alla risoluzione a video, settare il camera ‘Size’ alla metà dell’altezza della risoluzione a video. Ad esempio se l’altezza del video è 600px il camera ‘Size’ sarà 300.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – 2D Games – Pixel Perfect 2D Game

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Destroy Every ‘hit’ GameObject!

Create a scene with:

– Main Camera
– Cube
– Empty Object, assign Destroy.js:


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something         
       
            Destroy(hit.transform.gameObject); // destroy every hit object
        }
    }
 
}// End Update

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Destroy GameObject

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Create a scene with:

– Cube, attach the script ‘BoomScript.js’


#pragma strict

function Start () {
}

function Update () {
}

function Boom(){
Debug.Log("Kaboooom!");
}

– Main Camera, attach the script ‘Raycast.js’


#pragma strict


function Update(){
    // if you press Mouse Button
    if (Input.GetMouseButtonDown(0)){ // if you press Left Mouse Button -> GetMouseButtonDown(0) 
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something          
          
            // Get Script attached to the GameObject 
            var boomScript : BoomScript = hit.collider.GetComponent(BoomScript);

    		if (boomScript != null) {// if it exists
        	boomScript.Boom(); // execute the function Boom() of BoomScript
   	    	}
        }
    }
 
}// End Update()

Play, click over the Cube, in the console the message will be ‘Kaboooom!’.

For italian people: come funziona?

1. Se viene cliccato il tasto sinistro del mouse

2. parte un raggio dalla Main Camera

3. se colpisce qualcosa ottieni il Component ‘BoomScript’, cioè lo script BoomScript.js

4. se non è nullo, cioè esiste, avvia all’interno di BoomScript.js la funzione Boom()

5. function Boom() manda il messaggio ‘Kaboooom!’ alla console di Unity3D.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – GetMouseButton – PhysicRaycast – Get GameObject script Component

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Hit GameObject

Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Hit GameObject

Physic.Raycast is useful if you need to know the exact point where the mouse pointer is.

Create a scene with:

– Main Camera
– Cube
– Empty Object, assign GetMouseClick.js:


#pragma strict

function Update(){
    // if you press Left Mouse Button -> GetMouseButtonDown(0) 
    if (Input.GetMouseButtonDown(0)){
        var hit: RaycastHit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // it sends a ray from Camera.main
        if (Physics.Raycast(ray, hit)){ // if it hits something        

            var object = hit.transform.gameObject; // if the ray hits a GameObject
 
            Debug.Log("Clicked!");
            // Do something...
        }
    }
 
}

Play and click over the Cube

NOTICE: You can assign the script at every object in the scene

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Get Mouse Button – Physic.Raycast – Hit GameObject

Unity3D – Character Controller – isGrounded – JavaScript

Unity3D – Character Controller – isGrounded – JavaScript

Was the CharacterController touching the ground during the last move?


function Update () {
		var controller : CharacterController = GetComponent(CharacterController);
		if (controller.isGrounded)
			print("We are grounded");
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Character Controller – isGrounded – JavaScript