• 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 hcho · Jan 31, 2011 at 08:57 AM · characterbackgroundmoving-platform

infinite BG moving towards the character.

I'm trying to create a game with a moving BG which keeps moving towards the character. I currently have 2 boxes which shares the same script below to give it an illusion that the character is running in an infinite map.

var BGSpeed = 100.0;

 function Update() 
 {
     if (transform.position.z <= -30) //if the bg geo passes the character, it'll go back to the beginning.
 {

         transform.position.z=69.45;
         Spawn();

 }
     else
     {

// Move the object forward along its z axis 1 unit/second BGSpeed. transform.Translate(Vector3.forward Time.deltaTime * -BGSpeed ); }

 }

The problem I'm having with this script is that it's very unstable. it seems like time.deltaTime is not acurate enough and I can see the seam between the two boxes.If I run the game long enough, sometimes the gap between the two boxes get so big, the character fall through..

Any ideas? Thanks in advance,

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 james flowerdew · Jan 31, 2011 at 01:08 PM

I think that the key is not to work with moving your object to a set point, but to an offset that compensates. This negates innacuracy . Here's my solution to a 3D tiling infinite landscape, and it works very well...

public static Vector3 RepeatPos (Vector3 nPos){ Vector3 vPos=nPos; if(gTiling){ if(gGoody!=null){ Vector3 vDelta=vPos-goodyScript.GetPos(); if (vDelta.x>gMaxDist){ vPos.x-=gMazWid; }else if(vDelta.x<-gMaxDist){ vPos.x+=gMazWid; } if (vDelta.z>gMaxDist){ vPos.z-=gMazWid; }else if(vDelta.z<-gMaxDist){ vPos.z+=gMazWid; } } } return (vPos); }

This is called by any object that needs to tile, and does not reference time at all. note: gmazwid is just a number and gmaxdist is exactly half, so it basically moves any object that you've gone a certain distance away to the other side in 3D space.

-----further notes.....

As a note: the original question sounds like all that is needed is minor changes as hinted above... The solution posted is an example of tiling that looks ok from all angles, not just firing down one given axis, so... simpler solution for 2D tiling:

if(transform.position.x<=0){ transform.position.x+=gMaxDist; }

instead of "transform.position.x=gMaxDist;" which would result in gaps and offsets.

the other code above does work, but depends on all the above variables being set and accessible, and is "fully 3D".

firstly this behaviour should belong somewhere like in a "gamescript", as if you use it, you'll use it a hell of a lot.

try replacing my "goodyScript.GetPos()" with the goody's transform.position somehow (that's all "GetPos()" returns from the goodyscript), you could have a static "Pos" variable in the goody.

You'll also need to set your gMazwid and gMaxDist (gMazWid is the entire size of your level, and gMaxDist is half of gMazWid (only there to save on typing).

Your tiling objects will need the following code in the Udate function. transform.position=WhatEverMyScrtiptIsCalled.RepeatPos(transform.position);

Comment
Add comment · Show 1 · 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 hcho · Feb 01, 2011 at 08:15 AM 0
Share

Could you repost the script? It won't work as is?

avatar image
0

Answer by Rblain · Jan 31, 2011 at 12:48 PM

The issue may be that the slight difference defined by being less than or equal to 30 accumulates over time. Say you move over 30 by .5 units. When you reset it is off by .5 units. Perhaps find the actual over or undershoot when you reset the box and add or subtract the amount to take care of any offset occurring.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2D Parallax 1 Answer

How to make a rigidbody stay on a moving platform? 1 Answer

Make Character Controller a Box? 2 Answers

Need help with 3d Person Character Controller 0 Answers

Character Rotation 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