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 /
  • Help Room /
avatar image
1
Question by Reaverb0t · Aug 14, 2019 at 01:49 AM · spawningboxcollider2d

How to spawn Prefab anywhere within Box Collider 2D?

I have a lot of experience in Unreal and know it's really simple there, I am new to Unity and not sure how I would write code to spawn a prefab anywhere within the dimensions of a Box Collider 2D.

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

1 Reply

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

Answer by Vega4Life · Aug 14, 2019 at 03:12 PM

Here is a quick script. I am just grabbing the size of the collider, getting a random range from those sizes / 2 (because its based on center). Then adding this range to the position of the collider. There is a note to reduce the random range by half the size of the prefab if you wanted it to be perfectly in the box. That's up to you though.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class SpawnInCube : MonoBehaviour
 {
     [SerializeField] GameObject prefab;
     [SerializeField] BoxCollider2D bc;
 
     Vector2 cubeSize;
     Vector2 cubeCenter;
 
 
     private void Awake()
     {
         Transform cubeTrans = bc.GetComponent<Transform>();
         cubeCenter = cubeTrans.position;
 
         // Multiply by scale because it does affect the size of the collider
         cubeSize.x = cubeTrans.localScale.x * bc.size.x;
         cubeSize.y = cubeTrans.localScale.y * bc.size.y;
     }
 
 
     private void Start()
     {
         GameObject go = Instantiate(prefab, GetRandomPosition(), Quaternion.identity);
     }
 
 
     private Vector2 GetRandomPosition()
     {
         // You can also take off half the bounds of the thing you want in the box, so it doesn't extend outside.
         // Right now, the center of the prefab could be right on the extents of the box
         Vector2 randomPosition = new Vector2(Random.Range(-cubeSize.x / 2, cubeSize.x / 2), Random.Range(-cubeSize.y / 2, cubeSize.y / 2));
 
         return cubeCenter + randomPosition;
     }
 }
Comment
Add comment · Show 5 · 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 schicklerdev · Aug 17, 2019 at 07:14 PM 0
Share

For me, "GetRandomPosition()" has a red underline?

avatar image Vega4Life schicklerdev · Aug 17, 2019 at 07:59 PM 1
Share

What does your script look like?

avatar image schicklerdev Vega4Life · Aug 18, 2019 at 12:48 PM 0
Share
 private Vector2 GetRandomPosition()
     {
         Vector2 RandomPositionTop = new Vector2(Random.Range(-NorthBoxSize.x / 2, -NorthBoxSize.x / 2), Random.Range(-NorthBoxSize.y / 2, NorthBoxSize.y / 2));
         Vector2 RandomPositionBottom = new Vector2(Random.Range(-SouthBoxSize.x / 2, SouthBoxSize.x / 2), Random.Range(-SouthBoxSize.y / 2, SouthBoxSize.y / 2));
         Vector2 RandomPositionRight = new Vector2(Random.Range(-EastBoxSize.x / 2, EastBoxSize.x / 2), Random.Range(-EastBoxSize.y / 2, EastBoxSize.y / 2));
         Vector2 RandomPositionLeft = new Vector2(Random.Range(-WestBoxSize.x / 2, WestBoxSize.x / 2) Random.Range(-WestBoxSize.y / 2, WestBoxSize.y / 2));
 
         int Randomizer = Random.Range(0, 4);
 
         if (Randomizer == 0)
         {
             return NorthBoxCenter + RandomPositionTop;
         }
         else if (Randomizer == 1)
         {
             return SouthBoxCenter + RandomPositionBottom;
         }
         else if (Randomizer == 2)
         {
             return EastBoxCenter + RandomPositionRight;
         }
         else if (Randomizer == 3)
         {
             return WestBoxCenter + RandomPositionLeft;
         }
     }

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

181 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

Related Questions

Client Spawning Player Objects 0 Answers

Spawning issues 1 Answer

my objects is spawning on the wall 1 Answer

Random spawn script 1 Answer

make enemy fire at randomly times? 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