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 /
avatar image
0
Question by ThuverX · Aug 28, 2015 at 06:03 PM · unity 53dscript.

Random generation

I want to create a tube spawning mechanic. I had something done, but for some reason it does not want to go in the direction I want it to go. It also does not go further then one object, it just spawns in one another.

Code: using UnityEngine; using System.Collections;

 public class TubeCreation : MonoBehaviour {
     public GameObject tubeObject = null;
     public float add = 100000f;
 
     void Update () {
         Debug.Log("<color=blue>Info:</color> Object Created");
         tubeObject = Instantiate (Resources.Load ("Prefabs/prefab_tube"), new Vector3(0,0,287),Quaternion.Euler(0,90,270)) as GameObject;
         tubeObject.transform.Translate(Vector3.right * add * Time.deltaTime);
     }
 }
 

My ships model is backwards. So I use back to go forward, please keep that in mind.

Comment
Add comment · Show 4
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 mtdrume · Aug 28, 2015 at 08:11 PM 1
Share

You are trying to spawn a tube every frame from your ship?

avatar image ThuverX · Aug 28, 2015 at 08:47 PM 0
Share

I spawn it from an static object.

avatar image Salmjak · Aug 28, 2015 at 08:48 PM 1
Share

I notice that you just use the same coordinates for all pieces. Notice that Time.deltaTime is "The time in seconds it took to complete the last frame (Read Only).". On a "strong" computer this will (probably) be about the same value. You don't use any varying variable, so it will only spawn in one place.

avatar image ThuverX · Aug 29, 2015 at 08:03 AM 0
Share

Thanks @Salmjak . But I don't really get it are you saying I need the default value and then add coordinates every time? How would I do this?(Also, it really lags do you have any fix for that to?)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Suddoha · Aug 29, 2015 at 09:45 AM

Quite a few things to note:

  • Instantiation on each frame is quite expensive, performance-wise.

  • Instantiation with Resource.Load frequently is even worse.

Solution: You should be using the public GameObject variable in order to assign your prefab from the project folder. Also, you shouldn't instantiate per frame, in this case, this doesn't seem to make sense anyway, unless you need ~ 50-60 tubes to be spawned per second, which i doubt.

  • You've got a fixed value for the vector's z-value.

  • When you feel like multiplying Time.deltaTime with such a huge number, there's often something wrong (not always).

In order to simplify all that, use an integer or float variable which starts with the first z-value, or use a Vector3 in order to store x,y,z at once. Take another variable that defines your offset for each instantiation (float, int or Vector3, just as you like).

I fixed a few things and added a key input for the spawn control, play around with it and you'll get the idea. The current offset is 1 unit in x-direction for testing purposes with Unity's built-in cube.

Here's the code:

 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     // drag the prefab from the project folder in the inspector slot
     public GameObject tubeObject;
     // current startPosition (I've chosen 0,0,0 here)
     public Vector3 nextSpawnPosition = Vector3.zero;
     // the offset 1,0,0 
     public Vector3 offset = Vector3.right;
 
     void Update()
     {
         // only instantiate when space bar is pressed
         if(Input.GetKeyDown(KeyCode.Space) && tubeObject != null)
         {
             Instantiate(tubeObject, nextSpawnPosition, Quaternion.Euler(0, 90, 270));
             // add the value to the spawnPosition
             nextSpawnPosition += offset;
         }
     }
 }
Comment
Add comment · Show 5 · 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 ThuverX · Aug 29, 2015 at 10:28 AM 0
Share

How can I make it so it does not need to press space?

avatar image Suddoha · Aug 29, 2015 at 10:52 AM 0
Share

@ThuverX If you don't want to press a button, only put

 Instantiate(tubeObject, nextSpawnPosition, Quaternion.Euler(0, 90, 270));

into Update. But then again, you'll instantiate per frame and very, very often. I really doubt you want that.

avatar image ThuverX · Aug 29, 2015 at 10:55 AM 0
Share

@Suddoha Can I make it generate when I'm only in range? For less lag?

avatar image Suddoha · Aug 29, 2015 at 11:16 AM 1
Share

@ThuverX Yes, you actually can. There are methods like Vector3.Distance and others that you should take a look at. Unfortunately, i cannot write all the code for you.

avatar image ThuverX · Aug 29, 2015 at 11:21 AM 0
Share

Ok @Suddoha thanks anyways!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

HELP! Scripts looks fine but doesn't respond? 1 Answer

How do i access the MeshExtrusion.cs from Unity's PE? 1 Answer

What is the best way nowadays to record a film/movie(mp4) with Unity? 1 Answer

Accessing color presets in c# script. 1 Answer

Animation not working correctly 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