• 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 vinhuman · Jul 22, 2014 at 09:43 PM · transform.position

If (transform.position.. is immediately snapping to position

I'm trying to create a continuously scrolling background in a runner sort of 2d game where the actual player model is only moving along the x axis and everything is moving past the player (creating the illusion the player is moving).

However, I am having trouble getting the background to repeat itself by snapping back to a new position. The transform.position.y of the game object I'm attaching this script to is at 0, but no matter what I make the requirement in the if statement my object immediately locks to the (x,15,z) coordinate. No scrolling is achieved (although it will easily scroll along the x axis in the float). Thanks.

 using UnityEngine;
 using System.Collections;
 
 public class BackgroundScroll : MonoBehaviour 
 {
     public Vector2 velocity = new Vector2(0, 15);
 
     void Update()
     {        
 
         if (transform.position.y > 84f);
         {
             transform.position = new Vector3(transform.position.x, 15f, transform.position.z);
         }
         rigidbody2D.velocity = velocity;
 
     }
 }
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 Epicwolf · Jul 22, 2014 at 10:53 PM

Well, I haven't worked with rigidbodies much, but, if this is applied to a background object, why is there a rigidbody attached? You could get rid of the rigidbody and just use

  transform.Translate()

to manually move the object instead. That should solve your problem. Also, you might want to change that 84f to 84.0f, you put the f after numbers with a decimal point in them (floats), not whole integers. Here is what I suggest doing:

 using UnityEngine;
 using System.Collections;
 
 public class BackgroundScroll : MonoBehaviour 
 {
     public float yspeed = 2.0f;
     public float maxdistance = 84.0f;
     float startingposition;

     void Start()
     {
         startingposition = transform.position.y;
     }

     void Update()
     {       
  
         if (transform.position.y > startingposition + maxdistance)
         {
             transform.position = new Vector3(transform.position.x, startingposition, transform.position.z);
         }
         else
         {
             transform.Translate(new Vector3(0.0f, yspeed * Time.deltaTime, 0.0f));
         }
     }
 }

the yspeed * Time.deltaTime will cause the object to move along the y axis at yspeed units per second. Also, you could move this object anywhere, and it will still only travel as far as maxdistance, and will reset itself to its starting position.

EDIT: Oops, I only made y axis movement because the only thing you are checking is the y position.. x and z axis movement wouldn't be to hard to add though.

Comment
Add comment · Show 3 · 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 vinhuman · Jul 23, 2014 at 04:48 PM 0
Share

Wow thanks that worked great! I've only been using Unity a few days so I suppose I'm prone to making some really stupid mistakes. The rigidbody was attached because it was an attempt at recreating a similar version of a tutorial script that fit a somewhat different purpose. At this point the y axis movement is all I need.

Just in case someone else reads this, there were two simple corrections I had to make to Epicwolf's script. The ';' at the end of line 18 had to be removed and 'starting position' in the same line had to be changed to 'startingposition'.

Thanks again.

avatar image vinhuman · Jul 24, 2014 at 02:08 AM 0
Share

Thanks. I am new to this site's layout.

avatar image Epicwolf · Jul 25, 2014 at 12:49 AM 0
Share

You're welcome! I'm glad to have helped. :) Sorry about the typos, I didn't notice them. The are now fixed in my answer.

avatar image
0

Answer by smallbit · Jul 24, 2014 at 10:10 AM

Look at this video about background scrolling from unity live training http://www.youtube.com/watch?v=GtNW9uD94uc

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with inheritance to show GUI 1 Answer

unwanted movements when camera follows object 0 Answers

Why can't I get the transform from an instantiated object? 1 Answer

Get an Object to move to a Position for a Fixed time 2 Answers

How to teleport the player to the location of an instantiated projectile? 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