- Home /
How to freeze all enemy objects at once
I've looked around and I found a few different things but nothing that seemed to really work for what I was looking for specifically. I'm creating a Dodger game, basically things fall from the sky and you have to dodge. However, if you get hit, I want all of the enemies to freeze where they're at until the scene ends. I don't know how to get them to freeze.
I found this answer (lines 5-8 on best answer), but also found that FreezeAll doesn't work with kinematic rigidbody2d, so I tried it slightly different but it doesn't seem to work for me for some reason.
Here is my code so far. This code does freeze the singular enemy that hits the player, but not all enemies, which is what I'm going for. Thank you in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
float currentSpeed;
Player player;
public float minEnemySpeed = 3f;
public float maxEnemySpeed = 7f;
bool playerDied = false;
void Awake()
{
player = FindObjectOfType<Player>();
currentSpeed = Random.Range(minEnemySpeed, maxEnemySpeed);
}
void Update()
{
if(!playerDied)
{
transform.Translate(Vector2.down * currentSpeed * Time.deltaTime);
}
else
{
Enemy[] enemies = Object.FindObjectsOfType<Enemy>();
foreach (Enemy enemy in enemies)
{
transform.Translate(Vector2.zero);
}
}
}
void OnTriggerEnter2D(Collider2D collider)
{
player = collider.gameObject.GetComponent<Player>();
if(!player) { return; }
playerDied = true;
Destroy(collider.gameObject);
player.Die();
}
}
why do you need this part else { Enemy[] enemies = Object.FindObjectsOfType();
foreach (Enemy enemy in enemies)
{
transform.Translate(Vector2.zero);
}
}
I think it will stop automatically when the player dies. Just make sure that playerDied will turn to "true" when enemy hits the player
enemies has not been initialized or declared yet without the part omitted. Having said that I did try to declare it elsewhere and it was the same problem. The singular object that hits the player freezes, but I want every enemy object that's instantiated to freeze. $$anonymous$$aybe this will help? I have a separate script that spawns the enemies.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : $$anonymous$$onoBehaviour
{
public GameObject[] enemy;
float randX;
Vector2 whereToSpawn;
float spawnRate;
float nextSpawn = 0.0f;
public float $$anonymous$$SpawnTime = 0.0f;
public float maxSpawnTime = 5f;
void Update()
{
if (Time.time > nextSpawn)
{
spawnRate = Random.Range($$anonymous$$SpawnTime, maxSpawnTime);
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-6.0f, 6.0f);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemy[Random.Range(0, enemy.Length)], whereToSpawn, Quaternion.identity);
}
}
}
Answer by jackmw94 · Feb 04, 2021 at 09:23 AM
Okay, so I believe what's happening here is that when the player dies only the enemy that hits the player will have their player dies flag set to true, all the other enemies will still run their own update loop and therefore still translate downwards. By running the Translate(Vector2.Zero) on the other enemies, we're not overwriting their own translate operations so this is redundant as unity_HTr7mOeTIRzvVA was saying.
You already have a reference to your player so you can just check against this instead of each enemy having their own variable. Your player script will / could have a function to determine whether it's alive so replace !playerDied
with a !player.IsDead()
.
Let me know if this works for you / if you have any questions :)