Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Saintforhire · Feb 04, 2021 at 12:25 AM · 2dcollisionrigidbody2d

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


Comment
Add comment · Show 2
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 unity_HTr7mOeTIRzvVA · Feb 04, 2021 at 01:13 AM 0
Share

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

avatar image Saintforhire unity_HTr7mOeTIRzvVA · Feb 04, 2021 at 01:24 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 :)

Comment
Add comment · 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

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

312 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 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

Rigidbody2D.velocity out of controll 3 Answers

Why are tilemaps so bad ? 2 Answers

How can I prevend two Circle Collider's (2D) overlapping on collision? 1 Answer

2D Pong Ball Not Reflecting Off Walls "Exactly" 1 Answer

Unity2D game objects don't collide 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