Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sir_ver · Dec 25, 2018 at 07:34 AM · positionenemyenemiesspawnpointsspawnpoint

Problems with spawnpoints

Hey I'm programming a game where the player has to shoot three enemies in a map. If the player shot all of the three enemies, the spawnpoints of them will be changed and they will spawn at the new place. But it doesn't work properly. So if I kill the enemies the player has to trigger the new area and then the enemies will spawn. But if the trigger is on the enemies are spawn always, for a few frames, at the spawnpoint which where one before. I have just 3 spawnpoints for the enemies and I'm changing their positions. And after few frames they have the correct position. I don't understand this, because I have looked in my code and I change the spawnposition first and then activate them. My guess is that because I am doing this: enemies.ActivateSelf(false) they need a little bit to realise that their position has changed. But I'm not sure. I wanted to use the IEnumerator but do you guys have a better solution? Or is this the best? Here my code:

  public void GiveAllEnemiesSameReward(float reward)
     {
 if (!enemyAgent.activeSelf && !enemyAgent2.activeSelf && !enemyAgent3.activeSelf)
         {
 
 if (!isWallMoved)
             {
                 
                 MovingWalls();
                 isWallMoved = true;
             }
 
 if (triggerComp.isTriggered)
             {
                 SetEnemysActive();
                 Debug.Log("Enemies active");
                 triggerComp.SetIsTriggered(false);
                 isWallMoved = false;
                 isTimeIncreased = false;
                 isScoreIncreased = false;
                 isEnemyDone = false;
             }
         }
 }
 
  public void MovingWalls()
     {
         countLevel++;
 
         switch (countLevel)
         {
             case 1:
                 movingWall1.transform.localPosition = spawnPointWall3.transform.position;
 
                 spawnPointPlayer.transform.localPosition = new Vector3(27.2f, 2, 2.6f);
 
                 spawnPointEnemy1.transform.localPosition = new Vector3(62.4f, 2, 14);
                 spawnPointEnemy2.transform.localPosition = new Vector3(62.4f, 2, 1.9f);
 
                 spawnPointEnemy3.transform.localPosition = new Vector3(62.4f, 2, -12.3f);
                 Debug.Log("Spawnpoint verschoben");
                 break;
 }

I know it's not good to hardcode the positions but I didn't know how to do it in a better way.

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 ignacevau · Dec 25, 2018 at 09:54 AM 0
Share

Not sure whether that's the problem you're looking for, but you are missing a bracket in your switch statement.

avatar image sir_ver · Dec 26, 2018 at 07:05 AM 0
Share

No no that's not the problem

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ray2yar · Dec 27, 2018 at 02:07 AM

I'm not even sure what your problem is exactly...

You could try turning off the enemy renderers and colliders and leaving the objects themselves active.

You might also try setting the enemies active before changing their locations.

Maybe use .activeInHeirachy instead of .activeself

Definitely use .SetActive(true) and .SetActive(false) instead

I have provided a code example below to help with avoiding hard coding. You could setup an array of spawn locations and an array of enemies and have them choose a new spawn point from that array randomly (or by a logical method) You'll still have to code in the actual valid points in the array or use an array of empty gameobjects to represent. In the code I provided below you can use the inspector drag you enemies into the array of enemies and some empties as spawn spots in their array. The List of used locations might not update fast enough, so you might need to replace that with an array as well.

If you could clarify the actual issue you're having I'd be glad to help.

     public GameObject[] SpawnLocations;
     public GameObject[] EnemyAgents;
     List<int> UsedPoints;
     GameObject[] Enemies;
 
     private void Start()
     {
         GameObject[] SpawnLocations = GameObject.FindGameObjectsWithTag("SpawnPoint");
 
     }
 
     void OnSpawn()
     {
         int location=0;
         UsedPoints.Clear();
         foreach(GameObject GO in EnemyAgents)
         {
             bool doneFinding = false;
             do
             {
                 location = Random.Range(0, SpawnLocations.Length);
                 if (!UsedPoints.Contains(location)) {
                     doneFinding = true;
                     UsedPoints.Add(location);
                     GO.transform.position = SpawnLocations[location].transform.position;
                     GO.SetActive(true);
                 } 
             } while (!doneFinding);
             //be sure you have plenty of spawnlocations or this loop may not resolve
         }
     }

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

120 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

Related Questions

Get position of prefab enemy ? 0 Answers

3 spawn positions but one is ignored and i dont know why 2 Answers

bullet spawn point is not staying in the right place 0 Answers

How do I make my player re-spawn in its original place on collision of enemy? 1 Answer

Enemy Moves Jitters when Supposed to Be Still 0 Answers


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