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 Astelot · Oct 10, 2014 at 10:46 AM · instantiateendless runnergaps

Instantiate leaving a gap [Endless Runner]

Hello. I'm trying to create endless runner by moving gameobjects under my player instead of actually moving the player.

Everything works fine except every 4th instantiate creates a gap which size varies depending of the speed I'm moving the objects. I have been trying to fix this for a week now and I'm getting kinda depressed about it. Now, I understand I'm destroying the GO after a specific cordinates, -3,5 on Z axis to be specific, and I understand that those numbers won't round perfectly. So sometimes its destroyed on -3,5xxxx and I'm sure thats what could be cousing the problem. But I have no idea how to fix it or some other workaround.

Image: alt text

 using UnityEngine;
 using System.Collections;
 
 public class CubeBehaviour : MonoBehaviour {
     
     public Vector3 speed = new Vector3(0, 0, -20);
     public GameObject prefab;
     
     private Vector3 pos;            // position of rigidbody
     private float length;           // length of object
     
     // Use this for initialization
     void Start () {
         // copy our object's default rigidbody position
         pos = rigidbody.position;
         
         // fetch the object's length.  we'll use this to determine
         // if we've past our camera...
         length = 3.5f;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         
         // move our z towards the camera
         // to do this, we subject our position by the speed
         pos.z = rigidbody.position.z;
         // let's update our rigidbody's position
         // in this exercise, we're just moving the z.  however, we
         // have cached the X and Y in our start function.
         rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
         // if our object's length is past our camera (0,0,0), then...
         if (pos.z < -length)
         {
             // delete our object
             Instantiate(prefab, rigidbody.position + new Vector3(0,0,28f), transform.rotation);
             Destroy(gameObject);
         }
     }
 }
Comment
Add comment · Show 2
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 bubzy · Oct 10, 2014 at 11:21 AM 0
Share

does it work ok if you move the player AFTER you instantiate the new object? if this does, then you can implement another piece of code to destroy the object

avatar image Astelot · Oct 10, 2014 at 01:11 PM 0
Share

Im not moving the player on z axis at all. Only changing its x and y position.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Turkeybag · Oct 10, 2014 at 01:26 PM

I would suggest storing the last object spawned and using it's length value combined with the object your about to spawns length value to adjust the position of the next object spawned so you get pinpoint accuracy. Something like this:

 Instantiate(prefab, LastSpawn.position + new Vector3(0,0,(LastSpawn.Length + AboutToSpawn.Length) /2), transform.rotation);

It's just a base and you need to access the length variable and what not but hopefully that will put you on the right track and good luck!

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 Astelot · Oct 10, 2014 at 06:26 PM 0
Share

As you can see in the image above im using the objects of the same length, so your code would still be the same as $$anonymous$$e. Nothing is wrong with my code either, it does what I want but sometimes creates that gap. I guess its some kind of float precision error. I'm looking for a way around this.

avatar image Turkeybag · Oct 11, 2014 at 12:11 AM 0
Share

Oh I thought you were only using the length to destroy the last object rather than create the new one. Hmm. A workaround could be to move the position in the start function of the object once you spawn it.

avatar image
0

Answer by Enes22 · Dec 09, 2015 at 04:13 PM

I know this tread is one year old, but had the same problem. Fixed with setting Interpolate to Interpolate on rigidbody component. So whoever have same problem try my solution.

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
0

Answer by lvrbrtsn · Jan 02, 2020 at 05:01 AM

I was running into this same issue and found out it had to do with script execution order. My script that determined if it should spawn a road ran before the road movement function. This caused it so have a stale reference to the position by the time Unity actually Instantiates the object.

You can fix this by adding and moving the script that moves your environment to before default time in Project settings > Script Execution Order.

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

6 People are following this question.

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

Related Questions

Instantiate objects in a specific order 2 Answers

Generate a continuos platform for endless runner 3 Answers

instanstiate Spawns more then one objec 0 Answers

How to use the same object more than once at the same time when using an "object pooling" pattern? 3 Answers

Trouble spawning endless platform 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