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 RandomUser123 · Aug 16, 2014 at 06:31 PM · instantiatespawnoffset

Spawning enemies at an offset

I have fixed spawn locations in my game and I would like to spawn my enemies within a range close to the spawn point, rather then the enemies coming straight from the one fixed point if you get me.

Is there a way I can do that with my current setup?

 using UnityEngine;
 using System.Collections;
 
 public class spawnBall : MonoBehaviour 
 {
     //The spawn points for the enemies
     Vector3[] spawnPoints;
 
     //Initialises the end of the array for the enemies
     int endOfArray;
 
     //The wait time before the coroutine will start
     private int timeToBegin = 2;
     
     //Determines the spawn time of the enemies
     private float spawnTime = 2;
     
     //The object spawned towards the enemy
     public GameObject[] randomBall;
 
     void Start()
     {
 
         //Finds all ovjects marked with the tag Spawn Points
         GameObject[] objs = GameObject.FindGameObjectsWithTag("SpawnPoints") as GameObject[] ;
         
         //Sets the spawnPoints varibale to the size of the objs array
         spawnPoints = new Vector3[objs.Length] ;
         
         //for loop which trasfers all data into the array
         for(int i = 0 ; i < objs.Length ; i++)
         {
             spawnPoints[i] = objs[i].transform.position ;
         }
         //sets the end point for the array
         endOfArray = spawnPoints.Length;
         
         //Starts the coroutines for the enemies and missiles
         StartCoroutine(Spawn());
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     //Instantiates a new enemy based on a time given by the players score
     IEnumerator Spawn()
     {
 
 
         yield return new WaitForSeconds(timeToBegin);
         while (true)
         {
             int prefeb_num = Random.Range(0,15);
              //I'd like to create the offset here on the x-axis
             Instantiate(randomBall[prefeb_num], spawnPoints[Random.Range(0,endOfArray)], Quaternion.identity);
             yield return new WaitForSeconds(spawnTime);
         }
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 100, 100), "FPS: " + (int)(1.0f / Time.smoothDeltaTime));        
     }
 
 
 }

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 PaulOrac · Aug 16, 2014 at 06:54 PM 1
Share

http://docs.unity3d.com/ScriptReference/Random-insideUnitSphere.html

When you instantiate your object you can mix that to the position and adjust the y axis to the floor and the radius from the spot you wish to Spawn.

Something like this:

 Vector3 Pos = spawnPoints[Random.Range(0,endOfArray)] + Random.insideUnitSphere * radius;
 
 Vector3 SpawnPos = new Vector3(Pos.x, 0, Pos.z);
 
 Instantiate(randomBall[prefeb_num], spawnPos , Quaternion.identity);



It's the best that comes to my $$anonymous$$d right now. I haven't tested it but I think it might work... I hope it helps you getting on the right track. Cheers!

avatar image RandomUser123 · Aug 16, 2014 at 07:02 PM 0
Share

Cheers Paul :)

1 Reply

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

Answer by Sindorej · Aug 16, 2014 at 06:39 PM

You can do something like this :

 Float maxDistance = 15f;
 Vector3 spawnpoint = spawnPoints[Random.Range(0,10)];
 Vector3 target_pos = spawnpoint + new Vector3(Random.Range(0,maxDistance), Random.Range(0,maxDistance),Random.Range(0,maxDistance));
 Instantiate(prefab, target_pos, prefab.transform.rotation);

You can of course change the range to negative, or make Y zero so they don't spawn in the air.

Comment
Add comment · Show 1 · 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
avatar image RandomUser123 · Aug 16, 2014 at 06:52 PM 0
Share

This works a charm, thanks a million for the help :)

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

24 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

Related Questions

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

Respawning Single Enemy at a Random Range 1 Answer

Instantiate a Prefab with click in a certain area 2 Answers

UNet - How do I make Network.Spawn not show the prefab to the user that called it? 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