• 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
0
Question by Jezzywezzy101 · May 10, 2018 at 08:10 AM · meshraycasthitpositioninginstantiate prefabmesh collider

How to instantiate objects to mesh while restricting the spawn area on a single axis?

Currently i have it setup so i'm able to instantiate my object onto the mesh by using a raycast to cast down to the mesh's surface. The problem with this is that my instantiated objects can spawn at undesired locations, like the side of mountains or under the water. How can i go about restricting the spawn on a single axis (in this instance i need the .y of the instantiated object to be clamped based upon certain values)


Issue alt text


Thanks in advance.


  public class TreeGenerator : MonoBehaviour {

          public int numberOfObjects;
          public int currentObjects;
          public GameObject treeToPlace;
      
          private float randomX;
          private float randomZ;
          private Renderer r;
      
          void Start() {
      
              r = GetComponent<Renderer>();
          }
      
          void Update() {
              RaycastHit hit;
              if(currentObjects <= numberOfObjects) {

                  randomX = Random.Range(r.bounds.min.x, r.bounds.max.x);
                  randomZ = Random.Range(r.bounds.min.z, r.bounds.max.z);

                  if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y - 5f, randomZ), -Vector3.up, out hit)) {
                      Instantiate(treeToPlace, hit.point, Quaternion.Euler(0, Random.Range(0, 360), 0));
                      currentObjects += 1;
                  }
              }
          }
      }
 
 


trees-in-water.jpg (69.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
Best Answer

Answer by tormentoarmagedoom · May 10, 2018 at 09:35 AM

Good day.

I recoomend you to store the new instantiated object when instantiating. So you can modyfy all you want just at thse same frame is instantiated.

Replace your instantiate code line for this:

 GameObject NewTree = Instantiate(treeToPlace, hit.point, Quaternion.Euler(0, Random.Range(0, 360), 0)) as GameObject;

So now, you have your new tree gameobject in the variable "NewTree", and you can change anything as any other gameobject for example:

  NewTree.transform.position = new Vector3(NewTree.transform.position.x, 15, NewTree.transform.position.z);

This also respond how to change the variable Y from the position vector. (Create a new vector, assign the variables from the transfomr you need and the number of the variable you want to change)


Bye!

:D

If helped, accept the answer

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 Jezzywezzy101 · May 10, 2018 at 09:44 AM 0
Share

Thanks for the speedy response. This seems like a more sensible solution than the one i came up with, thanks for your help! :)

avatar image
0

Answer by Jezzywezzy101 · May 10, 2018 at 09:37 AM

Found a solution for anyone that may need this in the future.

I created a mesh collider on my water plane and add a new tag to it called 'Water'. I then did the same thing for my terrain (mesh) called the tag 'Ground'

from there i simply added && hit.transform.tag == "Ground" to my raycast IF Statement.

 void Update()
     {
         RaycastHit hit;
         if (currentObjects <= numberOfObjects)
         {
             randomX = Random.Range(r.bounds.min.x, r.bounds.max.x);
             randomZ = Random.Range(r.bounds.min.z, r.bounds.max.z);
 
             if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y, randomZ), -Vector3.up, out hit) && hit.transform.tag == "Ground")
             {
                 Instantiate(treeToPlace, hit.point, Quaternion.Euler(0, Random.Range(0, 360), 0));
                 currentObjects += 1;
             }
                 //Instantiate(treeToPlace, new Vector3(randomX, Random.Range(minY, maxY), randomZ), transform.rotation);
                 //currentObjects += 1;
             }
         }
     }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

150 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

Related Questions

Positioning a prefab correctly with Instantiate 3 Answers

Mesh Collider to Mesh Renderer 0 Answers

Empty mesh with vertices and triangles 0 Answers

Mesh Collider not working with Cloth Component 1 Answer

How to extend MeshCollider to cover full mesh,How to make mesh collider extend to cover the whole mesh. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges