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();
}
}