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.