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
2
Question by Sami_Pal · Feb 08, 2017 at 07:10 PM · camera2dbackgroundendless runnerinfinite runner

How to make the background follow the camera

I'm making a 2D infinite runner game, so now I wanted to add a background for the game, so I made a quad and attach the image as texture with offset looping so it will show the repeating effect.

here's the script of the looping background wich works great.

 using UnityEngine;
 
 public class Background : MonoBehaviour {

     public float speed = 0.5f;
     void Update() {

         Vector2 offset = new Vector2(Time.time * speed, 0);
         GetComponent<Renderer>().material.mainTextureOffset = offset;
      }
 }
 

Now the problem, my camera is an orthographic and made follows the Player buy through code so the camera view won't jump when the Player jump, but I can't make the background (AKA quad) follows the camera..!?? because the camera moves to positive x endlessly but the background is still..!

the most obvious way is by make the Background a child to the camera, but once I do that the background transform value reset as the Camera and it stopped showing...!?

The camera z position is -20 with (0,0,0) scale and the background has z position of 10 with (35,20,0) scale ..!!

so How can I make the Background follow a moving camera endlessly without changing the original values of the z-position or the scale..!?

here's a video that i recored which concise the problem..

https://drive.google.com/file/d/0B6igOAXj7uD9c0o0My1GQmlyN2s/view?usp=sharing

If the Camera script is wanted, I'll add it..!

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 Bongmo · Feb 08, 2017 at 07:46 PM 0
Share

I added the quad as a child to the camera. Then I added your code to the quad with a default texture. When I now move the camera everything works. $$anonymous$$y camera is orthographic.

avatar image Sami_Pal Bongmo · Feb 09, 2017 at 01:46 PM 0
Share

odd, but are you sure that u could make the z-position of the camera different from the quad..!?

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by SBrooks75 · Feb 09, 2017 at 04:05 PM

If your wanting a parallax effect this one works great just attach this to your camera. And add your backgrounds in order or you can add a Background GameObject with all of you backgrounds as child objects. Make sure to set the z coordinates of your background object before running. i set mine to -14.

using UnityEngine; using System.Collections;

public class CameraParallax : MonoBehaviour {

 public Transform[] backgrounds;
 public float[] parallaxScales;
 public float smoothing;
 
 private Transform cam;
 
 private Vector3 previousCamPos;
 
 // Use this for initialization
 void Start () {
     
     cam = Camera.main.transform;
     
     previousCamPos = cam.position;
     
     parallaxScales = new float[backgrounds.Length];
     
     
     for (int i = 0; i < backgrounds.Length; i++) {
         
         parallaxScales[i] = backgrounds[i].position.z * -1;
         Debug.Log(parallaxScales[i]);
         
         
         
         
     }
     
     
     
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     
     for (int i = 0; i < backgrounds.Length; i++) {
         
         float parallax = (previousCamPos.x - cam.position.x) * parallaxScales[i];
         
         float backgroundTargetPosX = backgrounds[i].position.x + parallax;
         
         Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds[i].position.y, backgrounds[i].position.z);
         
         backgrounds[i].position = Vector3.Lerp (backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
         
         
     }
     
     previousCamPos = cam.position;
     
     
 }

}

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 Sami_Pal · Feb 10, 2017 at 05:23 PM 0
Share

I did that and it worked, but it's not following the camera'a FOV !?? I tried adjusting different Smoothing values but the background sometimes skip and some times too slow since the progression of the player speeds up with more distance crossed..!

avatar image Alucard-Belmont · Jan 29, 2019 at 07:49 AM 0
Share

SBrooks75 To me if your code worked, but my game in an infinite runner and I already try to make each background repeat infinitely but I can not find the form. Can you help me know that I should add to your code to make it repeat infinitely every background please.

avatar image
4

Answer by Rickasheye · Apr 20, 2018 at 08:16 AM

or you could parent the background to the camera

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
1

Answer by FortisVenaliter · Feb 08, 2017 at 08:09 PM

The easiest way would be to subtract the camera's location X from your offset x in your update code.

Something like:

 transform.position = new Vector3(camera.transform.x, transform.position.y, transform.position.z);
 Vector2 offset = new Vector2(Time.time * speed - camera.transform.x, 0);
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 Sami_Pal · Feb 09, 2017 at 01:44 PM 0
Share

humm, the code mentioned is attached to the background (quad) not the camera??

avatar image FortisVenaliter Sami_Pal · Feb 09, 2017 at 02:14 PM 0
Share

Yes, sorry, I forgot a line. You want to move it with the camera, but offset it's texture the opposite way. See above.

avatar image Sami_Pal FortisVenaliter · Feb 10, 2017 at 05:34 PM 0
Share

I did that but it didn't work !! but I understand the idea that u r trying do.. so I did what u do but not for the camera.. I made the background take the Player last x position and subtract it with the background x position then I add the difference to the background current x position.. and after some hours trying to accomplish that,... it finally worked :D Thanks a lot..!

But I notice something.. the reparteeing texture of the background isn't showing to the background much of movement.. so while I was playing and the player is running.., the background seems like static or the parallax effect isn't noticeable and when I die.. It feels like it's poping up and crystal clear since the player stooped moving ..!! did you get what I mean ?? is there a soultuon to this problem..!?/

avatar image
0

Answer by Bing-Bam-and-Boom · Apr 20, 2018 at 08:07 AM

I created this code myself to follow the camera x but here's the changed edition:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class folCam : MonoBehaviour {
 
     private float camerax;
     private float cameray;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void LateUpdate () {
         camerax = Camera.main.transform.position.x;
         cameray = Camera.main.transform.position.y;
         transform.position = new Vector3(camerax, cameray, transform.position.z);
     }
 }

also this is on the background not the camera.

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
0

Answer by CrizzieBabe · Feb 18, 2019 at 05:59 PM

I get this error with the code: IndexOutOfRangeException: Index was outside the bounds of the array.

The error is on this line: float parallax = (cam.position.x - previousCamPos.x) * parallaxedScales[i];

@SBrooks75

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

13 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

Related Questions

Change the background color attribute of a camera in C#? 2 Answers

Making a camera semi-orthographic 2 Answers

How can I stop my endless runners background sprites from falling out of sync? 1 Answer

Scrolling Background 1 Answer

Random obstacles spawn in 2D side endless runner game ? 2 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