Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sndwav · Nov 24, 2011 at 09:33 PM · instantiateprefabinvokerepeatingrunnercontinuous

Continuous instantiation of a moving prefab

Hello, I'm trying to create a simple runner game, and I decided to create prefabs of segments and move them towards the player, while the player is only animating the running.

The idea is to instantiate a new segment of the tunnel, and when it passes the player, to remove it... and continue doing so endlessly.

My problem is that I don't know how to make sure the segments are instantiating right next to each other, so there won't be a gap and no overlapping.

Currently, I'm using an InvokeRepeating on the function that will instantiate a prefab (with the script that moves it already attached). But I'm getting inconsistent gaps between the prefabs.. I'm guessing it's because the InvokeRepeating method doesn't use the deltaTime or something of that nature... and I think that it's generally not the best practice for this kind of thing (correct me if I'm wrong).

I thought about checking the position of the prefab, and when it reaches a certain point then I will instantiate a new segment, but this can also cause inconsistent gaps, I think, since the prefab can move move more than one point if there is a lag, right? and I need the new segment to "attach" itself to the previous segment.

I also made an illustration to explain it more visually.

It's a Side View of what's going on. The one pixel gap between segments is just to make it possible to see them.

alt text

Can anyone point me in the right direction as to how to generally do this sort of thing with the position precision I need?

Thanks!

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 Nicolinux · Jan 21, 2012 at 12:15 AM

You need to take the width of your segments into account and define a point (offscreen) where you want the replacement to appear. Then in Update() simply check the transform.position.x if it is less then your defined point. If it is, then move your segment there via transform.position = ... Here is an example. I have defined leftSide as the outer most point and rightSide as the right most point:

var speed : float; var leftSide: float; var rightSide: float;

function Update () {

var move : float = speed Time.deltaTime; transform.Translate(Vector3.left move, Space.World);

if (transform.position.x < leftSide) { transform.position = Vector3(rightSide, transform.position.y, transform.position.z); }

}

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
avatar image
1

Answer by Rick.Miranda · Sep 15, 2013 at 04:04 PM

Hi, I see this was posted quite a while back. But I've been dealing with this problem myself. I've been needing to generate scrolling background and was also having the issue with the gap (or seam) between instances. Not sure how you are implementing this. But I will explain the way I am implementing. I start off with two background objects stacked vertically. They scroll downwards. Each background object has the script attached that checks the current position. If the position is the final position, will create a new background object on top of the second background object, and the point is to do it so that it is seamless. The check for this position was written as: if (transform.position <= final position). Then I would place the new object a fixed distance from the second object. But this distance was fixed and did not respond to any position error. So if it so happened that the object position was less than the final, there was an error amount associated with the placement of the new object. So the way I corrected this was by calculating the error, then the placement would be the fixed amount (based on the length of the tile) minus the error. This worked charmingly.

I'm sure by now you have moved on to another project or have solved it but if you need the code let me know.

Right now, I'm trying to figure out how to do the same procedure but now with different prefabs to give the appearance of changing background, like driving a car over changing landscape. Up to now, I've been using transform.position = new Vector 3( ) to generate the new prefab but since now I want to access other prefabs in the assets folder, I'm trying to use "Instantiate( )". But can't figure out why this is causing the prefabs to clone back to back instead of only when the object position reaches final.

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

continuous shooting 1 Answer

Updating a variable on a script in an instanced object 1 Answer

Instantiate Primitive Code Stopping Unexpectedly 2 Answers

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiating prefab with certain coordinates problem (c#) 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