Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DomNeuge · Jan 12, 2019 at 11:43 AM · performancetilemapperformance optimizationlaggylags

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: alt text

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.

scenelagquestion.png (203.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DomNeuge · Jan 14, 2019 at 09:50 AM 0
Share

I deleted this line of code now, but this did not improve the performance.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DomNeuge · Jan 18, 2019 at 08:33 PM 0
Share

@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.

avatar image xxmariofer DomNeuge · Jan 18, 2019 at 08:39 PM 0
Share

$$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)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Big Jerk or Lag when starting the game in Iphone caused by Shader.CreateGPUProgram, BatchRenderer.Flush and MeshSkinning.Render 0 Answers

Just One of my scene is so laggy 0 Answers

Random lag spikes any time processor intensive tasks a scene 0 Answers

Question regarding TileMap Performance 0 Answers

Unity Tile Array vs Int Array Performance 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges