Header
Unity 2D [E4] - Transition bounce effect of sprite or text
in: Gaming  So/Code: Generico Data: 04/01/2017
Ora: 11.02:58
Articolo visualizzato: 1068 volte

In this little script i explains how to create a small effect bounce parameterized applicable on any game object, which then can contain any resource. It is based on a 2 phase transition and the other of the bounce. Obviously it is widely editable. Parameterization is important in order to find the desired result.

I leave to the commented code.

 

 


// Use this for initialization
protected override void Start ()
{
	startPosition = gameObject.transform.position;	

	// parameters:
	// BounceDecrementalPercentage percentage of displacement according to the distance traveled by the transition to the destination
	// BounceDecrementalConstantPercentage percentage of decremental bounceDecrementalPercentage every bounce
	// TotalBounceTime total effect transition time + bounce
	// TransitionTimePercentage percentage of totalBounceTime dedicated to the transaction
	// BounceTimePercentage percentage of totalBounceTime dedicated to bounce
	// DestinationPosition target position
	// StartPosition initial position
	StartCoroutine (BounceEffect(20f,2f,1.5f,30f,70f,destinationObject.transform.position,startPosition));
}

}

private IEnumerator MoveToPosition (Vector3 destinationPosition, Vector3 startPosition, float duration)
{
	float elapsedTime = 0f;
	while (elapsedTime < duration) {
		float fracComplete = elapsedTime / duration;

		transform.position = Vector3.Lerp (startPosition, destinationPosition, fracComplete);
		elapsedTime += Time.deltaTime;

		yield return null;
	}

	transform.position = Vector3.Lerp (startPosition, destinationPosition, 1);
}

private IEnumerator BounceEffect (float bounceDecrementalPercentage, float bounceDecrementalConstantPercentage, float totalBounceTime, float transitionTimePercentage, float bounceTimePercentage, Vector3 destinationPosition, Vector3 startPosition)
{
	// calculating the input time and the bounce time
	float singleTimeUnitForPercentage = totalBounceTime / 100f; 

	// transaction time by multiplying the unit for 40% give 40% of the weight of the total of the transaction
	float transitionTime = singleTimeUnitForPercentage * transitionTimePercentage;

	// to destination
	yield return StartCoroutine (MoveToPosition(destinationPosition, startPosition, transitionTime));

	// bounce start
	float destinationDistance = Vector2.Distance (startPosition, destinationObject.transform.position);


	// calculating the time units of the bounce and the value decremental
	float timetodel = 0;
	float timeminuscomplete = (singleTimeUnitForPercentage * bounceTimePercentage) / (bounceDecrementalPercentage / bounceDecrementalConstantPercentage);

	// set bool
	bool isMinus = true;
	bool isFinish = false;

	// set for first bounce
	timetodel = timeminuscomplete;

	// bounce while
	while (!isFinish) {

		if (isMinus) {
			destinationPosition.x = destinationObject.transform.position.x - ((destinationDistance / 100f) * bounceDecrementalPercentage);
			yield return StartCoroutine (MoveToPosition(destinationPosition,gameObject.transform.position,timeminuscomplete));
			bounceDecrementalPercentage = bounceDecrementalPercentage - bounceDecrementalConstantPercentage;
			timetodel = timetodel + timeminuscomplete;
			isMinus = false;
		} else {
			destinationPosition.x = destinationObject.transform.position.x + ((destinationDistance / 100f) * bounceDecrementalPercentage);
			yield return StartCoroutine (MoveToPosition(destinationPosition,gameObject.transform.position,timeminuscomplete));
			bounceDecrementalPercentage = bounceDecrementalPercentage - bounceDecrementalConstantPercentage;
			timetodel = timetodel + timeminuscomplete;
			isMinus = true;
		}

		if (bounceDecrementalPercentage <= 0f)
			isFinish = true;

		yield return null;
	}

}


Commenti Commenti (0) | User Autore: Guido Camerlingo (Guiz)
Tags:  unity 2D videogame bounce effetto transizione transition effect buonce rimbalzo





Articoli Correlati
Unity 2D [E4] - Transition bounce effect of sprite or text
Unity 2D [E4] - Transizione effetto bounce su sprite o testo
Unity 2D [ E2 ] - Apply the effect vibration / earthquake to a sprite
Unity 2D [E1] - Detecting Collisions
Unity 2D [E1] - Gestire le collisioni

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


 [EN] Last Articles

 Siti Amici

Visita il Blog Roll
Contattaci! Diventa nostro amico!



 Amazon


 Hot Downloads

3742

 Categorie

85
163
57
87
13
2
20
36
1
2
5
5
128
7
24
15
59
45

 Code

11
11
3
5
2
23
1
7

 Games

19
45
13

 GUIDE


 Novità Downloads

62
54
3742

 Tags Cloud

server boot Chung-Pei nero microsoft bootvideo gratuito gratuito smtp essential microsoft antivirus boot nasa massiccio security array gratuito nasa



 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.351 secondi. Geek-Blog.it