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*.transform.position.x - player.transform.position.x <= 3)*
enemy*.SetActive(true);*
}
}
}
Does anyone have idea for optimizing script? There is a lot of things to do. But I stuck there.