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 kubawich · Jun 21, 2016 at 09:27 PM · c#programmingproblem during runtime

How to instantiate object dependent on position

Hi, I'm trying to make clouds system for my android game, and it should work like this, if player position is greater than 10 start generating clouds, and generate next one if player y position is grater than last generated cloud + 10, but idk how to do this :< I've already tried timers, for, and do while loops, and anything isn't working, clouds are generating each frame. Here's code for basic generating:

http://pastebin.com/n3CsmszF

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
Best Answer

Answer by Dibbie · Jun 22, 2016 at 05:06 AM

Firstly, NEVER run any type of "loop" inside an Update, BECAUSE that it runs every frame, youd essentially be trying to run a loop... Every frame, which is performance wise, essentially saying "goodbye" to your players CPU, and yours, because of the amount of resources its using to do something unnecessary. (and doing that, Unity will probably crash on you)

Second, what you want to do is a little bit of whats known as "Procedural Generation" its when the code figures out how to make something look endless or seamless without the assistance of a human or manual copy/paste essentially...

All you need to achieve it, is something like this - and this is all untested C# code...

 //Global variables
 
 public GameObject cloud; //the object you want to be instantiated over and over
 
 public GameObject player; //reference to your player
 
 public float distance = 10f; //the minimal distance each cloud should be apart from eachother
 
 public float timeframe = 3f; //the amount of time before the next cloud should be spawned
 
 private Transform lastCloud; //save the location of the last cloud for the next one to spawn
 
 void GenerateClouds(){
 if(lastCloud == null){
 lastCloud = (GameObject) Instantiate(cloud,new Vector3(10f,0,0), cloud.rotation);
 } else {
 lastCloud = (GameObject) Instantiate(cloud,new Vector3(lastCloud.position.x + distance, 0, 0), cloud.rotation);
 }
 }

 //----------------------------------------------------------------------------------------

 //In Update you can use it like this:
 void Update(){
 if(player.transform.position.x > distance){
 Invoke("GenerateClouds", timeframe);
 }

 //----------------------------------------------------------------------------------------

 //Or you can use it in Start instead, like this:
 void Start(){
 InvokeRepeating("GenerateClouds",timeframe,timeframe);
 }
 
 //If you use it in Start instead, you just have to surround the GenerateClouds function with that if-statement, so it would then look like this:
 void GenerateClouds(){
 if(player.transform.position.x > distance){
 if(lastCloud == null){
 lastCloud = (GameObject) Instantiate(cloud,new Vector3(10f,0,0), cloud.rotation);
 } else {
 lastCloud = (GameObject) Instantiate(cloud,new Vector3(lastCloud.position.x + distance, 0, 0), cloud.rotation);
 }
 }
 }


If im understanding your question right, that should achieve what you want, given, youll most likely have to tweak it a bit to work for your game, but mostly things like value changes, that is, if you choose to copy/paste rather than use that script as a reference or guide for building your own, that may be more efficient for your game.

Comment
Add comment · Show 2 · 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 kubawich · Jun 22, 2016 at 12:02 PM 0
Share

Oh man, you're an angel, i've just asked for logic, not whole implementation, thank you. I'll try to do something with this, because it's not working anyway(now spawning clouds, and if i try debug.log in if/else it doesn't appear) But thank you anyway :D

avatar image Dibbie kubawich · Jun 22, 2016 at 03:41 PM 0
Share

No problem, if you found my answer helpful as well, please give it an upvote or mark it as best answer =)

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

Why does my audio get messed up in the last part of this script? 0 Answers

How to make a surface glow when its hit by a laser. 0 Answers

C# for applications vs C# for Unity help 3 Answers

Non-MonoBehaviour object can access to its functions or interact with a MonoBehaviour'ed object in someway? 0 Answers

how to use class or struct in unity api? 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