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
1
Question by BahamutZeRo · Oct 21, 2015 at 09:42 AM · objectoptimizationimageoverlapping

Spawning at collision free Location

Hello there! Let's make this straight. The Object i'm spawning it does spawned overlapping with other object. Those objects have collider already. But it doesn't work. Hmm kindly seek for your help any kind soul ^^. Thank you!

Here's the code.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnSys : MonoBehaviour 
 {
     public float spawnTime = 5f;        // The amount of time between each spawn.
     public float spawnDelay = 3f;        // The amount of time before spawning starts.
 //    public float xRange = 1.0f;
 //    public float yRange = 1.0f;
     public GameObject[] enemy;
     private bool canSpawn;
 
     //public GameObject prefab;// Array of enemy prefabs.
     
     void Start ()
     {
         // Start calling the Spawn function repeatedly after a delay .
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Seabed")
         {
             canSpawn = false;
         }
         else
         {
             canSpawn = true;
         }
     }
     void Spawn ()
     {
         if (canSpawn == true)
         {
             float randomX = Random.Range(-8f,10f);
             float randomY = Random.Range (1.5f,2.8f);
             int enemyIndex = Random.Range(0,enemy.Length);
             //Instantiate(enemy[enemyIndex],transform.position, transform.rotation);
             Instantiate(enemy[enemyIndex], new Vector3(randomX, randomY, 0), Quaternion.identity);
         }
     }
 
 
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CanCo · Oct 21, 2015 at 10:21 AM

Some things I do:

  • Create a list of positions where the enemies can be spawned (or better, a cube area range),
  • Using raycast (spherecast, capsulecast
  • Place them in the sky (this seriously isn't recommended, but funny to watch).

I wrote a code, basically what happens is it runs the function SpawnRandom the set amount of times (toSpawn), runs a raycast downwards checking if there is anything below a randomly chosen spot, if there's something below it will check if the tag of the collider (transform in the code) is the specified spawning tag (string tag). I used the default "Respawn" tag. Here's the simple code:

 using UnityEngine;
 using System.Collections;
 
 public class spawns : MonoBehaviour {
 
     public Transform enemy;
     public int toSpawn = 10;
     public Vector3 size; //DOES NOT USE THE Y-SIZE!!
     public string tag;
 
 
     private RaycastHit hit;
 
 
     void Start () {
         for(int n = 0; n < toSpawn; n++) {
             SpawnRandom(n);
         }
     }
     
     void SpawnRandom (int n) {
         if (Physics.Raycast(transform.position + new Vector3(Random.Range(-size.x/2, size.x/2), 0, Random.Range(-size.z/2, size.z/2)), Vector3.down, out hit))
         {
             if (hit.transform.tag == tag)
             {
                 Instantiate(enemy, hit.point + hit.normal * 0.5f, Quaternion.identity);
             }
         }
     }
 
     void OnDrawGizmosSelected () {
         Gizmos.color = new Color(0, 1, 0, 0.4f);
         Gizmos.DrawCube(transform.position, size);
         Gizmos.color = new Color(0, 1, 0, 1);
         Gizmos.DrawWireCube(transform.position, size);
     }
 
 }

Comment
Add comment · Show 3 · 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 BahamutZeRo · Oct 22, 2015 at 02:08 AM 0
Share

Hello Canco! I'm kinda new to Unity, so may I request for a example of the codes from you? So I can use it as reference ^_^.

avatar image CanCo BahamutZeRo · Oct 22, 2015 at 05:05 AM 0
Share

Okay, I've created a code for you. It will probably have to be modified to your situation (I forgot to mention that it checks the raycast from the y-position).

avatar image BahamutZeRo CanCo · Oct 22, 2015 at 07:21 AM 0
Share

Hey Canco, You know what, so sorry I forgot to tell you that i'm creating 2D. It seems like your code only work for 3D. Or is it work for both?

Haha my bad!

avatar image
0

Answer by npatch · Oct 21, 2015 at 11:21 AM

You can create a layer (there's the Ignore Raycast layer as well but I haven't used that so I can't vouch for that) and then go to Edit->Project Settings-> Physics and uncheck collisions from all layers with that new one in the Layer Collision Matrix. Basically you are telling Unity that anything in that layer should not collider with any other layer.

Then instantiate your enemies anywhere...

The only thing you have to do is when you want their colliders to work, just change their layer to either Default or some other layer you've made for enemies which can collide with other layers.

Comment
Add comment · Show 4 · 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 BahamutZeRo · Oct 22, 2015 at 02:06 AM 0
Share

Hey npatch~ Your solutions I've tried but it seems not that effective. :) Or maybe it just that I couldn't make it. But I'll try to figure out in a moment!

avatar image npatch BahamutZeRo · Oct 22, 2015 at 06:17 AM 0
Share

Actually I misread, sorry for that. CanCo was on point.

Another solution would be to use RigidBody on the enemies to have them use the Physics module and once you spawn something inside a collider the physics alone will propel it outside since they collide.You can add a slight height to the spawning point to let it collide straight away without getting spawned inside like this:

 GameObject spawned = Instantiate<GameObject>(prefab);
 spawned.transform.SetParent(spawnPoint);
 spawned.transform.localPosition = new Vector3(0,1,0);

Though this solution depends on whether you want to use Physics at all.

In any way, the Layer Collision $$anonymous$$atrix is going to help you tremendously once you get a feel for it so take a look at it anyway since collisions are part of the engine and it's going to be way faster than trying to do everything from code and using raycasts.

For now I'm glad you got an answer that helped you and worked for you.

avatar image BahamutZeRo npatch · Oct 22, 2015 at 07:25 AM 0
Share

Guess what, the rigidbody you say it apply well for my 2D game just that the object takes time to move out from the collider. Although is not hard coded but still there! Yes I will look into the Layer Collision $$anonymous$$atrix in future! Thank you very much and appreciate your help!

Show more comments

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

Optimizing thousands of game objects 3 Answers

Many objects - optimization 1 Answer

Fit image to plane 1 Answer

Most performance friendly way to "unrender" a GameObject? 6 Answers

Anyone know which image file type unity reads the fastest generally speaking? 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