Unity Parallax Tutorial - How to infinite scrolling background

Поділитися
Вставка
  • Опубліковано 10 вер 2024

КОМЕНТАРІ • 952

  • @Danidev
    @Danidev  5 років тому +655

    **CORRECTION***
    I have no idea why I put the code in FIxedUpdate. I'm a dummy. USE Update() INSTEAD!!!
    **WHY DO I PUT THE BACKGROUND ON THE CAMERA??**
    To answer this commonly asked question:
    *Usually* your camera follows the player. So when the player moves, the camera moves. This method is perfect for that, since the background only moves when the camera moves. Meaning the background moved when the plsyer moves.
    You don't have to put the background on the camera, but you would have to reverse my script to get it working outside of the camera.
    BUT if you want a moving background where the camera stands still, this implementation is not going to work. Luckily for you, that's way easier, and you just need to repeat the backgrounds at different speeds, using something like Mathf.repeat
    If you have any questions regarding this, the easiest way to ask me is in my discord. You can ask me here on UA-cam too, just don't expect an instant reply.
    Thanks for watching!

    • @aaronswords404
      @aaronswords404 5 років тому +14

      About your correction. I was having stuttering in my game (only after building) by using Update(). Using FixedUpdate() actually eliminated the stuttering so I would recommend it.

    • @Sniper0502
      @Sniper0502 5 років тому +3

      @@aaronswords404 Fixed update is usually used for physics based needs since it updates either never, once, or multiples times a frame depending on the amount of physics changes and frame rate at that specific time

    • @TheDukeOfReason
      @TheDukeOfReason 5 років тому +6

      funny enough on Android I had to put it into LateUpdate to get rid of the stuttering. Go figure

    • @alexbarber2497
      @alexbarber2497 4 роки тому +2

      I did the folowing correction;
      //lenght = GetComponent().bounds.size.x;
      lenght = GetComponentInChildren().bounds.size.x;

    • @darksavior1187
      @darksavior1187 4 роки тому +1

      @@alexbarber2497 two things, first length is spelled wrong. Second, did you add an additional line, or change the first into the second?

  • @itsapocalypse8239
    @itsapocalypse8239 4 роки тому +384

    We need more tutorials from Dani. He's pretty epic.

    • @kimjongun3890
      @kimjongun3890 4 роки тому

      yep definitely maybe on Dani3

    • @equation1321
      @equation1321 4 роки тому +1

      @@kimjongun3890 Dani tutorials

    • @GasimovTV
      @GasimovTV 4 роки тому +1

      @@kimjongun3890 LMAO it is danistutorials. I guess you are not a true boner.

    • @gelis07
      @gelis07 4 роки тому

      epik*

    • @equation1321
      @equation1321 4 роки тому

      @@GasimovTV thats what i said :(

  • @geri4367
    @geri4367 5 років тому +301

    Way more simple that other implemetations I've seen. Good job :D

    • @hematogen50g
      @hematogen50g 4 роки тому +2

      Simple - yes. Optimal - No.

    • @jameswellington7745
      @jameswellington7745 4 роки тому +2

      hematogen50g
      Fair enough, but for those new to the C# scene such as myself and aren’t working on large scale projects, this is great.

    • @couchoclocknews
      @couchoclocknews 4 роки тому

      Aren't you a vr dev youtuber?

    • @ethicalrevolution3294
      @ethicalrevolution3294 3 роки тому +4

      @@hematogen50g Can you elaborate some details what you mean.

    • @sengoku8497
      @sengoku8497 3 роки тому +1

      @@ethicalrevolution3294 well, here are some points I might guess:
      - 5 textures (sprites) duplicated 3 times, 15 entities in total for background
      - all this textures are being transformed on CPU (via C# script)
      - if the game lags, the difference between temp and |startpos +- length| might be too big, causing a weird visual effect
      Much more efficient solution would be just by changing offset of the sprite-texture in shader code per frame, executed on GPU

  • @berkcan3475
    @berkcan3475 5 років тому +38

    thank you for video, I'm gonna give cool tip for fast editing who loves shortcuts, press Ctrl+3 to focus on Hierarchy and then press Ctrl+4 to focus on inspector

    • @Danidev
      @Danidev  5 років тому +21

      Oh, thanks!

    • @rystal
      @rystal 2 роки тому

      @@Danidev Lol

  • @mesenes77
    @mesenes77 4 роки тому +169

    Brackeys stopped making tutorials Dani, It's your time.

    • @user-tw9ck7yc9i
      @user-tw9ck7yc9i 3 роки тому +10

      Yeah, it's so sad that Brackeys won't make videos any more, he was the greatest youtuber making Unity tutors :(

    • @gamedevperson
      @gamedevperson 3 роки тому

      @@user-tw9ck7yc9i rip brackeys :( i watched him for 2 years

    • @gamedevperson
      @gamedevperson 3 роки тому +1

      @@ahsanzizan no he left

    • @bhawal5307
      @bhawal5307 3 роки тому

      @@user-tw9ck7yc9i i learnt basic of c# from his yt vdos
      no more tutorials though rip

    • @clastergaming6394
      @clastergaming6394 3 роки тому +3

      too bad dani turned into a meme channel with videos where code parts are skipped and stupid milk and other memes repeated

  • @adriansantos9316
    @adriansantos9316 4 роки тому +129

    Your code is now my property.

  • @vehemyr6332
    @vehemyr6332 2 роки тому +21

    at 07:00, this code doesn't work in my build.
    if (temp > startpos + length) startpos += length;
    else if (temp < startpos - length) startpos -= length;
    The background change is 1 length too short, a quick fix is to multiply the length by 2:
    if (temp > startpos + length) startpos += length * 2;
    else if (temp < startpos - length) startpos -= length * 2;
    Don't know if anyone else is experiencing the same problem I did, but if you did here's the solution. Oh and great vid btw.

  • @fypo
    @fypo 4 роки тому +17

    Well i didn't expect to find Dani from 1 year ago while searching this!

  • @squilliam2490
    @squilliam2490 4 роки тому +205

    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;
    }
    }

    • @mcwhite5925
      @mcwhite5925 4 роки тому +2

      thnx man xD

    • @warrior6633
      @warrior6633 4 роки тому +2

      Sports Mania same.

    • @ThatOneLadyOverHere
      @ThatOneLadyOverHere 4 роки тому +8

      @Sports Mania With practice we will start understanding. There's a reason they refer to it as a language, and the best way to learn a language is through immersion.

    • @bendent7261
      @bendent7261 4 роки тому +5

      i dint notice for the lazy pepole bit so i couldnt workout why it wasnt working

    • @rohitchalak6379
      @rohitchalak6379 4 роки тому +1

      Thanx!😎😎🤩

  • @RylinthAnderfel
    @RylinthAnderfel 4 роки тому +16

    Really appreciate this tutorial. Pretty easy to follow, straight to the point, simple code, and well presented. Helped me build an awesome parallax effect for a class project!

  • @PorkandBeans
    @PorkandBeans 4 роки тому +84

    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;
    }
    }

    • @pabli.inkorea
      @pabli.inkorea 4 роки тому +1

      i knew someone would help me with that! thank you a lot

    • @sebastiank686
      @sebastiank686 3 роки тому +1

      you are the mvp

    • @karansh491
      @karansh491 3 роки тому +5

      Haha nerd go brrrr

    • @OdinGamingMC
      @OdinGamingMC 3 роки тому

      Thank you soooo much!

    • @faus585
      @faus585 3 роки тому

      Thank you so much!!

  • @andreystarenky9780
    @andreystarenky9780 4 роки тому +7

    Excellent tutorial thank you so much!!
    For anyone trying to figure out how to get the background to not flip/invert if you're using a player going back and forth, move the camera outside of the player and set camera's position to player.

  • @semirukiya5308
    @semirukiya5308 4 роки тому +6

    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!

    • @christopherthomas7460
      @christopherthomas7460 4 роки тому

      Could you give an example of this

    • @semirukiya5308
      @semirukiya5308 4 роки тому +4

      @@christopherthomas7460 Of course. Well, it actually highly depends on if You want the BG's position to change from time to time or not. In my example You don't want to do that, but the difference in code wouldn't be that big anyway. You could also make it adjustable through the inspector, but I will just use the solid amount as private.
      In the line You define Your parameters: private float length, startpos;
      You want to add another one. Let's call it posY, so the line would look like this: private float length, startpos, posY = 9.32f;
      posY represents the Y position of Your background - take a note I'm not talking about the Backgrounds container, but about one of Backgrounds sub-containers, so taking as an example from the video it would be any of 0 - 4 containers in hierarchy. I did use 9.32f but it is just an example. Then in the Update method, in this line:
      transform.position = new Vector3(startpos + dist, transform.position.y, transform.position.z);
      You want to replace transform.position.y with Your posY, so it would look like this:
      transform.position = new Vector3(startpos + dist, posY, transform.position.z);
      And that's it. I'm pretty much sure there are other ways to achieve that, but this one is the simplest one that came to my mind. As a summary for those who would still have a problem with understanding how it works: The first version of the line (from the video) will update Y axis of the background based on the Y axis of the camera. What You've changed here is that You said to the program that You want the BG to stay at the solid position You want, so in this case at Y = 9.32f.

    • @christopherthomas7460
      @christopherthomas7460 4 роки тому

      @@semirukiya5308 Thank you. That makes a lot of sense it helped a lot 😊

    • @semirukiya5308
      @semirukiya5308 4 роки тому

      @@christopherthomas7460 Glad I could help :)

    • @mtnvr6545
      @mtnvr6545 3 роки тому

      Great reply! I was having an issue with this exact thing. Is it as simple as just removing the "transform.position.y" in the script? Thanks for any info

  • @noodledoodle6029
    @noodledoodle6029 5 років тому +5

    Omg! Yesterday I thought I should request you to make a tutorial on parallax, turns out you've already made one! This is by far the *best* one I've found anywhere. Thank you :D

    • @Danidev
      @Danidev  5 років тому +1

      Haha cheers, glad you like it!

  • @red.williams18
    @red.williams18 Рік тому +4

    To people viewing in the future, if the ends of your background asset don't match up (as described in the start of this video), a possible solution could be to import the same background twice, where the second background is flipped horizontally. This will ensure that either end of the background meets another at the exact same height.

    • @LAKXx
      @LAKXx Рік тому

      smart nice one

  • @ayebeesee
    @ayebeesee 4 роки тому +6

    I love this implementation, it is so much simpler than other ones out there. Thank you so much for sharing!

  • @UrFriendXD
    @UrFriendXD 3 роки тому +19

    A few things to help anyone who has these problems:
    If you're seeing the edges of the backgrounds when reaching the edge (it means your backgrounds are too short) replace the respecting code with this:
    if (temp > startpos + (length - offset))
    {
    startpos += length;
    }
    else if (temp < startpos - (length - offset))
    {
    startpos -= length;
    }
    This will make it so it moves earlier than needs too. You'll also need a public float of offset and just fine-tune it to your needs.
    If there's small jumping or the size of the children are not all the same (or you're lazy to find the correct X positions for each), have this script attached to each parallax child image (the dupes) and have one of each set to be marked isRight. This will automatically find the size of the image and offset them in the right X pos.
    public class ParallaxChild : MonoBehaviour
    {
    [SerializeField] private bool isRight;
    // Start is called before the first frame update
    void OnValidate()
    {
    var length = GetComponent().bounds.size.x;
    if (!isRight)
    {
    length *= -1;
    }
    transform.localPosition = new Vector3( length, 0, 0);
    }
    // Update is called once per frame
    void Update()
    {

    }
    Good luck with your Game Dev endeavors!

  • @kingdoom1906
    @kingdoom1906 Рік тому

    It’s been 4 years for publishing this video from dani and here we are , man this is super good tutorial thank you helped me with this stupid thing i was struggle with

  • @roboking1020
    @roboking1020 4 роки тому +6

    If anyone is using Cinemachine and is having a glitch where the background sprite doesn't properly loop when you reach the end, try changing
    length = GetComponent().bounds.size.x; in the start method to length = GetComponent().size.x;

  • @JonasTyroller
    @JonasTyroller 5 років тому +6

    Very nice. I like the colors as well! Looks pretty cool.

    • @Jackgalaga
      @Jackgalaga 3 роки тому

      first reply b4 this blows up

    • @altreedy1649
      @altreedy1649 3 роки тому

      @@Jackgalaga Nothing happened

    • @Jackgalaga
      @Jackgalaga 3 роки тому

      @@altreedy1649 shut up it will

  • @ereboss6348
    @ereboss6348 2 роки тому +5

    When I reach the end of the sprite, the sprite just does a weird jump and does not move smoothly. Does anyone else have this problem too?

  • @MohamadMohamad005
    @MohamadMohamad005 3 роки тому +1

    wow i didnt realize dani was making tutorials...!

  • @karol_k
    @karol_k 5 років тому +10

    Idea: you can make many variations of a background tile, and as long as its seamless with each one, you can make the background have a little bit of random variation

  • @alekseykustov9574
    @alekseykustov9574 2 роки тому +1

    Это просто потрясающе! Искал несколько дней как сделать паралакс эффект везде предлагали странные и громозкие способы , а тут пару строчек кода и все идеально работает! Спасибо!!!!!!!!!!!

  • @yoctometric
    @yoctometric 5 років тому +6

    This is a good strait forward tutorial. The game I'm working on right now could use a background, so now that I have the code all I need to figure out is the art! Thanks

    • @Danidev
      @Danidev  5 років тому +4

      Awesome! Good luck with that art, and thanks for watching! :)

  • @gfulpakwuli3799
    @gfulpakwuli3799 3 роки тому

    This the most simple yet most effective parallax script I've ever encountered. Really good job! You got one new subscriber.

  • @idoblenderstuffs
    @idoblenderstuffs Рік тому +3

    im just legit trying to learn unity here and the first result leads me back to you.
    stop being dead, its cringe.

  • @Alriightyman
    @Alriightyman 3 роки тому +25

    If you'd like part of the BG to auto-scroll use this: (NOTE: paralaxEffect should be something like 0.01 in the editor)
    using UnityEngine;
    public class Parallax : MonoBehaviour
    {
    public GameObject cam;
    public float parallaxEffect;
    public bool autoScroll = false;
    private float length, startpos;
    // 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 Update()
    {
    float temp = cam.transform.position.x * (1 - parallaxEffect);
    float distance = (cam.transform.position.x * parallaxEffect);
    float desiredXPos = startpos + distance;
    if(autoScroll)
    {
    // this will push bg to the left
    desiredXPos = transform.position.x - parallaxEffect;
    }
    transform.position = new Vector2(desiredXPos, transform.position.y);
    if (temp > startpos + length)
    {
    startpos += length;
    }
    else if(temp > startpos - length)
    {
    startpos -= length;
    }
    }
    }

    • @christianpaulb.basbas4569
      @christianpaulb.basbas4569 Рік тому +2

      Thanks

    • @kosuken
      @kosuken Рік тому

      but what if it was better,
      using UnityEngine;
      public class Parallax : MonoBehaviour
      {
      public GameObject cam;
      public float parallaxEffectX, parallaxEffectY;
      public bool autoScrollX = false, autoScrollY = false;
      private float lengthX, startposX;
      private float lengthY, startposY;
      // Start is called before the first frame update
      void Start()
      {
      startposX = transform.position.x;
      lengthX = GetComponent().bounds.size.x;
      startposY = transform.position.y;
      lengthY = GetComponent().bounds.size.y;
      }
      // Update is called once per frame
      void Update()
      {
      float tempX = cam.transform.position.x * (1 - parallaxEffectX);
      float distanceX = (cam.transform.position.x * parallaxEffectX);
      float desiredXPos = startposX + distanceX;
      float tempY = cam.transform.position.y * (1 - parallaxEffectY);
      float distanceY = (cam.transform.position.y * parallaxEffectY);
      float desiredYPos = startposY + distanceY;
      if(autoScrollX)
      {
      // this will push the object to the left
      desiredXPos = transform.position.x - parallaxEffectX;
      }
      if(autoScrollY)
      {
      // this will push the object down
      desiredYPos = transform.position.y - parallaxEffectY;
      }
      transform.position = new Vector2(desiredXPos, desiredYPos);
      if (tempX > startposX + lengthX)
      {
      startposX += lengthX;
      }
      else if(tempX > startposX - lengthX)
      {
      startposX -= lengthX;
      }
      if (tempY > startposY + lengthY)
      {
      startposY += lengthY;
      }
      else if(tempY > startposY - lengthY)
      {
      startposY -= lengthY;
      }
      }
      }
      this works for x and y

    • @kosuken
      @kosuken Рік тому +2

      yeaaaa my script doesent work, like always

    • @rianahazra9945
      @rianahazra9945 Рік тому

      TYSMMMM

    • @Hypnotic-thefirst
      @Hypnotic-thefirst Рік тому

      Hipoty hopity your code is now my property

  • @josefsilvar4872
    @josefsilvar4872 3 роки тому +3

    @Dani, please start making more tutorials like that again! It'd be really helpful because you explain it really great!

  • @sexkotik
    @sexkotik 7 місяців тому +1

    still useful after years ago, thanks 🔥

  • @NiclasGleesborg0
    @NiclasGleesborg0 5 років тому +17

    Nice tutorial and great explanation. Very understandable :)

  • @anupamchand3690
    @anupamchand3690 2 роки тому

    Such an easy way to get a real cool effect which I've always seen and admired in some of the games I've played. Thanks!

  • @danielvillalba4457
    @danielvillalba4457 4 роки тому +5

    You rock man, amazing video, I thought that might be some issue with Cinemachine but it actually worked perfect, thanks a lot!

  • @computercrafty9090
    @computercrafty9090 3 роки тому +1

    This helped me very much. Thanks Dani.

  • @ethann3725
    @ethann3725 2 роки тому +7

    If anyone has issues with their backgrounds moving around weirdly and not repeating properly, make sure only the parent backgrounds have the script, not the children.

    • @squeeziejetadore3800
      @squeeziejetadore3800 Рік тому +4

      went trough hell to log into my account here at school, just to tell you THANK YOU! omg
      I was struggling for so many hours and it was because of this... Anyways thank u fr fr

    • @animationmaster_4
      @animationmaster_4 8 місяців тому

      And you come to save me time trying to figure it out myself. Thanks.

  • @aresanstudio8335
    @aresanstudio8335 3 роки тому +2

    Thanks for sharing this great tutorial. I will need it for my 2D Game!

  • @hachasansavan2612
    @hachasansavan2612 4 роки тому +12

    hi guys. ı'm very new at unity. ı couldn't understand what he means here:
    float temp = (Cam.transform.position.x) * (1 - parallaxEfect);
    float dist = (Cam.transform.position.x * parallaxEfect);
    can you help?

    • @sarahclark4159
      @sarahclark4159 4 роки тому +2

      I don’t either, but I upvoted, so hopefully someone gets you an answer soon. If you figure it out I’d love to know!

    • @hotpockets898
      @hotpockets898 4 роки тому +1

      ok, so im pretty new to unity(so im probably wrong), but i do believe i can understand this, float temp is how long until it repeats, and float dist is telling the parallax efect to move, and once it gets one camera distance away its going to reset

    • @fuleswaripal9536
      @fuleswaripal9536 4 роки тому

      Float is a datatype where you can insert integer variabal with decimals also . Cam is the short form of camera and cam is a function which specifies the position and direction of the camera we use x for storing position value . There are vector 3 where we can use x y and z . In vector 2 we can store value of x and y .'transform' is basically a function you need to write before storing value x or y or z . You first need to learn coding unity c# before doing this there are a lot of videos on unity c# coding watch it they will surely help you👍

    • @hachasansavan2612
      @hachasansavan2612 4 роки тому

      @@fuleswaripal9536 thank you for your comment FulesWari. I'm not new at codding ı already know what you explain above. ı just couldn't understant the logic behind that part .... 1- parallaxEfect);

  • @neotrixsine7846
    @neotrixsine7846 2 роки тому

    You always motivated me with your devlogs and now you are helping me with your tutorials!

  • @diliupg
    @diliupg 5 років тому +11

    excellent tutorial. I implemented this in my game. Thank you. I will credit you. :)

  • @RandomDude-bo1lg
    @RandomDude-bo1lg 4 роки тому

    Don't understand anything but simply following the instructions is doable. Thank you very much

  • @amajesticmango
    @amajesticmango 3 роки тому +3

    bruh i watched this a few days ago looking for a tutorial and found this but didnt notice it was dani lmao

  • @abovethemist1412
    @abovethemist1412 3 роки тому +1

    Dani Before: chill
    Dani Now: *_milk_*

  • @vishnuj2717
    @vishnuj2717 4 роки тому +19

    Before writing the last if else code my scene working fine but after that,
    When i hit the play button all the layers started blinking ???
    What to do any suggestions?? 😕😕

    • @rorychang9965
      @rorychang9965 4 роки тому +3

      Idk but I liked the comment so more people see ur comment

    • @bradmcdaniel3004
      @bradmcdaniel3004 4 роки тому +2

      Same!

    • @vishnuj2717
      @vishnuj2717 4 роки тому +4

      @@bradmcdaniel3004 I found the mistake 😁
      Actually my both if conditions are cheaking greater then that's why they all are blinking. You can check yours also👍

    • @bradmcdaniel3004
      @bradmcdaniel3004 4 роки тому +2

      Vishnu J ahhh ok hopefully my issue just as minor 💀

    • @pabli.inkorea
      @pabli.inkorea 4 роки тому

      I had problems too, is not the best solution but i just made the bg as big as my level and didnt use the infinite copy part

  • @Froyenoff
    @Froyenoff 4 роки тому +2

    i just realised how good of a programmer dani is

  • @victorzhang6859
    @victorzhang6859 3 роки тому +6

    the biggest question is "WHICH WINDOWS VERSION DO U USE?!?!?"

  • @jessejames03
    @jessejames03 3 роки тому

    This was just what i needed, u made my day better Dani👍

  • @abelnicolaebaritone
    @abelnicolaebaritone 4 роки тому +7

    you're the best. Thank for the help.

  • @invertedRR
    @invertedRR Місяць тому +1

    Time to use this in my super mario world clone which me and the rest of inverted productions is working on.

  • @Angelongamer
    @Angelongamer 3 роки тому +4

    the code :D :
    using UnityEngine;
    public class Parallax : 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 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 + length) startpos += length;
    else if (temp < startpos - length) startpos -= length;
    }
    }

  • @FirstnameLastname-jo3zd
    @FirstnameLastname-jo3zd 2 роки тому +1

    wow thats really cool 👍

  • @Lajlaj
    @Lajlaj 3 роки тому +3

    This works, but my background images are shaking alot. Anyone with a solution?

  • @JOMU_MAKU
    @JOMU_MAKU 4 роки тому

    thx Dani from the past i needed this. also your future self motivate me to do game development

  • @akshatdubey4343
    @akshatdubey4343 3 роки тому +9

    its weird to see him not cracking jokes

  • @deano1
    @deano1 3 роки тому

    i really like this tuff and it actually has helped me with a 2d game im working on. he really should do more on either this channel or danitutorials.

  • @Flurette
    @Flurette 3 роки тому +5

    What is Karlson?

    • @dittodolphino
      @dittodolphino 4 місяці тому +1

      oh Karlsson IS JUST A LIITLW GME OM WORKING WISHLIST NOW BONERS

    • @wolfie-games
      @wolfie-games 4 місяці тому +1

      ​@@dittodolphinoI was gonna say the same thing rip Dani

  • @elvenfebriana1102
    @elvenfebriana1102 3 роки тому +2

    Maybe someone will see my comment and have a tiny bity jitter on the background just really a little bit jitter... Make sure the edge of the Background really connect to it's edge, you can zoom in and try 1 by 1 exact number so it can connect to it's really edge.
    also i use public void FixedUpdate() and it does work really smooth, i only put the background only to the camera, but not with the Ground that Player will step on it,
    don't forget to thanks to @Dani for the tutorial, it really works!

  • @coentertainer
    @coentertainer Рік тому +4

    What would be the easiest way to add variety? Like instead of the same foreground element every time, it's one of a possible 5 that appear.

    • @Broockle
      @Broockle Рік тому

      different pieces would be easy, you'd just have to place them all in order like in the video.
      If you want them to be random tho you'd prbly have to make a random range and then instantiate them in location... or w8 better idea..
      Maybe try replacing the pictures with empty game objects that move in this parallax fashion. Then every time you force one to move you also make it switch image... something like that.

  • @cmd5789
    @cmd5789 3 роки тому

    Stunning. Flawless. Perfect tutorial.

  • @marbldidnttry
    @marbldidnttry 4 роки тому +3

    When my (example: mountains) move from the back to the front they do not move back as I go left

  • @Skikopl
    @Skikopl 3 роки тому

    Thank you Dani, still 1 year later useful.

  • @PumpkinEaterSixtyNine
    @PumpkinEaterSixtyNine 4 роки тому +7

    Where can I get the sprites he's using? Cant find them on his dc

    • @mounamannou648
      @mounamannou648 4 роки тому

      discord

    • @mayankbhaisora2699
      @mayankbhaisora2699 3 роки тому +1

      I am also looking for the same. Can anyone please provide links to the assets. Can't find on Discord.

  • @b1tfl1p
    @b1tfl1p 3 роки тому

    bruh this legend is helping me with tutorials now?

  • @manuelsilva4931
    @manuelsilva4931 5 років тому +4

    It's really nice. worked with me, but when my Character go backwards the Parallax bugs and the sprites doesn't duplicate at real time. Need help

    • @m00dude
      @m00dude 4 роки тому +2

      Try this:
      else if(temp < startpos) { objPosition -= length; }

  • @mr.dickvonleonardojr.6738
    @mr.dickvonleonardojr.6738 2 роки тому +1

    Dani before insanity

  • @emlel1948
    @emlel1948 3 роки тому +3

    Lmao I had no idea that Dani did unity tutorials
    But still thank you

  • @VoidShadow66
    @VoidShadow66 11 місяців тому

    Epic script for parallax effect. Dani's best!

  • @Ox-Plays
    @Ox-Plays 5 років тому +3

    What image size should I use for the background? As big as possible so it can be shrunk down with no loss of quality?
    I'm making a 2D side scroller, but I'd like for it to look good with smooth lines and stuff even on my 4k display at full screen.

    • @Danidev
      @Danidev  5 років тому +3

      Yeah then you should go for a 4k image size. It's better to scale down for sure, just make sure it's no too big. That could hurt your performance.

  • @TofiOfficial
    @TofiOfficial 3 роки тому

    Thanks Dani for the tutorial!

  • @lazyemperor5182
    @lazyemperor5182 4 роки тому +8

    Who didn't find this before but got recommended now

    • @ItsCoderDan
      @ItsCoderDan 4 роки тому +1

      i was searching for it :p, although i had no idea Dani made a totorial on this

    • @user-tf9lo4fi4m
      @user-tf9lo4fi4m 4 роки тому

      ​@@ItsCoderDan Hey guys, here is much simpler way to make same stuff in unity, but be careful it is on russian, lol. check this out: ua-cam.com/video/t-2DP4UUC5E/v-deo.html

  • @jordanfaydherbe7394
    @jordanfaydherbe7394 2 роки тому

    Perfect tutorial. Simple, easy to use, can be applied in many ways. 👍

  • @xentox5016
    @xentox5016 4 роки тому +3

    Nice Tutorial!
    But for some reason, if i walk to the left , it works perfect, but if i walk to the right they repeat to late

    • @perssontm1628
      @perssontm1628 4 роки тому

      A bit late but just add another bg to the right then :)

  • @zhangchina5198
    @zhangchina5198 Рік тому

    First of all, I really appreciate it, because your work is very helpful to me

  • @vlodarz
    @vlodarz 3 роки тому +6

    So I needed the background to move from up to down infinitely while my camera would stay in place so I changed the script a bit. Might be useful for someone so here it is:
    public class Parallax : MonoBehaviour
    {
    private float length, startpos;
    public float parallaxEf;
    private float speed = -0.01f; //ypu can change this
    private float distFromMiddle = 0; //keep it 0
    void Start()
    {
    startpos = transform.position.y;
    length = GetComponent().bounds.size.y;
    }
    void Update()
    {
    distFromMiddle = distFromMiddle + speed;
    float dist = (distFromMiddle * parallaxEf);

    transform.position = new Vector3(transform.position.x, startpos + dist, transform.position.z);
    if (dist < startpos - length)
    {
    transform.position = new Vector3(transform.position.x, startpos - length, transform.position.z);
    distFromMiddle = 0f;
    }
    }
    }

    • @NomansSkywalker
      @NomansSkywalker 2 роки тому

      Thanks

    • @ekobooy
      @ekobooy 2 роки тому

      @@NomansSkywalker its work nice while cam stay in one position, nice to use in menu backgrounds thhx

  • @FuzzyDPozzy
    @FuzzyDPozzy 5 років тому +1

    the simplex parallel bg i saw on youtube so cool thanks for sharing man keep make more tutorials

    • @Danidev
      @Danidev  5 років тому +3

      Thanks! That's why I made this video :D

  • @michaelsander2878
    @michaelsander2878 4 роки тому +9

    If you hold alt while dragging images into unity it won't make an animation.

  • @BloodwolfTico
    @BloodwolfTico 5 років тому +2

    FINALLY! A Parallax tutorial that works for me

    • @Danidev
      @Danidev  5 років тому +1

      Glad to hear :)

  • @TheVarntex
    @TheVarntex 4 роки тому +3

    I am having trouble, I dont have layered background like you, but ive followed as closely as I can anyways. When the camera scrolls, all the images merge together and only repeats when the camera is completely out of the image

    • @FrozenWolf001
      @FrozenWolf001 4 роки тому +2

      This happend because you attach the script to the background object, try to remove the script on the child object, and apply the script ONLY to the parent object

    • @fcblp1
      @fcblp1 4 роки тому

      @@FrozenWolf001 legend

    • @noodleplexium5953
      @noodleplexium5953 4 роки тому

      @@FrozenWolf001 For some reason this doesn't solve the issue for me. I'm getting an error because there is no SpriteRenderer on the parent object.

    • @EdwinCreatives
      @EdwinCreatives 4 роки тому

      @@noodleplexium5953 Your parent Object must be the original image instead of duplicated image or Empty GameObjects

    • @MrLewislauyik
      @MrLewislauyik 4 роки тому

      @@FrozenWolf001 You saved me!!! Thank you very much!!!

  • @shapeshifter8986
    @shapeshifter8986 5 років тому

    WOW this blew me away..clean simple and beautiful
    Thanks a lot!

    • @Danidev
      @Danidev  5 років тому +1

      Glad to hear that!

  • @dailydoseofchocolate9411
    @dailydoseofchocolate9411 5 років тому +3

    Hi, how do you handle different aspect ratio when the bg wont cover the whole viewPort?

    • @EdwinCreatives
      @EdwinCreatives 4 роки тому

      aspect ratio doesn't affect the camera much. All you have to do is adjust the viewport's height and width to suit your background

    • @dailydoseofchocolate9411
      @dailydoseofchocolate9411 4 роки тому

      @@EdwinCreativesI see, how do you adjust the view port?

  • @Keksboi2
    @Keksboi2 2 роки тому

    DANI MADE TUTO ON HIS MAIN?!?!? i never knew abut did :O

  • @harleybekker
    @harleybekker 5 років тому +3

    Thank you :))

  • @felipetomachevski1562
    @felipetomachevski1562 Рік тому

    Great video, really helped me to understand the concept. Thank you very much

  • @SassyCassi
    @SassyCassi 5 років тому +4

    i've tried so hard x.x and it's just not repeating the layers at all

    • @aryanhavrest
      @aryanhavrest 4 роки тому

      Yupp, same for me. You gotta use this
      length = GetComponent().size.x;
      so dont write bounds

  • @uthriyakumar9743
    @uthriyakumar9743 3 роки тому

    Dani tutorial lives

  • @ryanh8518
    @ryanh8518 4 роки тому +3

    Great tutorial! My only problem is when I move it, it is very choppy. It takes a while to load in, I have a decent laptop so I am not sure why.

    • @iTheBOSSx
      @iTheBOSSx 3 роки тому

      did you fix it bro?

  • @RAULGARCIAYunqeExplorer
    @RAULGARCIAYunqeExplorer 3 роки тому +1

    great demo - thanks and seems easy!

  • @PanconGo
    @PanconGo 5 років тому +3

    Hey man, nice tutorial! I have a question... How do you get that Windows classic theme? I love it!!

    • @Danidev
      @Danidev  5 років тому +3

      If you're on Win 7, right click on desktop > preferences > and then select classic theme :D

    • @PanconGo
      @PanconGo 5 років тому

      @@Danidev Oh! I think that you're using Windows 10 u.u, thank you anyway

  • @Frithuritaks
    @Frithuritaks 3 роки тому +1

    Who would have guessed that when I’m trying to learn to code the milkman himself shows up at my doorstep and teaches me a lesson

  • @partea3
    @partea3 4 роки тому +3

    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 :)

    • @Helis-wz7ei
      @Helis-wz7ei 4 роки тому

      Exactly what i wanted ! Thanks !

  • @Walmartasorus
    @Walmartasorus 3 місяці тому

    cant wait for the second tutorial

  • @ozanyokuva6930
    @ozanyokuva6930 5 років тому +3

    As you put the BG under the cam , when you try to adjust the cam you also move the background images , it makes designing very hard. You better update your paralax code to work without this hierarchy dependency. A am working on updating the code but I am a little new in unity, if you can update It will make the tutorial more relevant.

  • @simplylogan93
    @simplylogan93 5 років тому +1

    Fantastic tutorial man -- you really helped me out 👍 Going to use this script in my game.

  • @edwardharks
    @edwardharks 5 років тому +3

    Thanks for the great tutorial. One question - why do you use FixedUpdate if you're not updating physics?

    • @Danidev
      @Danidev  5 років тому +2

      Mainly because I've had some problems using the normal Update method when working with cameras. Sometimes the background moves in a choppy and jittery way, but using FixedUpdate() fixed it for me. But in theory normal Update should work fine.

    • @edwardharks
      @edwardharks 5 років тому

      @@Danidev I'll keep that in mind next time i'm working with the camera!

    • @PiyPowKachou
      @PiyPowKachou 5 років тому

      @@Danidev It was probably because of Physics related things. Physics run in a fixed update, thus when moving stuff with Rigidbody, for example, you get choppy movements because while Update has already updated position for camera and new renders come from a different position. Other stuff is still standing still until physics update. So it's kind of 3 frames freeze usually depending on the frame-rate ofc.

    • @chineduachimalo391
      @chineduachimalo391 5 років тому

      @@Danidev still experiencing little jittering even after fixed update, tried a everything i know, so exhausted

    • @ericbishop2461
      @ericbishop2461 5 років тому

      Try using LateUpdate to move the camera, as it is called every frame, but after everything else is done moving.

  • @leonytgames
    @leonytgames Рік тому

    thanks dani 3 years ago! ;D really helped me!

  • @thebiggestmaninexistence6239
    @thebiggestmaninexistence6239 4 роки тому +3

    I cant do art dani, so can i use this for a game?

    • @monkizaki1440
      @monkizaki1440 4 роки тому

      Uniqueness brings attention. Download free tree, bush, rocks, mountain brushes to make the background.

  • @AlessandroRubeo
    @AlessandroRubeo 2 роки тому +1

    does anyone know why the background does not move smoothly but has a flickering effect? I followed the tutorial step by step but I have this problem

  • @carlosaguerou
    @carlosaguerou 4 роки тому +3

    Hi, amazing tutorial! where i can download the resources? the background? i joined into discord but i dont find the resources

    • @stephen9849
      @stephen9849 4 роки тому

      Now it's all about Karlson, no one cares anymore about any other game.

    • @EdwinCreatives
      @EdwinCreatives 4 роки тому

      That's because you join the Discord group way too late. Dani might have forbidden the project files or hidden or even deleted

  • @youngjepark5835
    @youngjepark5835 3 роки тому

    wait i did not know this video is from dani! amazing

  • @alphag4mer909
    @alphag4mer909 5 років тому +8

    7.5k Views and only 80 comments...

    • @skyhigh286
      @skyhigh286 5 років тому +6

      Which means.. he explained so well we don't even need to ask questions

  • @wildmcmxcv
    @wildmcmxcv Рік тому

    The Best Parallax Effect guide, Thanks for help