- Home /
Animate GUIText to a target GUIText?
Many games shows animated points (score) added to main score i.e by going from one position (usually a clicked position) to score position which is placed somewhere on the screen.
In my game there are random asteroids. These asteroids are destroyed on fire and on destroyed I need to show points are added to main score by animating GUIText from the destroyed asteroid to main score placed at top right corner of the screen.
I have tried following C# script attached to GUIText prefab which is animated (moved) from destroyed asteroid to main score GUIText,
void Start() {
scoreTF = GameObject.FindGameObjectWithTag ("ScoreTF").GetComponent<GUIText>();
}
void FixedUpdate () {
tarPos = new Vector2 (scoreTF.transform.position.x, scoreTF.transform.position.y);
transform.Translate (Camera.main.WorldToViewportPoint(tarPos) * Time.deltaTime * 0.3f);
}
The GUIText prefab to which above script is attached is Instantiated in other script like so,
Instantiate ( scoreGUITextToAnimate, new Vector2(0,0), transform.rotation);
All above scripts works partially fine. New GUIText is appeared correctly on a destroyed asteroid but do not animate towards Score placed at top right corner. They all go right side (diagonally) of the asteroid.
Can you guide me in a right direction.
Answer by AkooleGame · Nov 13, 2014 at 05:30 AM
I got it working using following code with the help of Vector3.Lerp and updating transform.position like so,
void FixedUpdate () {
Vector3 currentPosition = transform.position;
transform.position = Vector3.Lerp( currentPosition, scoreTF.transform.position, 2 * Time.deltaTime );
//Destroy GUIText after animation is complete
}
Your answer