- Home /
My game is laggy, but there are only a ball and some flat platforms.
Hi, I'm doing a simple smartphone game, where a ball moves horizontally with the movement of your finger (like RollingSky for example). I simply have a ball with this script:
private float playerSpeed = 500;
private float directionSpeed = 10;
void Update()
{
GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 16f));
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
transform.position = Vector3.Lerp(gameObject.transform.position, new
Vector3(Mathf.Clamp(touch.x, -15f, 15f), gameObject.transform.position.y,
gameObject.transform.position.z), directionSpeed * Time.deltaTime);
}
}
and the main camera with this script:
float offset;
public GameObject player;
void Start()
{
offset = player.transform.position.z - transform.position.z;
}
void Update()
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new
Vector3(gameObject.transform.position.x, gameObject.transform.position.y,
player.gameObject.transform.position.z - offset), Time.deltaTime * 100);
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new
Vector3(player.gameObject.transform.position.x, gameObject.transform.position.y,
gameObject.transform.position.z), Time.deltaTime * 5);
(I put the horizontal and vertical movement in two different lines because in the same the ball went too fast horizontally)
I don't know why the game is a little bit laggy, but there are only one ball and some flat platforms. I think it's the camera that goes wrong for some reason. What can I do? thank you!
you should open up the profiler, a great chance to learn how to make your projects more efficient!
Answer by kot2202 · Jun 29, 2020 at 05:58 PM
You are using GetComponent() in Update, try to assign it in Start() if it's possible. Also using lot of new Vector3. Generally it's good practice to create vector once and just edit it's xyz later. I think changing transform.position instead of velocity would be better on performance because you wouldn't have to calculate the physics of rigidbody. However opening the profiler should help more, these things affect performance a bit but I don't think they are the main problem
Answer by ttesla · Jun 29, 2020 at 07:47 PM
Your update logic seems quite strange. Using Lerps for the same variable twice and they are chained. I recommend comment out those and write a very basic follow code to test if thats the problem.
Also use LateUpdate() with camera stuff instead of Update(). And on the rigidybody of the ball, from the Interpolate settings. Set it to Interpolate or Extrapolate. This prevents jittering, if that is the thing happening.
Answer by BayonetCharge · Jun 30, 2020 at 12:12 AM
It might not have anything to do with performance (even if it can be improved).
Try doing movement in FixedUpdate().
https://connect.unity.com/p/why-we-add-forces-in-fixedupdate-not-in-update-eng
Your answer
Follow this Question
Related Questions
change objects Axis to match cameras rotation 0 Answers
Follow camera in 2d game 2 Answers
URGENT Cinemachine virtual camera while falling 1 Answer
Help make camera zoom smoother 3 Answers
Regarding transform.position in the roll a ball tutorial 1 Answer