Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Chocolade · Mar 29, 2017 at 11:10 PM · c#scripting problemscript.

How can i spawn new gameobjects to be inside the terrain area ?

I have two problems.

First I'm cloning objects inside the terrain area only. The problem is when i change the objects size for example to 50 some of them if they are near the terrain edge half of them inside the terrain half outside. And i want to make sure that all the objects in this case cubes will be inside the terrain area. How can i fix it ?

Second problem, How can i make a new variable to set the minimum space between each cubes ? Since i spawn them in random position the space between them is also random but i want to be able to change and set the minimum space that should be between them. For example if the minimum space is 10 then the random space between the cubes should be 10 and above. Random of course since i spawn them in random positions but the spaces between each cubes should be minimum 10 or what ever i will set.

This is the script i'm using:

 using System;
 using UnityEngine;
 using Random = UnityEngine.Random;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class CloneObjects : MonoBehaviour {
 
     public GameObject objectToClone;
     public int objectsStartingHeight = 20;
     [HideInInspector]
     public GameObject[] objects;
 
     // for tracking properties change
     private Vector3 _extents;
     private int _objectCount;
     private float _objectSize;
 
     /// <summary>
     ///     How far to place spheres randomly.
     /// </summary>
     public Vector3 Extents;
 
     /// <summary>
     ///     How many spheres wanted.
     /// </summary>
     public int ObjectCount;
     public float ObjectSize;
 
     // Use this for initialization
     void Start () {
 
         Clone();
         objects = GameObject.FindGameObjectsWithTag("objectToClone");
     }
 
     private void OnValidate()
     {
         // prevent wrong values to be entered
         Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
         ObjectCount = Mathf.Max(0, ObjectCount);
         ObjectSize = Mathf.Max(0.0f, ObjectSize);
     }
 
     private void Reset()
     {
         Extents = new Vector3(250.0f, 20.0f, 250.0f);
         ObjectCount = 100;
         ObjectSize = 20.0f;
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 
     private void Clone()
     {
         if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
             return;
 
         // cleanup
         var ObjectToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");
         foreach (var t in ObjectToDestroy)
         {
             if (Application.isEditor)
             {
                 DestroyImmediate(t);
             }
             else
             {
                 Destroy(t);
             }
         }
 
         var withTag = GameObject.FindWithTag("Terrain");
         if (withTag == null)
             throw new InvalidOperationException("Terrain not found");
 
         for (var i = 0; i < ObjectCount; i++)
         {
             var o = Instantiate(objectToClone);
             o.tag = "ClonedObject";
             o.transform.SetParent(base.gameObject.transform);
             o.transform.localScale = new Vector3(ObjectSize, ObjectSize, ObjectSize);
 
             // get random position
             var x = Random.Range(-Extents.x, Extents.x);
             var y = Extents.y; // sphere altitude relative to terrain below
             var z = Random.Range(-Extents.z, Extents.z);
             
             // now send a ray down terrain to adjust Y according terrain below
             var height = 10000.0f; // should be higher than highest terrain altitude
             var origin = new Vector3(x, height, z);
             var ray = new Ray(origin, Vector3.down);
             RaycastHit hit;
             var maxDistance = 20000.0f;
             var nameToLayer = LayerMask.NameToLayer("Terrain");
             var layerMask = 1 << nameToLayer;
             if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
             {
                 var distance = hit.distance;
                 y = height - distance + y; // adjust
             }
             else
             {
                 Debug.LogWarning("Terrain not hit, using default height !");
             }
 
             //o.transform.Rotate(0.0f,randomNumbers[i],0.0f);
             // place !
             o.transform.position = new Vector3(x, y+objectsStartingHeight, z);
         }
 
         _extents = Extents;
         _objectCount = ObjectCount;
         _objectSize = ObjectSize;
     }
 }
 

And this is when i set the size to 50 in some places the cubes half in half out the terrain:

Half in half out

And this is a screenshot show the script settings in the inspector: Y is set to 0.5 and creating 10 objects and the size of each object is 50. All the objects have the same size.

Settings

And last the terrain position is X = -250 Y = 0 and Z = -250 The terrain size is Width = 500 Length = 500 Height = 600

ss94.jpg (133.8 kB)
ss95.jpg (61.9 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Rhombuster · Mar 29, 2017 at 11:23 PM

I was helping you on the other site, but I didn't have an account so it wouldn't let me comment on your other question. You're working with randomness, so if you really want to avoid clipping and keeping it in bounds.

 1) Do a raycast from the corner of each cube (4 raycasts per cube).
 2) if any of the rays don't hit the terrain OR hit another cube, reject this position and rerun this iteration of the loop

This will be slow, but you aren't doing it at run time so you should be ok.

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 Chocolade · Mar 30, 2017 at 03:21 AM 0
Share

I added more 3 lines:

 var ray1 = new Ray(origin, Vector3.up);
 var ray2 = new Ray(origin, Vector3.left);
 var ray3 = new Ray(origin, Vector3.right);

Is that right ? And the rest i'm not sure yet how to do it.

avatar image
0

Answer by UnityCoach · Mar 30, 2017 at 11:57 AM

Here are a few things to consider :

1) you don't need to parent objects to work with a local position, you can use Transform.InverseTransformPoint() and Transform.TransformPoint() to get an object's position in another's space.

2) you can access the Object Mesh via the MeshFilter, and get its bounds to know how far off the terrain edge it should land. This will be useful if/when you need to rotate objects.

3) I would only use RayCasting to locate the terrain height/normal and align objects.

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

310 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 can i make the spaceship to land/take off with physics vertical ? 2 Answers

To pass a variable between scenes should I use scriptableobject or static ? 1 Answer

How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers

How can i lock the mouse cursor in the middle of the screen ? 1 Answer

Why InputField don't have the property text ? 2 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