Parallax Infinite Scrolling Background in Unity

  Рет қаралды 150,570

Code Monkey

Code Monkey

Күн бұрын

Пікірлер: 278
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Add this simple script to make your 2D games stand out with a nice depth effect! 🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel! ✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php
@wss_maze.7819
@wss_maze.7819 Жыл бұрын
pls help, for me the hills dont snap smoothly in position. They change their position and you can see it very obviusly.
@olddogscanlearnnewtricks6079
@olddogscanlearnnewtricks6079 3 жыл бұрын
It is amazing how every single 15-20 minutes video from Code Monkey leads yourself into an hour or even days of deep learning into Unity if you are open to really understand all the aspects of what he is teaching. Excellent content as always !!! Thanks a lot !
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
I'm glad the videos helped you! Thanks!
@metalfeng8778
@metalfeng8778 4 жыл бұрын
You pronunciation is so clear that I watch your videos in 2x speed and I can still comprehend what you're saying. Amazing, thank you for the efficiency boost.
@phantomBlurrrr
@phantomBlurrrr 3 жыл бұрын
Thank you for pointing this out, just saved 8 minutes or whatever
@ToadSprockett
@ToadSprockett 5 жыл бұрын
This is so stinking good, it's simple and solid. I've written a couple of these and this is now my go to...
@aldin7158
@aldin7158 5 жыл бұрын
I just hope that one day you won't delete all your videos because they are sooo valuable, I am planning to watch them all from beginning sometime Thank you for this amazing value you are giving us!
@ElSonk
@ElSonk 5 жыл бұрын
I programmed this myself just recently - but your code is so much better and flexible. Will adapt it tomorrow. Thanks!!
@rishabparmar2497
@rishabparmar2497 4 жыл бұрын
Thanks for this CodeMonkey, really appreciate this. Now, if your parallax is still jittering/ lagging: try one of the following methods, hopefully they might work: - Try alternating between FixedUpdate() and LateUpdate() - Try changing the Interpolation on the player(Interpolate or Extrapolate or None). Try combinations with the above step - Lastly, this worked for me: Try changing the pixel size of the sprites by clicking on the sprite and not the object. Mine was 100, but when I changed it to 64, it ran butter smooth
@Mooskislide
@Mooskislide 4 жыл бұрын
how do I click on the sprite instead of the object?
@ceoofbananaco.9960
@ceoofbananaco.9960 Жыл бұрын
OH MY GOD THANK YOU
@rishabparmar2497
@rishabparmar2497 Жыл бұрын
@@ceoofbananaco.9960 keep passing the knowledge ahead!
@AsaFalyRayyan
@AsaFalyRayyan Жыл бұрын
you are awesome, this works for player "check point" system
@VoidShadow66
@VoidShadow66 Жыл бұрын
Thanks a lot man i solved all my problems , 4 hours spent on creation big level of my game using a parallax and Cinemachine + drawing my own sprites. Ur vid is great!!!
@ItsAllAboutComedyHere
@ItsAllAboutComedyHere Жыл бұрын
thank you for awesome video , one thing for people who want to use your Script for their project , if you change Scale of your picture ... movement of the picture would not be smooth as the video and thats because texture unit size assume Scale is at one ... so only thing you have to do is to multiply textureUnitSizeX and textureUnitSizeY with transform.localScale.x and transform.localScale.y ... again thank you Code Monkey for amazing videos :) love from an indie dev from IRAN ...
@neonage6294
@neonage6294 5 жыл бұрын
You're always writing such simple stuff in more advanced way, I didn't know when to use mod operator, keep it up! :D
@BRicey
@BRicey 4 жыл бұрын
I've tried to write a few of these on my own.... But it was never as simple, robust, and expandable as yours... Thank you so much
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
I'm glad you found it useful!
@jean-michel.houbre
@jean-michel.houbre 5 жыл бұрын
Another video really well done and simple to follow. I just love it !
@jdc68
@jdc68 3 жыл бұрын
Awesome tutorial. Been scratching my head a long time over this, happy to find such an easy solution
@googlefirst
@googlefirst 4 жыл бұрын
This is by far the best video on Parallaxing.
@MrSixification
@MrSixification 5 жыл бұрын
Clever. Impressive simplicity. You rock.
@om3rz662
@om3rz662 5 жыл бұрын
I love you!! Thats exactly what I was searching for!! 😀😂
@hanpolo2727
@hanpolo2727 7 ай бұрын
Still works 4 years later so thanks bud!
@evoldev5450
@evoldev5450 3 жыл бұрын
Can you please tell me what to do if the new position.x of the background does not exactly fit the previous one?
@MioRaem
@MioRaem 3 жыл бұрын
I did everything till 6:13 and when I test the game, the background layers dont move at all anymore. What the f am I doing wrong? :(
@YannickGames
@YannickGames 4 жыл бұрын
Super useful tutorial! 👍 Only thing, when I used this with the cinemashine I needed to change the LateUpdate() to fixedUpdate(). Probably because the CM is Updating the camera in fixed Updates 🤔
@bertalannyary3765
@bertalannyary3765 4 жыл бұрын
Hello Code Monkey! Currently I'm making a 2d sidescroller in a pixelart style, and I tried to implement this infinite parallax background. I wrote the exact lines in my script (after failing multiple times I've downloaded the assets, and just copied the script.) but it did not work. Instead of the "background jumping to the other side" mine is kinda following the camera (like I have a wide background object and it keeps stuttering, and it only shows one part). Please, if you could help that would really make my day!
@MrLukimen
@MrLukimen 3 жыл бұрын
Once again, your way is the best! thank's for your content
@d.d.2673
@d.d.2673 2 жыл бұрын
It was exactly solution for what i was looking for. Other tutorials can be deleted. Had only problems with layer ordering while using some weird method like puting image on plane object or it wasnt working at all. By the way i like your code style. You do it like normal not amatour.
@juicedup14
@juicedup14 5 жыл бұрын
I was just looking at tuts for this!!
@digen_art
@digen_art Жыл бұрын
I really like the walking animation.
@anirbankarmakar2037
@anirbankarmakar2037 5 жыл бұрын
It was awesome , keep making such videos. It will be really good if you cover some 3d topic also.
@Plays-wi1yx
@Plays-wi1yx 4 жыл бұрын
I am trying with this code but the background moving with camera but not adjusting with the previous background.
@marcosengelhard5456
@marcosengelhard5456 5 жыл бұрын
Hello Code Monkey, is there a reason that you turned start() update() into the private? the variable parallaxEffectMultiplier could be public, if it's public, it would appear in the inspector ( when it's private, you need to write [SerializeField] in order to show on the inspector.)?
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Making a variable public not only exposes it to the editor it also exposes it to the rest of your entire project. So if you have something that is meant to be modified solely in the script and in the editor, make it private and add [SerializeField], that will keep you code nice and clean and make it obvious that no other script will ever touch that field. Start(); and Update(); should always be private, no other script should ever call those.
@bobbycrosby9765
@bobbycrosby9765 3 жыл бұрын
Note that if you use scaling this doesn't factor that in. I had to multiply textureUnitSizeX by transform.localScale.x.
@hanmayujiro1433
@hanmayujiro1433 3 жыл бұрын
OMG. thanks! =)
@tshubham36
@tshubham36 Жыл бұрын
You are awesome. Had to scroll a lot to reach this. But worth it. Thanks!
@hymus8627
@hymus8627 Жыл бұрын
Thank you very much
@TheOfficialPolo
@TheOfficialPolo 5 жыл бұрын
Is this the start of Side Scroll game tutorials? God Bless you!
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Yeah that's definitely something I'd like to cover more. I was going to make a complete game like Terraria but had to switch to the Top Down Shooter game because there are still tons of Platformer elements that I haven't covered.
@MinjiKim-k7j
@MinjiKim-k7j 2 жыл бұрын
Great video thank you so much!! You saved my day! I assume it doesn't make much difference but substracting the offsetPosition from the transform.position seems more natural for me. Sorry if I got it wrong! I'm just a beginner.
@JanMihail
@JanMihail Жыл бұрын
Я тоже заметил это. Я думаю это ошибка и она будет заметна, если размер повторяющейся зоны будет маленький
@MinjiKim-k7j
@MinjiKim-k7j Жыл бұрын
@@JanMihail Why, thank you! I'm happy that someone is on the same page. :)
@Badyliani
@Badyliani 4 жыл бұрын
Anyone using Cinemachine - change LateUpdate => FixedUpdate. It will stop the jittering.
@lamarhoward8478
@lamarhoward8478 4 жыл бұрын
Amazing, thanks for that! 👍
@BRicey
@BRicey 4 жыл бұрын
THIS WAS SUPER HELPFUL
@imheretosleep
@imheretosleep 3 жыл бұрын
I tried implementing this to my game but when I built it to android the background kept blinking, any idea how to fix it?
@IsaacPrice
@IsaacPrice 2 жыл бұрын
I wish I could like this video as many times as I can click like for it's duration of the video
@kliment7904
@kliment7904 2 жыл бұрын
Excellent work! Thank you.
@theDarkerSan
@theDarkerSan 4 жыл бұрын
A small improvment I found was to change the the lastCameraPosition in the start() from camera position to Vector3.Zero Let's say you build your level with the camera in a certain place, and playtest. looks good. Now if you change the camera position and play again, the parallax start in a place relative to the camera position and this messes things up a little (I found it the hard way). with vector3.zero it will aways have the deffault editor start position.
@nightlifebartenders
@nightlifebartenders 3 жыл бұрын
THE ULTIMATE GUIDE!
@ThinkCitric
@ThinkCitric 4 жыл бұрын
Thanks man! You saved my day! :)
@tigransahakyan2840
@tigransahakyan2840 5 жыл бұрын
Great videovand great effect )
@jamiejones8756
@jamiejones8756 4 жыл бұрын
Working beautiful, thanks!
@thedrewster2437
@thedrewster2437 Жыл бұрын
Ok so everything is working fine other than the clouds are not infinite for some reason and I cannot seem to figure out why. I have toggled off the vertical bool and that's not seeming to help the issue.
@heyyounotyouyou3761
@heyyounotyouyou3761 4 жыл бұрын
I don't know I to thank you You really are smart
@Smarthomeyourself
@Smarthomeyourself 4 жыл бұрын
@Code Monkey: can you check your script with scaled transformation on background graphic? if i scale my graphics up than the background jumps on x-axis if it was moved. I think I have to calculate the texture-width on another way but I didn't understand how. :(
@Howdyhowthis
@Howdyhowthis 4 жыл бұрын
Didnt know if i'm in time, but if you scale up your sprite simply multiply it with transform.localscale in textureunitsizeX
@Asheroonis
@Asheroonis 2 жыл бұрын
@@Howdyhowthis I know it's been ages since but thank you so much, I've been struggling with this for a while now, you're a life saver!
@Howdyhowthis
@Howdyhowthis 2 жыл бұрын
​@@Asheroonis I was struggling with that for a while too. Glad I'm leaving that comment there. Good luck with your project!
@BaguillaPascual
@BaguillaPascual Жыл бұрын
Hi @CodeMonkeyUnity, I am amazed to see that you have still responded to some relatively recent comments and it has given me hope to ask a question. I think I followed the video without errors, but I have a problem with the parallax because when you have to jump seamlessly there is a clear jump. I have been touching the dimensions of my background, I don't know if that could be causing the error. Any help would be appreciated.
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
It works seamlessly horizontally but not vertically? The issue will be something related to the math and/or the size of the texture. Maybe you're applying the same offset on the X and Y even though it has different width/height? Look at the scene view while playing to see exactly what is going wrong, is it jumping too much or too little? Add some logs to see what values you're working with
@Splash029
@Splash029 2 жыл бұрын
Thanks! :D
@Profil2579
@Profil2579 5 жыл бұрын
Very simple and very powerful to use, thanks! =)
@easternunit2009
@easternunit2009 Жыл бұрын
You must type really fast, based on the very super fast keyboard typing sounds. No, but seriously very good tutorial .
@Kidopya
@Kidopya 4 жыл бұрын
When i scale sprite game objects in Inspector, parallax lags. how can i scale sprites without lag? I use single sprite mode with full rect and 2000 width for tiling
@toxlandmusic912
@toxlandmusic912 4 жыл бұрын
Thank god I saw your ad moths ago it was worth it
@hilmanahza2688
@hilmanahza2688 4 жыл бұрын
I was exactly with your code but, instead my player movement scripts didn't work
@VampireForFood
@VampireForFood 4 жыл бұрын
so for whatever reason, the background elements that i add the script to are very shaky and unstable, nothing like the smooth effect you're getting.... any ideas?
@VampireForFood
@VampireForFood 4 жыл бұрын
to add to this, it's only happening in my game window, it looks great in the scene window
@MikhaelHld
@MikhaelHld 2 жыл бұрын
Thank you so much! You're amazing!
@an_bean373
@an_bean373 8 ай бұрын
Hi! I have a random question: when getting the sprite for the game object, why do you call GetComponent().sprite instead of GetComponent() ? To me it sounds like they do the same thing, is there like a small difference between the two? Your videos are incredible, by the way, they are lifesavers!
@CodeMonkeyUnity
@CodeMonkeyUnity 8 ай бұрын
There is no Sprite component that you attach to a GameObject, just a SpriteRenderer so you cannot do GetComponent(); you need to get the SpriteRenderer and find the sprite attached to it I'm glad my videos have helped you! Thanks!
@Ermite_des_songes
@Ermite_des_songes 4 жыл бұрын
Someone has by any chance a tip that this could work if I change the size of the camera ? My target is that the camera go back more the character run fast. :) It's working by setting the camera on perspective mode but is it good ?
@Pendo8
@Pendo8 2 жыл бұрын
Every so often, the texture jumps and its quite noticable that the image has moved. Has anyone experienced this ?
@SayehAvaPazouki
@SayehAvaPazouki 4 жыл бұрын
I just set X Multiplier to 0.9 but its still too fast for me! how can I make it slower?
@david_chan_95
@david_chan_95 2 жыл бұрын
Thank you so much! Awesome! 8)
@tangcahill
@tangcahill 5 жыл бұрын
Hey, Can you think of any reason why this script might not work for me? The general parallax effect works great but after adding the if statement to LateUpdate the images don't align on transition at all. Using a virtual camera rather than a regular camera in my project. If I can't find a solution, would it be acceptable to just have a very long parallax background instead? Cheers.
@IvoryAV
@IvoryAV 4 жыл бұрын
Did you find a solution ? Got the same problem
@theAmbroise16
@theAmbroise16 3 жыл бұрын
I think that I am facing the same issue. When the background repositions itselfs, there is a small jump. It almost fit perfectly, but we can still notice that it is not aligned. I've copied perfectly and double checked almots everything but it still does it.. If you've found a solution it woud be so much appreciated ! thanks
@jak12329
@jak12329 Жыл бұрын
Did you or anyone figure out why? Got the same issue
@howardxu8050
@howardxu8050 2 жыл бұрын
I followed your instructions to create a parallax background, but mine is still showing clear snaps when it triggers. Not useful
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Then you did something wrong, computers are deterministic, if you do exactly what I show in the video you will get exactly the same result.
@khalidalhariri4433
@khalidalhariri4433 4 жыл бұрын
What if I want to add stuff to the scrollimg background that appear randomly but gets harder the more the player goes forward?
@thiagobastos164
@thiagobastos164 5 жыл бұрын
Great video! I'd like to ask if there's a way to make the background (a static background) always fit the player screen. I've tried many things but it never worked.
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
That requires doing some math based on the texture size, sprite pixelsPerUnit and Screen.width and Screen.height
@thiagobastos164
@thiagobastos164 5 жыл бұрын
@@CodeMonkeyUnity Does the code that you showed in this video helps with that? If not, do you have one video on the subject? Thanks for the answer!
@ДмитрийНечепуренко-т7д
@ДмитрийНечепуренко-т7д 2 жыл бұрын
Why it not works smooth with cinemamachine?
@PhilDeveloper
@PhilDeveloper 4 жыл бұрын
Why is my background moving backwards on the Z axis :(
@frantaoo2820
@frantaoo2820 4 жыл бұрын
Finaly something that works for me thank you
@topit
@topit 4 жыл бұрын
Nice tutorial. But as I am a beginner, I really hope that you will make a tutorial of it using Bolt Visual Scripting. Thank you.
@vergilw7009
@vergilw7009 2 жыл бұрын
hello, is it work for the Main camera by orthographic ??
@supercruiser4925
@supercruiser4925 4 жыл бұрын
I subscribed because you don't shove your face to the camera like other Unity coders.
@dr.gaming2022
@dr.gaming2022 5 жыл бұрын
What about Cinemachine. If I am using Cinemachine will this still work.
@riskybicky8335
@riskybicky8335 2 жыл бұрын
Hi, the movement of the background goes to move, jitters, and then resets back to its original position? Is this something I can solve? I am using Cinemachine as a camera and the background witth the effect applied is a child of the CM vcam1.Thanks
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
It moves then moves back? Sounds like you have some conflicting logic, add some Debug.Log's to see what your code is doing
@RobitI795
@RobitI795 5 ай бұрын
I made same script like in this video, put same values and setting for sprites, it's noticeable that they're snapping, Is it supposed to be like this or did I do something wrong? I put video in United Programming server
@phantomBlurrrr
@phantomBlurrrr 3 жыл бұрын
I'm having an issue where moving up/down the background does not seamlessly move to where it should be. I double checked the sprite renderer size and it's set properly to the default values when you first drag in the background * 3. Should those default values not be trusted or what.. super inconsistent anyway, sometimes it moves perfectly seamlessly and sometimes not..
@benelliott9158
@benelliott9158 3 жыл бұрын
try putting everything in FixedUpdate
@curb47
@curb47 4 жыл бұрын
Kinda nice, but can't get it working. When the background layers re-position themselves, they don't align and there is an obvious jump. I've experimented with parameters but no success. In the video, you skim over the section about setting the edge of the texture too quickly, there's no detail. What can I do to fix this? Thanks.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
The texture just needs to repeat itself. The code teleports it into the exact perfect position.
@theAmbroise16
@theAmbroise16 3 жыл бұрын
@@CodeMonkeyUnity I am facing the same issue. When the background repositions itselfs, there is a small jump. I've copied perfectly and double checked almots everything but it still does it.. Have you found a solution curb47 ? Would be greatly appreciated !
@kristianfejes2133
@kristianfejes2133 Жыл бұрын
@@theAmbroise16 did u fixed that? bcs now i am strugglin on it. Please help
@podcastcluster
@podcastcluster Жыл бұрын
the light mode burns my eyesssss
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Sorry but dark mode burns my eyes
@robh5555
@robh5555 2 жыл бұрын
Where are the best place to get utilities for making games when making and selling games?
@jul7871
@jul7871 3 жыл бұрын
my background sprite turns into just 1 plain color when i change the draw mode to tiled. it looks normal in the other 2 modes, but tiled mode doesnt work. its just a rectangle.
@olddogscanlearnnewtricks6079
@olddogscanlearnnewtricks6079 3 жыл бұрын
click on your asset and change the import setting: Mesh Type from Tight to Full Rect.
@resistancefighter888
@resistancefighter888 2 жыл бұрын
Hey, thank you so much for the video! Can you please explain why you're adding offsetPositionX to the new background position and why did you calculate it in the way you did?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
It needs to be perfectly calculated so that when it snaps to a different position the effect is invisible, if you don't calculate it perfectly then you will see the background "move" instead of flawlessly repeating
@resistancefighter888
@resistancefighter888 2 жыл бұрын
@@CodeMonkeyUnity Thanks for the explanation, I understand that this is meant to make the snapping invisible to the user, my question is why does this achieve this?
@JanMihail
@JanMihail Жыл бұрын
@@resistancefighter888 Вообще более правильно сделать camera.position.x - offsetPositionX. В данном случае это может быть несущественным, т.к. если изображение достаточного высокого разрешения, то склейка со смещением в пару пикселей не будет заметна, но это может стать заметным на более мелком разрешении. Достигается это за счёт того, что offsetPositionX вычисляется таким образом, что он всегда кратен ширине повторяющейся части фона и сдвигается ровно на то расстояние, на котором идёт повтор. Таким образом мы за один кадр перемещаем повторяющуюся зону вперед или назад и оно идеально стыкуется и кажется, что картинка не двигалась.
@telocim
@telocim 2 жыл бұрын
Would you consider updating this or giving some additional instructions on getting it to work with Unity 2022? I've tried several times, even re-installed Unity 2019. I haven't been able to get it to run as displayed in the video. The biggest issue is the player is "invisible" but the background is moving around nicely. After playing around with the shader settings (I'm a novice) I was able to see a blocky white player-like object moving around the screen. Please and thank you!
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
There's nothing here that is dependent on a specific Unity version. Sound like you just have an issue with your sorting layers, not shaders unitycodemonkey.com/video.php?v=5_BwFB-1dAo
@IlieStoianLiveShow
@IlieStoianLiveShow Ай бұрын
How can I make this effect if my character doesn't move, but only jumps, like the flappy bird?
@CodeMonkeyUnity
@CodeMonkeyUnity Ай бұрын
It's the exact same thing, you just have different layers moving at different speeds, doesn't matter if they're moving in the X or Y axis
@IlieStoianLiveShow
@IlieStoianLiveShow Ай бұрын
I made it 🥰🥰
@haithammahmoud6825
@haithammahmoud6825 5 жыл бұрын
how do u manage enemy damage per levels do u use formal? because if player bought weapon and armor will kill enemy easy so game will be boring i don't want to make game very hard or very easy can u help with that Thanks
@Tidalley
@Tidalley Жыл бұрын
How do you apply this to TileMaps?
@lighthousestudio15
@lighthousestudio15 2 жыл бұрын
Guys what to do when the mountains snap? It snaps whenever I move pls help
@alexv9607
@alexv9607 4 жыл бұрын
the script is not working for me i get an error i says: transform.position assign attempt for 'clouds_4' is not valid. Input position is { NaN, -4.730000, 0.000000 }. UnityEngine.Transform:set_position(Vector3) ParallaxBackground:LateUpdate() (at Assets/Scripts/ParallaxBackground.cs:31) how to fix this? i dont know how but great vids.
@naughtychrome1322
@naughtychrome1322 3 жыл бұрын
I know it's a year later but I copied everything you did on the video fine and it worked but when I made a new project and I did everything again. everytime I moved horizontally or vertically it's snaps the background so I can see in game that It is definitely moving and it doesn't look smooth any suggestions?
@kojimamcmaple
@kojimamcmaple 3 жыл бұрын
I think LateUpdate() is the issue. I had to change everything to Update() for it to work.
@rayztech7384
@rayztech7384 2 жыл бұрын
thanks for the tutorial. i followed everything and i keep getting "object reference not set to an instance of an object" and the error comes from vector 3 line 24 in your code
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
Use Debug.Log to find what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
@nateham4260
@nateham4260 2 жыл бұрын
I know it's a long shot because of the age of the video, but with the [SerializedField] I am getting an error in unity that says "the type or namespace name "SerializedField" could not be found" am I doing anything incorrect?
@CodeMonkeyUnity
@CodeMonkeyUnity 2 жыл бұрын
It's SerializeField
@wadetwynham4734
@wadetwynham4734 Жыл бұрын
In the beginning of the script when you add camera.main.transform it doesn't reference the camera/ highlight it. So when I go out of the script it gives error 'Camera does not contain a definition for Transform.......etc Can anybody please assist? Just a note I had to wipe my pc and downloaded the new version of unity and c#
@CodeMonkeyUnity
@CodeMonkeyUnity Жыл бұрын
Make sure you write the code correctly, it's Camera.main.transform not Camera.main.Transform, code is case sensitive
@itayefraim3559
@itayefraim3559 3 жыл бұрын
if your parallax is still lagging, Maybe it's because you scale the picture up
@nischalbhatta815
@nischalbhatta815 5 жыл бұрын
You inspire me!
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Glad to hear it!
@iyanez
@iyanez 3 жыл бұрын
forsome reason the background goes backwards not forward
@curb47
@curb47 4 жыл бұрын
I had a 2.5D game, with3D characters, platforms and objects etc on Z=0. Imagine Little Big Planet. I need to set up an infinite scrolling background like this, but it only seems to work when the sprites are on Z=0, which obviously intersect with my game objects. Is this technique strictly ONLY for completely flat 2D sprite based games? Thanks.
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
If your camera is 3D then you don't need this since you have Parallax by default, just place your backgrounds at different Z positions. The further an object is from the camera the slower it will appear to move.
@curb47
@curb47 4 жыл бұрын
@@CodeMonkeyUnity Yes, I understand that, but to get the kind of of parallax you demonstrate in your tutorial, the horizon layer will have to be really far away on the Z, and a huge image. Is there not a performance hit if images are hugs, or scaled up? Thanks.
@alanwest681
@alanwest681 4 жыл бұрын
I could get it working on my own dodgy project, but when I import the provided package file, all the textures and scripts are broken/missing. I'm using Unity 2019.3.15f1. Any suggestions?
@CodeMonkeyUnity
@CodeMonkeyUnity 4 жыл бұрын
What exactly is broken or missing?
@mr.fakeman4718
@mr.fakeman4718 4 жыл бұрын
For me, the code shown in the video doesn't work for creating the vertical infinite. However, it does work for the X axis, and I also can move for the Y axis, except that for the Y axis, it is not infinite... too bad. Here is my code: using UnityEngine; using System.Collections; using System.Collections.Generic; public class ParallaxBackground : MonoBehaviour { [SerializeField] private Vector2 parallaxEffectMultiplier; private Transform cameraTransform; private Vector3 lastCameraPosition; private float textureUnitSizeX; private float textureUnitSizeY; private void Start() { cameraTransform = Camera.main.transform; lastCameraPosition = cameraTransform.position; Sprite sprite = GetComponent().sprite; Texture2D texture = sprite.texture; textureUnitSizeX = texture.width / sprite.pixelsPerUnit; textureUnitSizeX = texture.height / sprite.pixelsPerUnit; } private void FixedUpdate() { Vector3 deltaMovement = cameraTransform.position - lastCameraPosition; transform.position += new Vector3(deltaMovement.x * parallaxEffectMultiplier.x, deltaMovement.y * parallaxEffectMultiplier.y); lastCameraPosition = cameraTransform.position; if (Mathf.Abs(cameraTransform.position.x - transform.position.x) >= textureUnitSizeX) { float offsetPositionX = (cameraTransform.position.x - transform.position.x) % textureUnitSizeX; transform.position = new Vector3(cameraTransform.position.x + offsetPositionX, transform.position.y); } /* if (Mathf.Abs(cameraTransform.position.y - transform.position.y) >= textureUnitSizeY) { float offsetPositionY = (cameraTransform.position.y - transform.position.y) % textureUnitSizeY; transform.position = new Vector3(transform.position.x, cameraTransform.position.y + offsetPositionY); }*/ } }
@muItipix
@muItipix 4 жыл бұрын
You typed x twice instead of y... private void Start() { cameraTransform = Camera.main.transform; lastCameraPosition = cameraTransform.position; Sprite sprite = GetComponent().sprite; Texture2D texture = sprite.texture; textureUnitSizeX = texture.width / sprite.pixelsPerUnit; textureUnitSize = texture.height / sprite.pixelsPerUnit; ⬅️ }
@sublemonal9758
@sublemonal9758 3 жыл бұрын
your a hero brother 💪💪💪💪💪💪💪
@pantoygames5392
@pantoygames5392 4 жыл бұрын
Hi nice tutorial, but in android, it causes slow... When I remove it, it's fast... Is there any workaround for that? thanks... (I am using cinemachine2d by the way)
@avery6787
@avery6787 4 жыл бұрын
Hi pantoy, did you find any workaround for this? I am having the exact same problem
@archer8629
@archer8629 4 жыл бұрын
when you code... you don´t speed up the video do you..?
@steffstar
@steffstar 3 жыл бұрын
Hi! How do you make the backgrounds move accordingly in scene view when you move the camera around? I got the parallax effect to work when I run the game, but when I move the camera in scene view, it doesn't show the effect. Thank you!
@CodeMonkeyUnity
@CodeMonkeyUnity 3 жыл бұрын
The script only runs when the scene is playing. If you want it to run while not running you need some sort of Editor script kzbin.info/www/bejne/mH7YhaZ3osZnqs0
@hhashh2327
@hhashh2327 4 жыл бұрын
How can I make it work in an endless runner?
@KalponicGames
@KalponicGames 4 жыл бұрын
Hi I think you can make an array for background elements. Then we can add as many background assets we want.
@ricks8011
@ricks8011 5 жыл бұрын
I'm noticing a lot of stuttering as you move. Is there a way to smooth that out? Thanks!
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
I don't see any stuttering, are you talking about the video or in-game?
@aava5312
@aava5312 4 жыл бұрын
@@CodeMonkeyUnity I'm getting stuttering as well in my project. The faster my character moves the worse the stutter seems but its always stuttering, I have pixel art not sure if that could be the root of the issue.
@aava5312
@aava5312 4 жыл бұрын
@@CodeMonkeyUnity Fixed it by doing this if (cameraTransform.position.x - transform.position.x >= textureUnitSizeX) { transform.position += new Vector3(textureUnitSizeX, 0, 0); } else if (cameraTransform.position.x - transform.position.x = textureUnitSizeX) { float offsetPosition = (cameraTransform.position.x - transform.position.x) % textureUnitSizeX; transform.position = new Vector3(cameraTransform.position.x + offsetPosition, transform.position.y, transform.position.z); }
@janmiodekrulez4472
@janmiodekrulez4472 4 жыл бұрын
@@aava5312 It's incorrect logic because "cameraTransform.position.x - transform.position.x >= textureUnitSizeX" probably will never execute exact when cameraTransform.position.x - transform.position.x IS EQUAL your textureUniteSizeX. That's why you need offset. 👍
@alistairshanks8141
@alistairshanks8141 2 жыл бұрын
i also got jittering and this fixed it for me, thanks very much. do you know why the jittering occurs?
@GamerXPlayz
@GamerXPlayz 5 жыл бұрын
Amen video on how to add level system on unity
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
Here's a Level System kzbin.info/www/bejne/oXymfYCsqMx0iJI
@GamerXPlayz
@GamerXPlayz 5 жыл бұрын
@@CodeMonkeyUnity no-no not character level I said game level
@CodeMonkeyUnity
@CodeMonkeyUnity 5 жыл бұрын
@@GamerXPlayz Loading scenes? kzbin.info/www/bejne/aXqYlWWoiq9jpqc
@looking_arround
@looking_arround 5 жыл бұрын
@@CodeMonkeyUnity I assume they meant Scenes aka Game Levels (like Levels of an Adobe flash platformer)
Infinite Parallax Scrolling Background - Unity 2D Complete Tutorial
7:24
Game Code Library
Рет қаралды 11 М.
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 196 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 54 МЛН
Learn Unity in 17 MINUTES! [2024 UPDATED PINNED COMMENT]
17:27
Code Monkey
Рет қаралды 1,3 МЛН
Unity Beginners - How to Loop the Background Image
9:24
Adamant Algorithm
Рет қаралды 26 М.
TOP 10 FREE NEW Assets DECEMBER 2024!
5:27
Code Monkey
Рет қаралды 8 М.
Escaping Unity Animator HELL
18:18
Lost Relic Games
Рет қаралды 517 М.
PAUSE MENU in Unity
12:13
Brackeys
Рет қаралды 997 М.
Evolving AIs - Predator vs Prey, who will win?
12:15
Pezzza's Work
Рет қаралды 2,9 МЛН