• 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 Strektail · Feb 19, 2021 at 11:13 AM · scripting problem2d gameproblem during runtimelaggymario

Super Mario Bros 2D very lagging

Hello! I'm creating Super Mario Bros 2D. When I run it with 1 enemy mushroom it runs smoothly. But if enemies are more than 2 it becomes very laggy. Here is mushroom enemy script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Mushroom : MonoBehaviour

{

 public float speed;
 public bool moveLeft = true;
 public GameObject camera;
 bool colliding;
 float collidingTime;

 void Start() => Physics2D.IgnoreCollision(camera.GetComponent<BoxCollider2D>(), GetComponent<Collider2D>());

 void FixedUpdate()
 {
     if (moveLeft && GetComponent<En>().isHit == false)
         transform.Translate(Vector2.left * Time.deltaTime * speed, Space.World);
     else if (!moveLeft && GetComponent<En>**This is Enemy script**().isHit == false)
         transform.Translate(Vector2.right * Time.deltaTime * speed, Space.World);

     if (colliding)
     {
         collidingTime = 0;
     }
     else
     {
         collidingTime += Time.deltaTime;
         if (collidingTime >= 2)
             Destroy(gameObject);
     }
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     colliding = true;

     if (!collision.gameObject.CompareTag("Player") && !collision.gameObject.CompareTag("chunk") && !collision.gameObject.CompareTag("gr"))
     {
         if (moveLeft)
             moveLeft = false;
         else
             moveLeft = true;
     }

     if (collision.gameObject.CompareTag("MainCamera"))
         Destroy(gameObject);
 }

 private void OnCollisionStay2D(Collision2D collision) => colliding = true;

 private void OnCollisionExit2D(Collision2D collision) => colliding = false;

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         collision.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 3, ForceMode2D.Impulse);
         GetComponent<PolygonCollider2D>().enabled = false;
         GetComponent<En>().startDeath();
     }
 }

}

And enemy script for all enemies:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class En : MonoBehaviour

{

 public bool isHit = false;
 public Sprite juSp;

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Player" && !isHit)
         collision.gameObject.GetComponent<Mario>().Lo();
 }

 public IEnumerator Death()
 {
     isHit = true;
     GetComponent<Animator>().enabled = false;
     GetComponent<SpriteRenderer>().sprite = juSp;
     if (GetComponent<Rigidbody2D>().bodyType == RigidbodyType2D.Dynamic)
         GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
     if (gameObject.CompareTag("mu"))
         GetComponent<Collider2D>().enabled = false;
     yield return new WaitForSeconds(2f);
     Destroy(gameObject);
 }

 public void startDeath() => StartCoroutine(Death());

} and I have a script for when player is nearby enemies will be active:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class FPSOptimizer : MonoBehaviour

{

 public GameObject player;

 public GameObject[] enemy;

 void FixedUpdate()

 {

     for (int i = 0; i < enemy.Length; i++)

     {

         if (enemy[i].transform.position.x - player.transform.position.x <= 3)

             enemy[i].SetActive(true);

     }

 }


} Does anyone have idea for optimizing script? There is a lot of things to do. But I stuck there.

Comment
Add comment · Show 1
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 logicandchaos · Feb 21, 2021 at 08:50 PM 0
Share

yo are using getComponent in fixed update, and on collision stay, those 2 things are probably slowing it down the most.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Megaboy238 · Feb 21, 2021 at 10:36 PM

Declare all the GetComponents in the Start Method as variables then it only has to do it once not every frame, that should make a massive difference.

Comment
Add comment · Show 4 · 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 Strektail · Mar 05, 2021 at 10:19 AM 0
Share

I will try.

avatar image AbandonedCrypt · Mar 05, 2021 at 10:27 AM 0
Share

Of course this is true, however GetComponent does Dictionary lookups which are upon the fastest data access operations possible (O(1) instead of O(n)) and should not result in this noticeable of a difference.

avatar image Strektail · Mar 05, 2021 at 10:34 AM 0
Share

Which variable need for script?

avatar image Strektail · Mar 05, 2021 at 11:56 AM 0
Share

I did so, but it didn't help:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Mushroom : MonoBehaviour

{

 public float speed;

 public bool moveLeft = true;

 public GameObject camera;

 bool colliding;

 float collidingTime;

 En enemy;

 void Start()
 {
     Physics2D.IgnoreCollision(camera.GetComponent<BoxCollider2D>(), GetComponent<Collider2D>());
     enemy = GetComponent<En>();
 }
 void FixedUpdate()
 {
     if (moveLeft && enemy.isHit == false)
         transform.Translate(Vector2.left * Time.deltaTime * speed, Space.World);
     else if (!moveLeft && enemy.isHit == false)
         transform.Translate(Vector2.right * Time.deltaTime * speed, Space.World);

     if (colliding)
     {
         collidingTime = 0;
     }
     else
     {
         collidingTime += Time.deltaTime;
         if (collidingTime >= 2)
             Destroy(gameObject);
     }
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     colliding = true;

     if (!collision.gameObject.CompareTag("Player") && !collision.gameObject.CompareTag("chunk") && !collision.gameObject.CompareTag("gr"))
     {
         if (moveLeft)
             moveLeft = false;
         else
             moveLeft = true;
     }

     if (collision.gameObject.CompareTag("MainCamera"))
         Destroy(gameObject);
 }

 private void OnCollisionStay2D(Collision2D collision) => colliding = true;

 private void OnCollisionExit2D(Collision2D collision) => colliding = false;

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         collision.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 3, ForceMode2D.Impulse);
         GetComponent<PolygonCollider2D>().enabled = false;
         enemy.startDeath();
     }
 }

} If its wrong give me full information about declaring GetComponents in the Start Method as variables

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

240 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Sprite constantly flipping 0 Answers

Admob plugin lagging when downloading ad 1 Answer

how to shoot bullets from circumference of a circle ? 1 Answer

Each click in Unity opens a window with the text “code compilation”. 0 Answers

What happened to Line Renderer! 1 Answer


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