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 corinne44 · Sep 27, 2016 at 10:41 AM · c#networkrandomspawnenemies

Generate gameObject horde, more positioned towards front of horde

I am trying to create a non-uniform random range of enemy characters in a scene. I want there to be more enemies towards the front of the horde and less trailing off towards the back of the horde. How would I do something like this? I figured maybe create an empty gameObject and have the generated enemies target it like

 public Vector3 target : Transform;

 if (target == null && GameObject.FindWithTag("Empty"))
 {
          target = GameObject.FindWithTag("Empty").transform;
 }

But I feel like that would not work. Does anyone have any ideas?

Here is my method for spawning the horde

     void SpawnHorde()
     {
         for (int i = 0; i < hordeCount; i++) 
         {
             Vector3 spawnPosition = new Vector3(Random.Range (-location.x, location.x), location.y, location.z);
             GameObject obj = Instantiate(horde, spawnPosition, Quaternion.identity) as GameObject;
         }
     }


Maybe set bounds for the location?? Ugh I'm stumped

here is an image of how I want to generate them: alt text

horde.png (33.1 kB)
Comment
Add comment · Show 3
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 sharnold52 · Sep 19, 2017 at 07:03 PM 0
Share

Hey, you ever figure that out? I am trying to do the same thing right now and I am completely stumped.

avatar image corinne44 sharnold52 · Sep 19, 2017 at 07:11 PM 0
Share

Yes I figured it out. Lol, are you in IG$$anonymous$$E 202?

avatar image sharnold52 corinne44 · Sep 20, 2017 at 02:05 AM 0
Share

Yeah I am, haha

Thank you for the answer, it's making more sense now, I got the other parts of the assignment down, just was having trouble with this one. :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by corinne44 · Sep 19, 2017 at 07:14 PM

@sharnold52

 // Name
 // NonUniform - A class that generate a larger number of characters whose position is based upon non-uniform random values.
 // No known errors. For the purpose of this assignment, variables have been made private and their values have been hardcoded.
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NonUniform : MonoBehaviour 
 {
     private int hordeCount; // Number of horde Prefabs to generate, 50-100
     private List<string> collisions = new List<string>(); // A list of strings containing all the detected collisions
 
     // Methods
     // Use this for initialization
     void Start () 
     {
         SpawnHorde ();
     }
 
     void SpawnHorde()
     {
         int hordeCount = 100;
 
         float zPosition = 0;
 
         const int maxInColumn = 10;
 
         while (hordeCount > 0) 
         {
             int numberInCol = Random.Range (5, maxInColumn);
             hordeCount -= numberInCol;
 
             if(hordeCount < 0)
             {
                 numberInCol += hordeCount;
             }
 
             for(int i = 0; i < numberInCol; i++)
             {
                 int x = Random.Range (0, 200);
                 float z = 40 + zPosition;
                 float y = Terrain.activeTerrain.SampleHeight (new Vector3 (x, 0, z)) + Terrain.activeTerrain.GetPosition().y;
                 Vector3 spawnPosition = new Vector3(Random.Range (0, 200), y, 40 + zPosition);
                 Instantiate(Resources.Load ("Prefabs/Mum"), spawnPosition, Quaternion.identity);
             }
             zPosition += (float)maxInColumn * 20f / (float)hordeCount;
         }
     }
 
     /// <summary>
     /// Raises the collison enter event; checks to make sure Gaussian Leads are not colliding with any other models.
     /// Although I realize that since this script is attached to the Terrain and not a gameObject, it doesn't work as desired.
     /// </summary>
     /// <param name="col">Col.</param>
     void OnCollisonEnter(Collision col)
     {
         collisions.Add (col.gameObject.name);
         if (collisions.Contains ("Mummy") || collisions.Contains ("Barrel2") || collisions.Contains ("Lara")) 
         {
             Destroy (gameObject);
         }
     }
 
     void OnCollisionExit(Collision col)
     {
         collisions.Remove (col.gameObject.name); // When the objects are no longer colliding, remove them from the list of detected collisions
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies spawn on top of each other(C#)(Unity) 1 Answer

How to create random spawn for an object? (C#) 0 Answers

AI spawning areas. What are the ways to make them? 1 Answer

Multiple Cars not working 1 Answer

How to Spawn an Extra Platform for the Player to Jump On If There Are None Within the Player's Field of View? 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