- Home /
2D Game has very bad lag because of one script?
I am making somewhat of a flappy bird clone (mostly for practice) and everything works fine, but it is just really slow with a low FPS of 13 and a max FPS of 28. Why would this be?
So I ran a check with the unity profiler and it turns out that my MoveLeft script which I am using to reuse my obstacles is taking up 73% of the CPU. I have it attached to each of my 6 prefabs, which include 2 long obstacles and a token (to collect) in middle each. The game works by moving the prefabs along the camera, then moving them back after they pass the camera.
Here is the script:
using UnityEngine;
using System.Collections;
public class MoveLeft : MonoBehaviour
{
[SerializeField]
private float _speed = 5f;
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.left * Time.deltaTime * _speed);
if (transform.position.x < -15)
{
float randomYPosition = UnityEngine.Random.Range(-3, 3);
transform.position = new Vector3(15, randomYPosition, 0);
}
}
}
Any help is appreciated! Thank you!!
Your answer
Follow this Question
Related Questions
Send Mouse Event 0 Answers
How to Use Key Combinations with the Control-Key? 2 Answers
How can I Update c# version? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers