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 KasperVildner · Jan 11, 2016 at 06:22 AM · c#gametransform.position

Problems making something happen local c#

I'm trying to make a game with some random generated elements in it. I'm pretty new in unity and C# therefore it's properly a beginner problem. It works fine with the objects centered around the world position (0,0,0). But later i want several platforms and therefore i need it to work locally instead(see image) alt text Pls help :)

Here is my code:

 public GameObject[] prefab = new GameObject[3]; // number of objects to be placed
     public GameObject[] floraObjects; // The objects to be places
     private int numberOfObjects;
     
 
     // The min and max positions to spawn the flora in, to keep it inside of the surface
     public float minX = -1;
     public float maxX = 1;
     public float minY = 0;
     public float maxY = 0;
     public float minZ = -1;
     public float maxZ = 1;
     private int i;
 
     //numbers of objects to create
     public int minObjectSpawning;
     public int maxObjectSpawing;
 
     // Use this for initialization
     void Start () {
 
         
 
         var numberOfObjects = Random.Range(minObjectSpawning, maxObjectSpawing); // create x numbers of objects
         for (int i = 0; i < numberOfObjects; i++)
         {
             int randomObjectFromArray = Random.Range(0, prefab.Length); // Get random object from array
             int randomObject = Random.Range(0, prefab.Length); // get random object
             
             Instantiate(prefab[randomObject], new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ)), Quaternion.identity);
             /*Instantiate(prefab[randomObject], new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ)), Quaternion.identity);*/
            
         }
     }


exc.png (238.4 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
0

Answer by LazyElephant · Jan 11, 2016 at 08:05 AM

To make your objects match the platform, you can do one of two things.

  1. Calculate their world position yourself. If you have some restriction which keeps you from adding a parent to your flora objects, you might have to do this. You would have to add the position of your platform to the randomly generated position you assigned to your flora.

  2. Make your objects the children of the platform where they will be placed. This is the one I would recommend and it is very easy to do. Unity provides a function called SetParent which you can call on your instantiated game objects. It takes two arguments: the parent transform, and a boolean indicating whether to keep the world positioning of your object or translate it to the parent's coordinate space. To implement this in your code, you would add a call to this function under your Instantiate call.

     GameObject temp = Instantiate(---Your code omitted for brevity---) as GameObject;
     temp.transform.SetParent( whateverYourPlatformObjectIs, false );
    
    

By making them children, you could also move your platforms around in the editor after the flora has been placed and the instantiated flora will move with it automatically.

http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make gun aim? 0 Answers

Having some issues with camera position 1 Answer

Loot Rarity System 1 Answer

Code is acting different depending on call 2 Answers

transform.InverseTransformPoint not working correctly in 2019.3.1f1 1 Answer


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