- Home /
Animating GameObject into UI Element
Hi, i am working on a Mobilegame similar to Doodle Jump. Each time, the player jumps on a bird i am spawning a Heart-Prefab. On the Heart-Prefab i have this Script attached :
public class HeartMovement : MonoBehaviour
{
private Vector2 spawnPosition;
private Vector2 heartDestination;
private Vector2 heart1Offset;
private Vector2 heart2Offset;
private Vector2 heart3Offset;
private Vector2 cameraPosition;
public float speed = 1f;
private float journeyLength;
private float startTime;
// Start is called before the first frame update
void Start()
{
startTime = Time.time;
spawnPosition = new Vector2(transform.position.x, transform.position.y);
//Offsets are where the UIelements will appear, relative to the Camera.main.transform.position
heart1Offset = new Vector2(2.444f, 4.418f);
heart2Offset = new Vector2(1.652f, 4.418f);
heart3Offset = new Vector2(0.872f, 4.418f);
}
// Update is called once per frame
void Update()
{
cameraPosition = new Vector2(Camera.main.transform.position.x, Camera.main.transform.position.y);
heartDestination = new Vector2(cameraPosition.x + heart1Offset.x, cameraPosition.y + heart1Offset.y);
CalculateLength();
float distCovered = (Time.time - startTime) * speed;
float fractionOfJourney = distCovered / journeyLength;
//change destination based on player lives
transform.position = Vector2.Lerp(spawnPosition, heartDestination, fractionOfJourney);
}
void CalculateLength()
{
journeyLength = Vector2.Distance(spawnPosition, heartDestination);
}
}
When the Heart reachs its Destination, i want to Destroy it and instead activate a UI Element to show the amount of Hearts. Now my Question : Is this a good way to do this ? I am worrying about performance because i constantly have to calculate multiple Vectors.
Thanks in advance.
Answer by HappiiGamer · Oct 14, 2019 at 02:21 PM
I don't think there is a problem in the vector computation.
What is eating away at performance in your case is constantly Instantiating and Destroy the heart object. It's better to have object set to inactive when you're not using it, and set to active when you want to show it. Even better, have it in a pool of GameObjects, along with enemies and other stuff that move. So that you reduce the amount of objects you destroy and create during gameplay.
Okay, i thought it would be better in regards of memoryusage to destroy an object which i don't need and spawn a new one when i need to. Guess i was wrong. Thanks for your answer.
Yea, I used to think so too, but I noticed significant lag when I would pop missiles and explosions into existence. It's better to have a smart way of keeping track of reoccurring GameObjects. You should read up on Object Pooling, it's simple to do, but a hassle to implement at later stages of development. It is a must though. Good luck with your game!
I just watched Brackeys Video on Object Pooling. It was pretty confusing and im afraid i can't implement it without deleting big portions of my Game. However i will try use it as much as possible and remember it for my future projects. Thanks
Your answer
Follow this Question
Related Questions
UI object move? 2 Answers
Animation not rendering on canvas set to overlay 1 Answer
Unity 5 canvas animation laggs, why? 0 Answers