
Unity Parallax Tutorial - How to infinite scrolling background
video description
Date: 2020-05-17
Comments and reviews: 9
Voter96
I modified this code a bit so the background is affected by the Y position too (useful if your player character wants to move up or down) using System.Collections; using System.Collections.Generic; using UnityEngine; public class Parralax : MonoBehaviour private float length, startpos, ypos; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startpos = transform.position.x; ypos = transform.position.y; length = GetComponent().bounds.size.x; // Update is called once per frame void Update() float temp = (cam.transform.position.x (1 - parallaxEffect)); float dist = (cam.transform.position.x parallaxEffect); float ydist = (cam.transform.position.y parallaxEffect); transform.position = new Vector3(startpos + dist, ypos + ydist, transform.position.z); if (temp > startpos + length) startpos += length; else if (temp < startpos - length) startpos -= length;
reply
I modified this code a bit so the background is affected by the Y position too (useful if your player character wants to move up or down) using System.Collections; using System.Collections.Generic; using UnityEngine; public class Parralax : MonoBehaviour private float length, startpos, ypos; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startpos = transform.position.x; ypos = transform.position.y; length = GetComponent().bounds.size.x; // Update is called once per frame void Update() float temp = (cam.transform.position.x (1 - parallaxEffect)); float dist = (cam.transform.position.x parallaxEffect); float ydist = (cam.transform.position.y parallaxEffect); transform.position = new Vector3(startpos + dist, ypos + ydist, transform.position.z); if (temp > startpos + length) startpos += length; else if (temp < startpos - length) startpos -= length;
reply
Mickplouffe
Hi! Thanks you for this quick and easy tutorial! I like the simplicity of the code, but I got a strange error/Behevior. If someone knows where I f-u cause everythings else the repeating, Works: First I checked up the code 20 times. I ensure you that it is EXACLY the same! At first I have made the effect. I've use ParallaxEffect with setting like 0.25. Everything is working wunderfully. BUT I added the code to make it infinite. I have place the Main Camera on ALL first sprites (like 2. But 2 (1) does not have the script_ When I push play the right section directly goes the the far left. When I move the camera, some of the sprite is blincking in the both positions. (Go far right, go far left, go far right... and so on) I have tried to set the camera size EXACTLY on the zone of the background. Still not fixed. I'm not using the same sprite as in the video. [Sorry for my really bad english.]
reply
Hi! Thanks you for this quick and easy tutorial! I like the simplicity of the code, but I got a strange error/Behevior. If someone knows where I f-u cause everythings else the repeating, Works: First I checked up the code 20 times. I ensure you that it is EXACLY the same! At first I have made the effect. I've use ParallaxEffect with setting like 0.25. Everything is working wunderfully. BUT I added the code to make it infinite. I have place the Main Camera on ALL first sprites (like 2. But 2 (1) does not have the script_ When I push play the right section directly goes the the far left. When I move the camera, some of the sprite is blincking in the both positions. (Go far right, go far left, go far right... and so on) I have tried to set the camera size EXACTLY on the zone of the background. Still not fixed. I'm not using the same sprite as in the video. [Sorry for my really bad english.]
reply
Istrate
for lazy bastards or for those who are trying to fix their code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class parallaxx : MonoBehaviour private float lenght, startpos; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startpos = transform.position.x; lenght = GetComponent().bounds.size.x; // Update is called once per frame void Update() float temp = (cam.transform.position.x (1 - parallaxEffect)); float dist = (cam.transform.position.x parallaxEffect); transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z); if (temp > startpos + lenght) startpos += lenght; else if (temp < startpos - lenght) startpos -= lenght; //I feel alone AF if we wanna chat +40751531428
reply
for lazy bastards or for those who are trying to fix their code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class parallaxx : MonoBehaviour private float lenght, startpos; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startpos = transform.position.x; lenght = GetComponent().bounds.size.x; // Update is called once per frame void Update() float temp = (cam.transform.position.x (1 - parallaxEffect)); float dist = (cam.transform.position.x parallaxEffect); transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z); if (temp > startpos + lenght) startpos += lenght; else if (temp < startpos - lenght) startpos -= lenght; //I feel alone AF if we wanna chat +40751531428
reply
Parry
For those having trouble with implementing a vertical solution: create a new script, e.g Yparallax and follow a similar approach public class Yparralax : MonoBehaviour private float length, startpos; public GameObject cam; public float parallaxEffect; private void Start() //make sure these are . y s startpos = transform.position.y; length = GetComponent().bounds.size.y; private void Update() float temp = (cam.transform.position.y (1 - parallaxEffect)); float dist = (cam.transform.position.y parallaxEffect); //make sure the x poition stays the same, and the y is changine with dist transform.position = new Vector3(transform.position.x, startpos + dist, transform.position.z); //no need for repeating hope this helps :)
reply
For those having trouble with implementing a vertical solution: create a new script, e.g Yparallax and follow a similar approach public class Yparralax : MonoBehaviour private float length, startpos; public GameObject cam; public float parallaxEffect; private void Start() //make sure these are . y s startpos = transform.position.y; length = GetComponent().bounds.size.y; private void Update() float temp = (cam.transform.position.y (1 - parallaxEffect)); float dist = (cam.transform.position.y parallaxEffect); //make sure the x poition stays the same, and the y is changine with dist transform.position = new Vector3(transform.position.x, startpos + dist, transform.position.z); //no need for repeating hope this helps :)
reply
кирьещька9
Hi! Pls help me! errors: CS1525: unexpected symbol bounds CS1525: unexpected symbol transform CS1525: unexpected symbol . here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class parellex : MonoBehaviour private float length, startpos; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startpos = transform.position.x; length = GetComponent()bounds.size.x; // Update is called once per frame void FixedUpdate() float dist = (cam.transform.position.x parallaxEffect) transform.position = new Vector3(startpos + dist, transform.position.y transform.poition.z) Pleas help!!!
reply
Hi! Pls help me! errors: CS1525: unexpected symbol bounds CS1525: unexpected symbol transform CS1525: unexpected symbol . here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class parellex : MonoBehaviour private float length, startpos; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startpos = transform.position.x; length = GetComponent()bounds.size.x; // Update is called once per frame void FixedUpdate() float dist = (cam.transform.position.x parallaxEffect) transform.position = new Vector3(startpos + dist, transform.position.y transform.poition.z) Pleas help!!!
reply
Semi
I know this is not the most recent video, still I would like to point out one thing You've missed: What if camera is following player when he jumps? With the current script, the whole BG would jump with the player which wouldn't look too nice. Everyone who use their brain or know some basics would find the solution in an instant, but here's some tip for complete beginners: You need to add another float for Y axis and define its parameter, then put it in line: transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z); replacing ,,transform.position.y . That way Your BG will stay in place at Y axis. The video is great anyway, cheers to that!
reply
I know this is not the most recent video, still I would like to point out one thing You've missed: What if camera is following player when he jumps? With the current script, the whole BG would jump with the player which wouldn't look too nice. Everyone who use their brain or know some basics would find the solution in an instant, but here's some tip for complete beginners: You need to add another float for Y axis and define its parameter, then put it in line: transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z); replacing ,,transform.position.y . That way Your BG will stay in place at Y axis. The video is great anyway, cheers to that!
reply
Squilliam24
for the lazy people using System.Collections; using System.Collections.Generic; using UnityEngine; public class Parallax : MonoBehaviour private float length, startpos; public GameObject cam; public float parallexEffect; void Start() startpos = transform.position.x; length = GetComponent().bounds.size.x; void Update() float temp = (cam.transform.position.x (1 - parallexEffect)); float dist = (cam.transform.position.x parallexEffect); transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z); if (temp > startpos + length) startpos += length; else if (temp < startpos - length) startpos -= length;
reply
for the lazy people using System.Collections; using System.Collections.Generic; using UnityEngine; public class Parallax : MonoBehaviour private float length, startpos; public GameObject cam; public float parallexEffect; void Start() startpos = transform.position.x; length = GetComponent().bounds.size.x; void Update() float temp = (cam.transform.position.x (1 - parallexEffect)); float dist = (cam.transform.position.x parallexEffect); transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z); if (temp > startpos + length) startpos += length; else if (temp < startpos - length) startpos -= length;
reply
Haioner
For who want the X and Y parallax private float lenght, startposX; private float height, startposY; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startposX = transform.position.x; lenght = GetComponent().bounds.size.x; startposY = transform.position.y; height = GetComponent().bounds.size.y; private void FixedUpdate() float dist = (cam.transform.position.x parallaxEffect); float distY = (cam.transform.position.y parallaxEffect); transform.position = new Vector3(startposX + dist, startposY + distY, transform.position.z);
reply
For who want the X and Y parallax private float lenght, startposX; private float height, startposY; public GameObject cam; public float parallaxEffect; // Start is called before the first frame update void Start() startposX = transform.position.x; lenght = GetComponent().bounds.size.x; startposY = transform.position.y; height = GetComponent().bounds.size.y; private void FixedUpdate() float dist = (cam.transform.position.x parallaxEffect); float distY = (cam.transform.position.y parallaxEffect); transform.position = new Vector3(startposX + dist, startposY + distY, transform.position.z);
reply
Wajid
Dani I have a slight problem with my parallax effects. The background that is on No. 2, 3, 4 does not repeat with the position of the camera on that specific background. My first background that in on No. 0 is fine when I go to the end of the frame o it removes the previous frame and past it next to the current frame. But it happens to all the backgrounds. i.e I am at the end of 0 backgrounds but in the middle of No. 2, 3 backgrounds Unity Removes my 2,3,4 backgrounds as well and past to the next frame. Any suggestion Please.
reply
Dani I have a slight problem with my parallax effects. The background that is on No. 2, 3, 4 does not repeat with the position of the camera on that specific background. My first background that in on No. 0 is fine when I go to the end of the frame o it removes the previous frame and past it next to the current frame. But it happens to all the backgrounds. i.e I am at the end of 0 backgrounds but in the middle of No. 2, 3 backgrounds Unity Removes my 2,3,4 backgrounds as well and past to the next frame. Any suggestion Please.
reply
Add a review, comment
Other channel videos















