- Home /
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..!
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.
odd, but are you sure that u could make the z-position of the camera different from the quad..!?
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;
}
}
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..!
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.
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);
humm, the code mentioned is attached to the background (quad) not the camera??
Yes, sorry, I forgot a line. You want to move it with the camera, but offset it's texture the opposite way. See above.
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..!?/
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.
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];