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 CobOfTheCorn · Jan 24, 2018 at 11:03 PM · backgroundrepeating

A repeating background

I am making a scrolling background that repeats itself along the Y axis, I am sure it is probably just a typo of some sorts but the background is scrolling, but not re-positioning itself at all.

 using UnityEngine;
 using System.Collections;
 
 public class RepeatingBackground: MonoBehaviour
 {
     private BoxCollider2D backgroundCollider;
     private float backgroundVerticalLength;
 
     private void Awake()
     {
         backgroundCollider = GetComponent<BoxCollider2D>();
         backgroundVerticalLength = backgroundCollider.size.y; 
     }
     private void Update()
     {
        if (transform.position.y < -backgroundVerticalLength)
         {
             RepositionBackground(); 
         }
     }
              private void RepositionBackground()
     {
         Vector2 backgroundOffSet = new Vector2(0, backgroundVerticalLength * 2f);
         transform.position = (Vector2)transform.position + backgroundOffSet;
     }
 }

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

Answer by JonPQ · Jan 31, 2018 at 11:20 PM

First off... what moves the background? I can't see any code that add's to the position of the object to move it... are you sure you are not moving the camera and forgetting about the background ? IF you are moving the camera.... you'll need to subtract the cam.position from your background position, then check the Y on that offset, to see when to re-position it.

if you are already doing that in another script... then check this... ....are you sure your offsets are not going backwards instead of forwards... Check your 'IF' in update... try both > and < Or Just add a couple of break points and debug the code as it runs, line by line, you should soon see what it is doing.

Also transform.position is a Vector3, I'm not sure adding Vector2's to it is fully kosher. Slightly ambiguous what is getting added to what.

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 CobOfTheCorn · Jan 31, 2018 at 11:37 PM

I have a script that moves the background that is also attached to it. I have two background copies and I am trying to make it to where it takes one and places it in front of the other as the background scroll.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScrollingObject : MonoBehaviour {
 
     private Rigidbody2D rb2d;
 
     // Use this for initialization
     void Start ()
     {
         rb2d = GetComponent<Rigidbody2D>();
         rb2d.velocity = new Vector2(0,GameController.instance.scrollSpeed);
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (GameController.instance.gameOver == true)
         {
             rb2d.velocity = Vector2.zero;
         }
     }
 }
 
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

74 People are following this question.

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

Related Questions

How to script a repeating environment texture 3 Answers

Better way for a tile based system 0 Answers

turn based two player bord game 0 Answers

Background on a Quad 1 Answer

how do I create background image? 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