Unity: 2D Moving Platforms

  Рет қаралды 31,985

Root Games

Root Games

Күн бұрын

Learn how to move platforms in any direction and how to move the player with them.
See how to upgrade your platform here:
👉 • Unity: 2D Platform Upg...
✅ Get my courses with discount:
👉 Unity 2D Master: www.udemy.com/course/unity2dm...
👉 Unity Mobile Course: www.udemy.com/course/unitymob...
👉 Unity Input System with Rebinding: www.udemy.com/course/unity-in...
👍 Save your time and learn Unity faster
-------------------------------------------------------
🔔 Subscribe for more awesome content:
🔴 / @rootgames
-------------------------------------------------------
🟡 Support this channel through donations
Any amount means a lot and will help grow the channel:
👉 PayPal: www.paypal.com/paypalme/rootg...
-------------------------------------------------------
Social Networks:
⭐ Instagram: / rootgamesofficial
⭐ Twitter: / rootgamescro
⭐ TikTok: www.tiktok.com/@rootgamesoffi...
⭐ Facebook: / root-games-10337038860...
-------------------------------------------------------
Timestamps:
0:00 Intro
0:11 Moving Platform
2:28 Moving the Player with Platforms
#unity2d #moving #platform #unitytutorial #rootgames

Пікірлер: 127
@RootGames
@RootGames 2 жыл бұрын
See how to UPGRADE your platform here: 👉 kzbin.info/www/bejne/pojLZnpnbLqGa7c
@Pedro_Marangon
@Pedro_Marangon 2 жыл бұрын
One thing to add for the parenting the player to the platforms is that, depending on the height of your platform, the player can touch it on the side or bottom and still be on ground, which means he'll be a child of the platform the same way. To fix that, you just need to check if the object that touched the platform is coming from a superior Y coordinate, like this: private void OnCollisionEnter2D(Collision2D collision) { if(collision.transform.position.y > transform.position.y) { collision.transform.SetParent(transform); } }
@RootGames
@RootGames 2 жыл бұрын
Yes, good tip! 😉
@DangoGang
@DangoGang 2 жыл бұрын
eres como dios pero sin serlo por eso rene es suwako
@MiguelRodriguez-ff9wl
@MiguelRodriguez-ff9wl Жыл бұрын
Shouldn't it be SetParent(collision.transform) ?
@capnnemo5330
@capnnemo5330 Жыл бұрын
Or you can use platform effector 2d
@jansmix6033
@jansmix6033 Жыл бұрын
I love it, especially since the script for player staying on the platform is within the platform script to keep it simple (unlike in tutorials) so it works with different objects as well!
@alespozar1075
@alespozar1075 2 жыл бұрын
Man I am so happy this showed up in my recommended. Love your videos.
@RootGames
@RootGames 2 жыл бұрын
It's nice to hear that, thank you :D
Жыл бұрын
Simple and to the point. Thanks!!
@gabrielsouza9651
@gabrielsouza9651 2 жыл бұрын
Really awesome tutorial, simples and highly effective! Congrats and thanks
@RobertoDYanis
@RobertoDYanis 2 жыл бұрын
thx man it was a great help for player mouvement !
@joaoviniciussantos7106
@joaoviniciussantos7106 Жыл бұрын
thanks man, it really helped me, you are a true legend
@personalgamedevyt9830
@personalgamedevyt9830 7 ай бұрын
Thank you for the concise tutorial. I realized I hadn't already commented and given thanks so I wanted to do so now as I was referencing my demo from your video.
@RootGames
@RootGames 7 ай бұрын
Thank you, I appreciate it 😉
@judahowens6040
@judahowens6040 2 жыл бұрын
amazing tutorial, thank you!
@UnknownJigsaw
@UnknownJigsaw 2 ай бұрын
Thank you really helps the game run more smooth and such a simple solution!
@RootGames
@RootGames 2 ай бұрын
Glad to help!
@coalcooler666
@coalcooler666 3 ай бұрын
Amazing tutorial Thank you so much!
@TheSlashpark
@TheSlashpark Жыл бұрын
Great tutorial! Thank you
@nawfalnjm5699
@nawfalnjm5699 2 жыл бұрын
Thank you . finally i fixed the problem . your video is very very good !
@RootGames
@RootGames 2 жыл бұрын
Glad you fixed it, thanks 😉
@dcry1003
@dcry1003 2 жыл бұрын
i was having a hard time moving my player along with my platform! thank you very much for the solution!!
@RootGames
@RootGames 2 жыл бұрын
Glad to help! 😉
@dcry1003
@dcry1003 2 жыл бұрын
@@RootGames soo uhh.. ive been tinkering with the moving platforms in my game and uhh something happen where whenever my player enters the moving platform going sideways *except left to right up and down* it always change my player's shape and size *since the code is link to my player's transform thats probably why* oh and my player is just a standard 2D box sprite gameobject any tips on how i can fix it?
@RootGames
@RootGames 2 жыл бұрын
It’s hard for me to say something blindly, didn't quite understand you well. In this tutorial, we are not doing anything with the player's transform other than setting the parent. Any changes to transform must be somewhere else in your code.
@alok8744
@alok8744 2 жыл бұрын
Thank You😁
@darkstorm4378
@darkstorm4378 2 жыл бұрын
Dude thank u so much
@faizmohd732
@faizmohd732 Жыл бұрын
Thanks Man
@doctorsuper
@doctorsuper 2 жыл бұрын
Great tutorial! You deserve much more subscribers than you have!
@RootGames
@RootGames 2 жыл бұрын
Thanks! With time, people will come. More cool tutorials coming 😉
@doctorsuper
@doctorsuper 2 жыл бұрын
@@RootGames Awesome!
@matijadokmanovic8226
@matijadokmanovic8226 11 ай бұрын
BEST DAMN VIDEO ON THIS TOPIC!
@RootGames
@RootGames 11 ай бұрын
Thank you! 🤝🏻
@IAMMAT87
@IAMMAT87 Жыл бұрын
Thank you
@highscore4866
@highscore4866 3 жыл бұрын
Hey that's pretty guuud :)
@richardestes6601
@richardestes6601 2 жыл бұрын
If anyone is having trouble with your character sticking to the moving platform and being unable to move/jump while on it, try checking to see if there's input along the horizontal axis (or however your controller may be set up) and then setting your player's parent to null. For example, here's my check right before I call my Move() method: If (Input.X != 0) gameObject.transform.SetParent(null);
@argisnutz
@argisnutz Жыл бұрын
thx very much!
@jonaswunsch2617
@jonaswunsch2617 Жыл бұрын
if you want to stick to the platform again after moving your charakter you need to add to the Moving Platform Script: void OnCollisionStay2D(Collision2D collision) { collision.transform.SetParent(transform); }
@nahnot6247
@nahnot6247 9 ай бұрын
Theres no Input.X. Im very confused
@robotest4115
@robotest4115 2 жыл бұрын
thanks
@rubabmubarrat
@rubabmubarrat 11 ай бұрын
Danke
@bobbville
@bobbville 2 жыл бұрын
Hey I'm making a dev log also and I just gotta say, your thumbnails are🔥🔥🔥🔥
@RootGames
@RootGames 2 жыл бұрын
Thanks :)
@timetorelaxfocus9642
@timetorelaxfocus9642 2 жыл бұрын
Very cool. How would you make the movingplatform move in a circle or even rotate on it's own axis? Thanks
@RootGames
@RootGames 2 жыл бұрын
You can use functions that spawn objects (transforms) in a circle and then pass that objects as transform points to the array. Or you can manually create transforms and place them in a circle. For rotation use function transform.Rotate.
@timetorelaxfocus9642
@timetorelaxfocus9642 2 жыл бұрын
@@RootGames Appreciate it.
@JovsValorant
@JovsValorant 3 жыл бұрын
Upload more 2d tutorials. I subscribed
@RootGames
@RootGames 3 жыл бұрын
Usually I upload once a week, sometimes two times.
@zarlaart640
@zarlaart640 2 жыл бұрын
If i want to make multiple platforms like the same 1st one moving I can just duplicate it right? Or should I drag and make it a prefab first?
@RootGames
@RootGames 2 жыл бұрын
Make a prefab so you can use it in other levels as well ;)
@zarlaart640
@zarlaart640 2 жыл бұрын
Awesome ❤
@Sheldi
@Sheldi 2 жыл бұрын
Best tutorial
@RootGames
@RootGames 2 жыл бұрын
Thanks
@Sheldi
@Sheldi 2 жыл бұрын
@@RootGames I have one problem, I want to make the platform change direction after touching a point. Any tips?
@RootGames
@RootGames 2 жыл бұрын
@@Sheldi Don't quite understand what you need to do, but for collision between points and the platform you need to add colliders/triggers to the points and go from there
@veryvella
@veryvella 2 жыл бұрын
thnx
@MitkoD
@MitkoD 2 жыл бұрын
Nice video! Thank you for this tutorial! I couldn't understand why do we need Rigidbody2D for the platform as we use its Transform component to move it?
@RootGames
@RootGames 2 жыл бұрын
Because every object that moves and have a collider needs to have a Rigidbody2D component. Otherwise Unity will consider that object as static object. Unity doesn't expect that static objects move and then it needs to render the whole physics scene every frame (leads to performance drops)
@MitkoD
@MitkoD 2 жыл бұрын
@@RootGames Thanks a lot for your answer and for sharing your knowledge!!
@alijandaqi9019
@alijandaqi9019 2 жыл бұрын
tnx
@RootGames
@RootGames 2 жыл бұрын
Feel free to subscribe so you don't miss future tutorials :)
@rob6508
@rob6508 Жыл бұрын
nice tutorial! quick question though. Is there a way to make it so that the platform only moves when the player is on top of it?
@RootGames
@RootGames Жыл бұрын
When the player enters the trigger, set the speed of the platform to some number. When the player exits the collider, set the speed to 0.
@MyMusic-pi6lz
@MyMusic-pi6lz Жыл бұрын
It shows the error An object reference is required for the non-static field, method or property 'Collision.transform'... Plz help 😭
@paramecium_sp762
@paramecium_sp762 2 жыл бұрын
Hello i have a problem with my character when standing on top of the platform. Everything works fine, the code works and my player character is moving with the platform but the player character is jittering and spazzing out when standing on the platforms, both horizontal and vertical moving platforms. I've tried changing the interpolate setting from none to Interpolate and then Extrapolate but still the player jittering/stuttering. I've followed your code exactly, can you or anyone help me solve this?
@Krobelux
@Krobelux Жыл бұрын
You've probably already figured this out, but I've learned a lot of the jittery-ness is usually from the Rigidbody 2D - Collision Detection being set to discrete by default. Change this to Continuous (perhaps change it for each object) and that may solve your issue.
@Chilly_M04
@Chilly_M04 Жыл бұрын
Great tutorial, but for some reason my player is jittering when moving in the opposite direction of the platform while standing on it. Is there any solution?
@RootGames
@RootGames Жыл бұрын
Try to change the interpolation settings on player's rigidbody.
@bosong7183
@bosong7183 2 жыл бұрын
将玩家对象设置成移动平台的子对象,有一个问题是,它会随着移动平台的缩放而改变大小,就是说要确保移动平台的scale为vector3(1,1,1),这是我之前做过的例子得到的结果。(虽然不知道你能不能看得懂。。)
@RootGames
@RootGames 2 жыл бұрын
谷歌翻译理解。是的,你应该小心,不要改变平台的转换。
@VEOdev
@VEOdev 6 ай бұрын
If anyone have a problem that the player have jittery movement in the platform, you have to change your interpolation in the player rigidbody to "none"
@snowhornet0982
@snowhornet0982 8 ай бұрын
I have a one way collision on my platform that stops working when i add this script.
@IM-ws5if
@IM-ws5if Жыл бұрын
Everything is great but I have a problem when a player wants to jump from the platform the player's appearance changes
@InternationalCheeseDude
@InternationalCheeseDude 2 жыл бұрын
how do u add the points, do u use spheres?
@RootGames
@RootGames 2 жыл бұрын
Empty gameobjects 😉
@RootGames
@RootGames 2 жыл бұрын
Select the empty game object and in the Inspector change the icon to sphere.
@InternationalCheeseDude
@InternationalCheeseDude 2 жыл бұрын
@@RootGames thx, i was wondering why it wasn't working
@hfucc0324
@hfucc0324 2 жыл бұрын
Hello i have a problem with scale cause my platform's scale is not equal to player's one. So whenever tha player steps on, it's scale will be changed. Can you help me pls? thank youu
@RootGames
@RootGames 2 жыл бұрын
It's a bit tricky when you change the scale of the platform because the player becomes a child of that platform. However, by default, when you jump on the platform, the scale shouldn't change. Probably it happens if you use transform.localScale.x in your Flip function (the function in which you flip the player). To avoid that, try to flip the player with rotation. You can use these two functions: transform.rotation = Quaternion.Euler(0, 0, 0); transform.rotation = Quaternion.Euler(0, -180, 0);
@hfucc0324
@hfucc0324 2 жыл бұрын
@@RootGames That's really nice. Thank you so much
@KDRchicken
@KDRchicken 2 жыл бұрын
@@RootGames Thanks alot, I was running into the same problem :)
@editsforevery1
@editsforevery1 2 ай бұрын
The platforms don't seem to move but only move when I touch them. It just gets stuck on one point.
@DangoGang
@DangoGang 2 жыл бұрын
¡Gracias! Al carapapa de JoexScript no le entendía ni un poco.
@aqa222
@aqa222 Жыл бұрын
I have a problem with my character not sticking to the platform
@Apebek
@Apebek 2 жыл бұрын
I am new to Unity and confused. I read that you should always use rigid bodies on moving objects and use FixedUpdate. Also that you should us velocity or movePosition instead of transform. I see everybody use update function and transform. I got the same result on a dynamic rigdbody but my only problem is that the platform sinks when the player is standing on it. Does anybody have some tips?
@RootGames
@RootGames 2 жыл бұрын
We use fixed update when we move objects using rigidbody component in the script, for example rb.velocity= something. If we move objects using transform, then use update. Your platform sinks because the rigidbody is not kinematic. Select your platform and set the rigidbody to kinematic type.
@Apebek
@Apebek 2 жыл бұрын
@@RootGames Thank you so much!
@highscore4866
@highscore4866 3 жыл бұрын
I subbed
@RootGames
@RootGames 3 жыл бұрын
Thanks 😀
@highscore4866
@highscore4866 3 жыл бұрын
@@RootGames 👍
@bordexeditor5842
@bordexeditor5842 2 жыл бұрын
When i am trying to jump of the platform, my player doesnt, why ? (i use velocity as movement in my PLAYER script, he can move on the platform well but cannot jump)
@RootGames
@RootGames 2 жыл бұрын
It has to do something with your jump script or layers. Make sure that platforms are on the correct layer (Ground or something).
@MiguelRodriguez-ff9wl
@MiguelRodriguez-ff9wl Жыл бұрын
@@RootGames I have the same issue, when the platform is moving upwards, the player cannot jump, but as soon as that platform goes down ward the player can jump. I tried fixing with oncollisionstay2d() if input is jump then setparent = null and increase the jump speed by accessing the players jumpspeed variable via collision.gameobject.getcomponent().jumpspeed = 10f for example, it looks like it works but it still feels janky and can use some fine tuning. I just can't figure out why it won't let me jump when going upwards
@panagiotiskotsias4496
@panagiotiskotsias4496 Жыл бұрын
but my character doesn't have a rigidbody2D what else can i do so it moves with the platform
@RootGames
@RootGames Жыл бұрын
Every moving object that has colliders should have a rigidbody. Otherwise Unity will consider that object as static. Furthermore, even if you enable/disable/destroy objects with a collider, they should have a rigidbody component. Try to explore unity physics and rendering physics workflow in the documentation.
@panagiotiskotsias4496
@panagiotiskotsias4496 Жыл бұрын
@@RootGames I just added a rigidbody2D to my player object and put kinematic on it and it works fine :)
@ray2dasherthedragon336
@ray2dasherthedragon336 Жыл бұрын
For some reason my platform just keeps going to the left and doesn't go to the second point and back Edit: Nvm i fixed it I just did the mistake at the start where I made the platform itself the parent object instead of a empty object
@Squilllo34
@Squilllo34 Жыл бұрын
What tod o when it jitters?
@MiguelRodriguez-ff9wl
@MiguelRodriguez-ff9wl Жыл бұрын
try changing Update() to FixedUpdate()
@Squilllo34
@Squilllo34 Жыл бұрын
@@MiguelRodriguez-ff9wl thanks
@markallancarba5055
@markallancarba5055 2 жыл бұрын
When I stick to the platform my character will scale on thin. How to fix it ?
@RootGames
@RootGames 2 жыл бұрын
You must have the platform's transform scale to (1,1,1 or 0). If you change the scale, the player will also change the scale.
@IM-ws5if
@IM-ws5if Жыл бұрын
try move in the platform and you well see the size changed of your player
@RootGames
@RootGames Жыл бұрын
You probably have the size of your platform different from 1. That is the core of the problem.
@MohamadNurjaman
@MohamadNurjaman Жыл бұрын
i followed the tuorial, but not working on me the platform just falling down and bouncing
@RootGames
@RootGames Жыл бұрын
Make sure you use a kinematic rigidbody.
@WhiteGreenBlueBlack
@WhiteGreenBlueBlack Жыл бұрын
Great!! But, when I jump from the platform it automatically moves to another position. Like teleporting. 😞
@RootGames
@RootGames Жыл бұрын
Follow the tutorial carefully. You missed something, this works 100% 💪
@WhiteGreenBlueBlack
@WhiteGreenBlueBlack Жыл бұрын
@@RootGames I found the problem, is the scale and the flip. Particulary the flip, I still don't resolve that problem. When my player is facing left all works fine. But when it face right over the platform and then I jump the posX changes the sign, like a 180 degrees flip around the Y axis. I tried two methods for flipping: 1. transform.Rotate(0f,180f,0f); 2. transform.localScale = new Vector2(-1f, 1f); //or (1f,1f). In both cases it behaves the same but only on the platform, on the ground everything goes nice. Do you have an idea? Or maybe see the mistake?
@WhiteGreenBlueBlack
@WhiteGreenBlueBlack Жыл бұрын
@@RootGames Solved, but what a way... I separate the sprite from the game object. So, I created a GameObject called player, I put the RigidBody, Collider and Script in this. Inside the Player I put the sprite, this Sprite only have the SpriteRenderder and the Animator. Then, I passed the sprite GameObject to the Player script, to make flips and animations. All this sounds inusual, because what I have seen in many tutorials is just a GameObject with the SpriteRenderer component, maybe there is a better solution. But now it rotates around itself when jump from platform and facing right. Anyway, if you know a better way please tell me. 😅
@RootGames
@RootGames Жыл бұрын
Strange, it should work just with one GameObject. I can't really much help you in YT comments, the core of this problem is somewhere but great if you managed to work around it!
@InternationalCheeseDude
@InternationalCheeseDude 2 жыл бұрын
the part gets stuck on the right part thingy
@RootGames
@RootGames 2 жыл бұрын
check the platform upgrade tutorial
@xDTHECHEMISTx
@xDTHECHEMISTx 2 жыл бұрын
the platform is fine moving but from that small portion of code here Nothing is happening at all. My character is still sliding off. is there something more in this video not shown? And here is the exact code i put at the bottom of the platformer function script public void OnCollisionEnter2D(Collision2D other) { other.transform.SetParent(transform); } private void OnCollisionExit2D(Collision2D other) { } -what is "Other" supposed to be? for the Player Object itself? does anything have to match? Or that can be named anything? -when you just put the code that way simplified that way and showed then showed the result it just attached no problem. It didnt happen that way for me at all. You dont have a GitHub for Public studying purposes of these kind of scripts and example projects?
@RootGames
@RootGames 2 жыл бұрын
There is nothing more outside the video. Standing on the platform should work with a few lines of code just as showed. In case you missed something: 1. OnCollisionEnter2D and OnCollisionExit2D must be placed in the script of the moving platform 2. public void OnCollisionEnter2D(Collision2D other) { other.transform.SetParent(transform); } -in your example, "other" is the object that collides with the platform, in this case the player. Also no need to make Collision functions public. Make it private. 3. Make sure that the platform and the player are on the correct layers so that collision is detected and they can collide/touch properly
@xDTHECHEMISTx
@xDTHECHEMISTx 2 жыл бұрын
@@RootGames ah i confused you with a different KZbinr i see. You two had similar tutorials and i had the same issue it seemed like. My apologies. You had collision. he had "Other". So then ill ask you then, when you have it like collision.transform.SetParent(transform); is the "Collision" expression there representing something? Is it connected to the player in any way. i was trying to spot what is it connected to but i was thinking it was supposed to be the object "Player". so there is some confusion with that for me. overall i got a bunch of stuff working but this platform stability issue is the oddest annoying thing now.
@RootGames
@RootGames 2 жыл бұрын
It's just a name for the object that collides with the platform. You can name it as you wish. Everything that collides with the platform becomes "Collision", you can get data from that Collision (GetComponents, manipulate objects through that, and other stuff). That being said, for the platform, it's good to compare tag and check that you use SetParent only when the player collides (use "if" statement and compare tag to check if the player collided, then use SetParent)
@xDTHECHEMISTx
@xDTHECHEMISTx 2 жыл бұрын
@@RootGames Ah ok. That compare tag makes sense. thats what i saw in another method also. ill follow up with that and see what my results are. Thanks
@xDTHECHEMISTx
@xDTHECHEMISTx Жыл бұрын
I got my Platform issue working now. It works perfect now. what i realized ultimately it was my characters ground collider. I had to adjust that better and then Make his rigidbody settings to discrete, start awake and none. And then it all started working fine no issues with standing on horizontal and vertical moving platforms
@topias7436
@topias7436 2 жыл бұрын
It works, but when i try to move on the platform, my player moves very slowly
@topias7436
@topias7436 2 жыл бұрын
Changed Update() to FixedUpdate() and now it works
@RootGames
@RootGames 2 жыл бұрын
Nice 🙂
@supa6964
@supa6964 2 жыл бұрын
I have the same problem. I tried changing Update to FixedUpdate, but then the platform block and camera looked really bad and choppy. Any other solutions? *Edit* I found another potential solution. On the Player's Rigidbody, there is an option regarding interpolation. You can set that to Extrapolate and it works. From what I read on unity3dtips.com, it says, "Setting the rigidbody to interpolate will make the rigidbody act similarly to a normal lerp, it’ll lag behind the wanted position more if the rigidbody is moving fast. Extrapolation predicts movement based on the current velocity of the rigidbody which allows it to keep up when the rigidbody is moving fast. However extrapolation can result in a mild ‘rubber banding’ effect where if the rigidbody hits a collider at high speed it may appear to clip inside or through it for one frame until the rigidbody realizes that the collider is there in the next frame." Hope this is helpful!
@RootGames
@RootGames 2 жыл бұрын
Yes, the solution is to change interpolation. Usually Extrapolate works better for me. This is something I talk about in my Unity 2D Master Course, so please check it out. Maybe it can help you :)
@kapcapkapcap6104
@kapcapkapcap6104 4 ай бұрын
1:02
Unity: 2D Platform Upgrade
1:47
Root Games
Рет қаралды 4,1 М.
Improve Your Platformer with Forces | Examples in Unity
7:01
Dawnosaur
Рет қаралды 164 М.
Заметили?
00:11
Double Bubble
Рет қаралды 3,1 МЛН
Cute Barbie Gadget 🥰 #gadgets
01:00
FLIP FLOP Hacks
Рет қаралды 35 МЛН
Can you beat this impossible game?
00:13
LOL
Рет қаралды 52 МЛН
UFC 302 : Махачев VS Порье
02:54
Setanta Sports UFC
Рет қаралды 1,3 МЛН
How I learned Unity without following tutorials (Developing 1)
18:11
Game Maker's Toolkit
Рет қаралды 1,9 МЛН
MOVING PLATFORM  2D | How to make moving platform in Unity ?
6:01
Rehope Games
Рет қаралды 13 М.
Unity 2D Moving Platform Tutorial
6:59
Xlaugts
Рет қаралды 59 М.
SCRIPTABLE OBJECTS and EVENTS in Unity
6:46
Root Games
Рет қаралды 2,8 М.
How much MONEY my Game made (and how much it cost!)
14:00
Code Monkey
Рет қаралды 153 М.
How to Create 3D Moving Platforms (Unity Tutorial)
16:31
Ketra Games
Рет қаралды 31 М.
How to Start Gamedev in 2024
10:28
Sasquatch B Studios
Рет қаралды 366 М.
How Do Unity 2D JOINTS WORK And Which One To USE?
10:26
Freedom Coding
Рет қаралды 6 М.
Заметили?
00:11
Double Bubble
Рет қаралды 3,1 МЛН