Header
Unity 2D [ E2 ] - Apply the effect vibration / earthquake to a sprite
in: Utility  So/Code: Generico Data: 06/04/2016
Ora: 11.34:22
Articolo visualizzato: 1514 volte

The effect of shake , vibration or earthquake is easily implemented using some methods such as Random.insideUnitCircle that generates randomly a point on a circle of radius 1 .

It is therefore easily applicable to a sprite especially in 2D environment . Whereas the generation of a point randomico in a circle , having the center coordinates 0x , 0y we can easily apply a force to multiply the "power" then the radius of action.

Suppose you've got a point random " Random Point 1 " Coordinate ( example) -0.5x and -0.6y to which we apply a constant , called the shake strength but in essence it is an increase in the radius for example, force 2 .

So we will have a new location (environment 2d ) with coordinates :

Vector3.x = random.x * force
Vector3.y = random.y * 
force
Vector3.z = z_original

Having said that, we can create a script that cycle on and off the shake , vibration or earthquake , if you prefer , where the force is parameterized as input parameter . This script is applicable to the sprite on which you want to act .

 

using UnityEngine;
using System.Collections;

public class ShakeEffect : MonoBehaviour {

	// vars
	private bool shakeOn = false;
	private float shakePower = 0;

	// sprite original position
	private Vector3 originPosition;

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {

		// if shake is enabled
		if(shakeOn) {
			
			// reset original position
			transform.position = originPosition;

			// generate random position in a 1 unit circle and add power
			Vector2 ShakePos = Random.insideUnitCircle * shakePower;

			// transform to new position adding the new coordinates
			transform.position = new Vector3 (transform.position.x + ShakePos.x, transform.position.y + ShakePos.y, transform.position.z); 
		}
	}

	// shake on
	public void ShakeCameraOn(float sPower){

		//save position before start shake, 
		//this it's really important otherwise 
		//the sprite can goes away and will not return 
		//in native position
		originPosition = transform.position;

		//enable shaking and setting power
		shakeOn = true;
		shakePower = sPower;
	}

	// shake off
	public void ShakeCameraOff(){

		// shake off
		shakeOn = false;

		// set original position after 
		transform.position = originPosition;
	}

}

Commenti Commenti (0) | User Autore: Guido Camerlingo (Guiz)
Tags: unity 2D videogame effect shake vibrazione effetto terremoto earthquake sprite move position random





Articoli Correlati
Unity 2D [ E2 ] - Apply the effect vibration / earthquake to a sprite
Unity 2D [E4] - Transition bounce effect of sprite or text
Unity 2D [E1] - Detecting Collisions
Unity 2D [E1] - Gestire le collisioni
Unity 2D [E2] - Applicare ad una sprite l'effetto vibrazione / terremoto

Commenti

Scrivi Commento
Codice Verifica

Commento massimo 5000 caratteri.(Tutti i campi contrassegnati da * sono obbligatori).

   

Ricerca

Glossario Naviga nel nostro glossario!
Scopri il gergo dei Geek!


 ULTIMI ARTICOLI


 Giochi in uscita oggi

Nessuna uscita prevista


 [EN] Last Articles

 Siti Amici

Visita il Blog Roll
Contattaci! Diventa nostro amico!



 Amazon


 Hot Downloads

3669

 Categorie

81
163
57
87
13
2
20
36
1
2
5
5
128
7
24
14
58
45

 Code

11
11
3
5
23
1
7

 Games

19
45
13

 GUIDE


 Novità Downloads

61
39
3669

 Tags Cloud

a ios estate download touch 1998 2012 novità rumors google Scansione ambiente asteroide mac google iscriversi hack airprint come



 CopyRight

Valid XHTML 1.0 Transitional CSS Valido! [Valid RSS] Creative Commons License


Geek-Blog by Flavio Mandato, Giuseppe Vaccaro, Guido Camerlingo, Stefano Natale, Domenico Cavallo is licensed under a Creative Commons Attribuzione-Non opere derivate 2.5 Italia License.
Based on a work at www.geek-blog.it.
Permissions beyond the scope of this license may be available at http://www.geek-blog.it/

Disclaimer - Responsabilità - Pagina generata in 0.097 secondi. Geek-Blog.it