Warning: Cannot modify header information - headers already sent by (output started at /var/www/lalieno.it/index.php:48) in /var/www/lalieno.it/inc/cookie.php on line 3
a cadenza discontinua
Come se fossi
BLOG

Unity 2D [E4] - Transition bounce effect of sprite or text

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;
	}

}



di GuiZ
04/01/2017

Commenta

We'll never share your email with anyone else.