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 Carterryan1990 · Jan 14, 2019 at 08:09 PM · positionvector3

How to spawn objects within a planes dimensions

alt text

I posted an example of what im trying to accomplish. Any help is greatly appreciated!

ex.png (19.7 kB)
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
1

Answer by Cornelis-de-Jager · Jan 14, 2019 at 09:30 PM

The trick here is to not spawn it in within the dimensions initially. But rather manipulate it once you have spawned it. That way you don't have to worry about rotational offset.

 public Transform plane;
 public GameObject spawnablePrefab;
 
 // Plane Properties
 float x_dim;
 float z_dim;
 
 void Start () {
     // Get the length and width of the plane
     x_dim = plane.size.x;
     z_dim = plane.size.z;
 }
 
 void Spawn () {
     // Spawn the object as a child of the plane. This will solve any rotation issues
     GameObject obj = Instantiate (spawnablePrefab, Vector3.zero, Qauternion.Identity, plane) as GameObject;
 
     /* Move the object to where you want withing in the dimensions of the plane */
     // random the x and z position between bounds
     var x_rand =  Random.Range(-x_dim, x_dim);
     var z_rand =  Random.Range(-z_dim, z_dim);
 
     // Random the y position from the smallest bewteen x and z
     var z_rand = x_rand > z_rand ? Random.Range(0, z_rand) : Random.Range(0, x_rand); 
 
     // Now move the object
     // Since the object is a child of the plane it will automatically handle rotational offset
     obj.transform.position = new Vector3 (x_rand, y_rand, z_rand);
 
     // Now unassign the parent
     obj.parent = null;
 }

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 DCordoba · Jan 14, 2019 at 10:28 PM 1
Share

hahaha, u post a solution where I was testing a answer script whatever it is, uses size of the meshes... almost the same

The most easy approach is, I believe, measure manually the "square" and put this on a script in the plane object, u can set the variables on inspector, or measure a mesh plane attached to gameobject,

the following script measure it on start, delete start function to set custom plane measures by inspector, and the update function is to test...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SquareScript : $$anonymous$$onoBehaviour {
     public float dimX;
     public float dimY;
     public GameObject spawntest;
     int itest = 0;
 
     void Start () {
         $$anonymous$$esh _mesh = transform.GetComponent<$$anonymous$$eshFilter> ().mesh;
         dimX = _mesh.bounds.size.x;
         dimY = _mesh.bounds.size.z;//I assume here that the square is on x and z, otherwise replace for the correct plane axis
     }
 
     //to test purpose, attach to default plane and set a gameobject on spawntest variable
     void Update(){
         if (itest < 5){
             SpawnInside (spawntest);
             itest++;
         }
     }
 
     public void SpawnInside(GameObject spawnObject){
         Vector3 randpos = Vector3.zero;
         randpos.x = Random.Range(-dimX/2f, dimX/2f);//assume mesh of the plane is centered, view mesh.bounds.$$anonymous$$.x and mesh.bounds.max.x if not centered
         randpos.y = 0f;//"level" hoy much up to the plane spawn the objects
         randpos.z = Random.Range(-dimY/2f, dimY/2f);
         Transform instance = Instantiate (spawnObject, this.transform).transform;
         instance.localPosition = randpos;
     }
 }
 

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

136 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

Related Questions

Position in Vector (C#) 1 Answer

Cannot implicitly convert type 'int' to 'UnityEngine.Vector3' 3 Answers

split vector3 coordinates in to x,y,z 1 Answer

collider triggers transform position 1 Answer

Vector3.Lerp works outside of Update() 3 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