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
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.

Follow this Question

Answers Answers and Comments

149 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

Related Questions

Animated rigged blender model generating colliders problem 1 Answer

How can I recalculate mesh collider to fit mesh? 0 Answers

How do I make it so the rigibody on the parent uses the mesh collider on the children ? 0 Answers

Prefabs aren't instantiating where they should be 0 Answers

How to implement dart game? 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