- Home /
Performance issue Unity 2D
Hello, I am new to Unity, so maybe my question is a really basic one. I tried to make a simple Scene with a TileMap and it works just fine on a very good Computer. But when I want to run the "game" with my Laptop, which has 16Gb of RAM, an Intel i7 5th generation and a small graphics chip from Nvidia, the game starts to lag a bit. Sometimes the game runs smooth and a few seconds later I have 30fps or more. It is only a very small game with very low graphics so I can not understand why I get those performance issues. I tried to set the Application.targetFrameRate to 300 and disable V-Sync but this made it only worse.
The game Scene:
My Code (only 2 Scripts):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowing : MonoBehaviour
{
private Transform playerTransform;
private float smoothSpeed = .13f;
private Vector3 offsetToPlayer = new Vector3(0, 0, -9);
private void Start()
{
Application.targetFrameRate = 300;
playerTransform = GameObject.Find("Player").GetComponent<Transform>();
}
// Update is called once per frame
void LateUpdate() //LateUpdate wird nach Update aufgerufen. Macht aber genau das selbe.
{
Vector3 desiredPosition = playerTransform.position + this.offsetToPlayer;
Vector3 smoothPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); //Lerp wird verwendet um "smooth" von einem Punkt A nach einem Punkt B zu kommen
this.transform.position = smoothPosition;
}
}
The PlayerController:
using System;
using UnityEngine;
public class CharacterController2DTopDown : MonoBehaviour
{
//Inspector Variables
private float playerSpeed = 8; //speed player moves
public Boolean isTurnedRight;
private SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer = this.GetComponent<SpriteRenderer>();
spriteRenderer.flipX = false;
isTurnedRight = true;
}
void Update()
{
Move();
if (1 / Time.deltaTime <= 60)
{
Debug.Log(1 / Time.deltaTime);
}
}
void Move()
{
//Um ein starkes lineares Bewegungsgefühl zu schaffen verwende ich
//Transform.translate und nicht RigidBody.AddForce.
//UP
if (Input.GetKey(KeyCode.W)) //Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0, playerSpeed * Time.deltaTime, 0);
}
//DOWN
if (Input.GetKey(KeyCode.S))
{
transform.Translate(0, -playerSpeed * Time.deltaTime, 0);
}
//LEFT
if (Input.GetKey(KeyCode.A))
{
transform.Translate(-playerSpeed * Time.deltaTime, 0, 0);
//Flip Player Graphic
if(isTurnedRight)
{
spriteRenderer.flipX = true;
this.isTurnedRight = false;
}
}
//RIGHT
if (Input.GetKey(KeyCode.D))
{
transform.Translate(playerSpeed * Time.deltaTime, 0, 0);
//Flip Player Graphic
if (!isTurnedRight)
{
spriteRenderer.flipX = false;
this.isTurnedRight = true;
}
}
}
}
I hope someone can help me.
Answer by samth · Jan 12, 2019 at 02:14 PM
Debug.Log() is pretty bad for performance, especially when called every frame. Try removing that line in Update and see if the performance improves at all.
I deleted this line of code now, but this did not improve the performance.
Answer by xxmariofer · Jan 15, 2019 at 09:31 AM
it is imposible to be a code problem since those 2 script should run really fast, please use the profiler window for checking whats causing the spikes, also why are you using lateupdate in the camera rather than update? The remove the logs and save the transform in a variable at the start and use that rather than transform.
void Start() { Transform myTransform = transform; }
screenshot in the profiler what is making the lag spikes please.
@xxmariofer Sorry for the late answer. I tried a few things now and I came to the conclustion, that it works really well, when I use the rigidbody.transform. I have no Idea why, but it makes the performance a little bit better. I also tried to compile the game, so I can see if my PC is just too weak for Unity and Unity realtime rendering at the same time and it did work out really fine. I think it was not a Unity problem, but rather a hardware problem. However using the rigidbody.transform did increase the performance a bit, so i will use it in the future.
$$anonymous$$aybe an extrange bug or some background task, but even if those 2 scripts were terribly coded, with a lot of performace issues you would not even see a lag spike, it is true unity editor consums some extra resources but nothing too terrible, your pc is better than $$anonymous$$e not sure what was happening but never code related issue (although it could be optimized)