W1zzel
1
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.

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.