- Home /
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.
yo are using getComponent in fixed update, and on collision stay, those 2 things are probably slowing it down the most.
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.
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.
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
Follow this Question
Related Questions
2D Sprite constantly flipping 0 Answers
Find Tilemap Player is Currently At 0 Answers
2D Top Down SmokeBomb 0 Answers
Trying to get gameobject to point to mouse in a orbit around the player 2D 1 Answer
2D game kit Door isn't opening 3 Answers