Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 DuperSoup · Mar 21 at 11:36 PM · unity 2dcanvasbackgroundrigidbody 2d

Do Canvas Objects behave similarly under the effect of Rigidbody2D when it comes to position checking?

Hello. I am trying to make a scrolling background for a replica of Flappy Bird. I have a Canvas Object with the shown settings:

alt text

The Canvas is a parent to two background child image objects with the following code. The idea was to make it such that when the image moved off of the screen by a value equal to its width, it is then teleported back to the right side of the screen. This way two images can be the scrolling background as long as they keep teleporting back to the right side repeatedly. When this script is run, the objects do not teleport at all. Are Canvas/image objects not affected by rigidbody2D transforms in the same way normal game objects are? Or is there something that needs to be changed about the Canvas settings? When I check the scene view when the game is running, the background image objects just keep moving to the left indefinitely far beyond their width (in this case 900). There are videos that show how to do scrolling background, but this approach seemed the simplest and I don't see why it's not working.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class BackgroundScroll : MonoBehaviour
 {
     private Rigidbody2D rigidBody;
     private RectTransform background;
     private float width;
     private float speed = -3;   
 
     // Start is called before the first frame update
     void Start()
     {
         
         rigidBody = GetComponent<Rigidbody2D>();        
         rigidBody.velocity = new Vector2(speed, 0);
 
         background = GetComponent<RectTransform>();
 
         // Get image width from RectTransform component
         width = background.rect.width;
         // Display width for troubleshooting
         Debug.Log(width);
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         // if game object was moved further than its width, then teleport
         // back to the right side based on width
         if (gameObject.transform.position.x <= -width)
         {
             gameObject.transform.position = new Vector2(width, 0);
             Debug.Log("Teleported");
         }
     }
 }


bg-canvas-settings-32122.png (31.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How to make the camera follow the character without dragging the background along with it in screen space camera render mode? 1 Answer

Unity UI - layout fixed number of buttons 1 Answer

What aspect ratio should the slider background use 0 Answers

Unity 2d - Rigidbody rotation constraints? 4 Answers

can i insert an image to be the background of my 2d game if so how ? 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