Unity 2D Moving Platform Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ธ.ค. 2024

ความคิดเห็น • 118

  • @Montigorable
    @Montigorable 4 ปีที่แล้ว +4

    For everyone who does not have their platform moving: Do exactly as he does, make empty gameobject(Moving_Platform), place platform inside of it and make pos1 and pos2 objects be children of an empty gameobject(Moving_Platform).

  • @FortbloxNET
    @FortbloxNET 2 ปีที่แล้ว +1

    if people have problems. think about it. check your player script. most player
    movement scripts use rigidbody velocity. if you press a button you change velocity on x or y. if you dont press a button you make velocity of x and y zero. you WRITE zero every frame. so no matter if you parent the player to the moving platform. you dont get keystrokes so you write velocity zero to the player and he doesnt move with the platform. you need to change your player script. instead of parenting you could call your playerscript from the moveing platform and set a BOOL like _isOnMovingPlatform true or false and then copy the velocity of the moving platform. ooooooooor. yes, you could parent it, and if its true _isOnMoving Platform then dont write velocity zero onto your player rigidbody etc.... just a few thoughts.

  • @calebgunner
    @calebgunner 3 ปีที่แล้ว +1

    Thank you.... I had to do a little adjustment to make it work in 3d but it works like a charm....

  • @ethanos6365
    @ethanos6365 3 ปีที่แล้ว +4

    why doesn't the player stick onto the platform for me

    • @Jarzykk
      @Jarzykk 3 ปีที่แล้ว

      Hey!
      Have you solved the issue?

  • @regejs
    @regejs 3 ปีที่แล้ว

    hey, i have a problem that my character doesnt move with the platform when standing on it, he keeps staying in the same play i land on

  • @darkergood5248
    @darkergood5248 4 ปีที่แล้ว +6

    My platform goes to pos1 and stops. What did I do wrong?

    • @maph420
      @maph420 3 ปีที่แล้ว +2

      try creating the pos gameobjects outside the original gameobject you are trying to move

    • @darkergood5248
      @darkergood5248 3 ปีที่แล้ว +1

      @@maph420 This is actually how I ended up fixing the problem back about a year ago, so credits to u for figuring it out!

    • @maph420
      @maph420 3 ปีที่แล้ว +1

      ​@@darkergood5248 HAHAH omg didn't see the date when I answered :p, anyway keeps being useful for new people with the same issue

    • @IndikaRecords
      @IndikaRecords 2 ปีที่แล้ว

      @@maph420 Yes, thanks a lot :D

  • @Alex-re3qm
    @Alex-re3qm 4 ปีที่แล้ว +5

    platform doesn't render I have collision but now actuall model shows up dispite one being in scene view no background layer is obstructing it

  • @techgod360
    @techgod360 5 ปีที่แล้ว +11

    Is there any problem here?
    Here is my code: (I think you need some fix)
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class MovingPlatform : MonoBehaviour
    {
    public Transform pos1, pos2;
    public float speed;
    public Transform startPos;
    Vector3 nextPos;
    // Start is called before the first frame update
    void Start()
    {
    nextPos = startPos.position;
    }
    // Update is called once per frame
    void Update()
    {
    if (transform.position == pos1.position)
    {
    nextPos = pos2.position;
    }
    if (transform.position == pos2.position)
    {
    nextPos = pos1.position;
    }
    transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    }
    private void OnDrawGizmos()
    {
    Gizmos.DrawLine(pos1.position, pos2.position);
    }
    }

    • @wiedzmiz2982
      @wiedzmiz2982 4 ปีที่แล้ว +3

      I fixed this script
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class MovingPlatform : MonoBehaviour
      {
      public Transform pos1, pos2;
      public float platformSpeed;
      Vector3 nextPos;

      void Start()
      {
      nextPos = pos1.position;
      }
      void FixedUpdate()
      {
      if (transform.position == pos1.position)
      nextPos = pos2.position;
      if (transform.position == pos2.position)
      nextPos = pos1.position;
      transform.position = Vector2.MoveTowards(transform.position, nextPos, platformSpeed * Time.deltaTime);
      }
      private void OnDrawGizmos()
      {
      Gizmos.DrawLine(pos1.position, pos2.position);
      }
      }

    • @yahyaaliahmed3466
      @yahyaaliahmed3466 4 ปีที่แล้ว +1

      @@wiedzmiz2982 what is a different thing?

    • @wiedzmiz2982
      @wiedzmiz2982 4 ปีที่แล้ว +2

      @@yahyaaliahmed3466 in void start i use nextPos = pos1.position; no nextPos = startPos.position;

    • @yahyaaliahmed3466
      @yahyaaliahmed3466 4 ปีที่แล้ว +2

      @@wiedzmiz2982 yea thank you. because startPos start empty

    • @zKingRox
      @zKingRox 3 ปีที่แล้ว +3

      @@wiedzmiz2982 ty i was having problems as well!

  • @ZMPedro
    @ZMPedro 4 ปีที่แล้ว +4

    So I have a problem with my moving platform. I followed the exact procedures shown in the video, and my platform does move, but for some reason it is invisible in my Game view. I tried changing the layers of the sprites, but it didn't work. Do you know how this can be fixed?

    • @jeremiahmiranda431
      @jeremiahmiranda431 4 ปีที่แล้ว +1

      check to see if your prefab has the transforms reset to 0's

    • @LuxxGD651
      @LuxxGD651 3 ปีที่แล้ว

      I have the same problem

    • @JTAGames
      @JTAGames 3 ปีที่แล้ว +1

      Check your z axis and make sure it's in front of the camera. If your object is behind your camera the layer wont matter.

    • @Fourzifer
      @Fourzifer 3 ปีที่แล้ว +1

      @@JTAGames This fixed it for me! I I was too caught up with the layers :,)

    • @swirlybulldozer3105
      @swirlybulldozer3105 2 ปีที่แล้ว

      this is happening to me as well

  • @incebaba2654
    @incebaba2654 2 ปีที่แล้ว +1

    nice tutorial liked

  • @Geckotr
    @Geckotr 3 ปีที่แล้ว +1

    when i add rigidbody2d to the platform it falls, how can i stop that?

    • @Geckotr
      @Geckotr 3 ปีที่แล้ว

      ok i fixed it. i just checked the constraint checkboxes of rigidbody2d

  • @guruprasanna4801
    @guruprasanna4801 2 ปีที่แล้ว +2

    I am completely new to game development, I followed brackeys 2d game series. my problem was the player keeps falling from the platform even after trigger, so added a new collider to the platform without trigger and set the rigid body gravity scale to zero. solved. I hope somebody find this helpful

  • @stevancosovic4706
    @stevancosovic4706 3 ปีที่แล้ว

    Thank you very much, you saved my day bro

  • @osmondchua1808
    @osmondchua1808 4 ปีที่แล้ว

    Finally! Been looking for a solution for hours! Thanks for this mate!

  • @janispranevskis2307
    @janispranevskis2307 2 ปีที่แล้ว

    I have a problem.. I'm pretty shore I followed up step by step but still my platform sins I press "play game" start's moving to the position 1 and stops there, and that's all..

  • @WeebiDash
    @WeebiDash 5 ปีที่แล้ว +1

    Hey, your channel has been really useful as you explain everything really well! Thanks for making these as they really help. Also, if you ever have the time I'd love to know how to make projectiles that you can shoot. In the game I'm making you have to throw spear like objects that can get stuck in walls and I'm really not sure how to do it. It's a bit complicated but it would be really nice to even just get a nudge in the right direction!

  • @sapy4124
    @sapy4124 4 ปีที่แล้ว +2

    it is working but player is changing its size automatically after moving off the platform.

  • @mailmike933m
    @mailmike933m 4 ปีที่แล้ว +2

    Can u make a 2d ladder tutorial?

  • @HardGam3r
    @HardGam3r 5 ปีที่แล้ว +1

    hello the script did not work here. I added the script to the platform and tagged it but my character keeps falling off the platform.

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      Hi Gamer,
      Is your character falling of the platform ? is the boxcollider attached properly to the platform ?

    • @ferrarif430fordgt
      @ferrarif430fordgt 4 ปีที่แล้ว

      @@xlaugts151 friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them ..
      how do I double jump from the video code .. thanks

  • @dekunub1012
    @dekunub1012 2 ปีที่แล้ว

    How can I use this if I am not using update function?

  • @hey_hey4394
    @hey_hey4394 4 ปีที่แล้ว +6

    me: peacefully creates a platform
    me 5 min later reading the comments: :OOOO

  • @FLStudioTutorialz
    @FLStudioTutorialz 3 ปีที่แล้ว

    Thanks for this great explanation. Keep it up.

  • @aliagha.huseynli
    @aliagha.huseynli 4 ปีที่แล้ว +1

    there is no other scripts on git hub project files :/

    • @xlaugts151
      @xlaugts151  4 ปีที่แล้ว

      Hey Aliagha, thank you for sharing with me. I will come back probable in a month with the channel and sort all these problems too. If you have any suggestion for a video let me know.
      Cheers

  • @lhalasantos9322
    @lhalasantos9322 3 ปีที่แล้ว

    Hi how can i use it for a new scene? It works for my main game scene but when I try to use it for a new scene it slowly moves, stops at pos1 then disappears.

  • @forestofsoundlp1365
    @forestofsoundlp1365 3 ปีที่แล้ว

    how do you have multiple platforms in a scene? everything physically works but one of the sprites follows the others movement despite the collider not being there

  • @Geckotr
    @Geckotr 3 ปีที่แล้ว

    transform coordinates of pos1 and platform are different when i try to do this. i set the x coordinate of pos1 to 5 but when i set the platform to 5 as well it goes to another position in the scene. Is this something about world position and the local position? I couldn't figure it out

  • @fedechaotix
    @fedechaotix 5 ปีที่แล้ว +7

    It didin't work for me :S

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      Hi Frederico
      Is there any error ?

  • @bobmcbob4399
    @bobmcbob4399 3 ปีที่แล้ว

    If you set the moving platform to be moving at player eye level and also across where idle player stands, this pushes the player. How about if you want player to remain still so that platform moves freely behind player layer without pushing the player?

  • @Roland_Luna
    @Roland_Luna 4 ปีที่แล้ว

    Great tutorial man! Keep it up :)

  • @ferrarif430fordgt
    @ferrarif430fordgt 4 ปีที่แล้ว

    friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them ..
    how do I double jump from the video code .. thanks

  • @RHCIPHER
    @RHCIPHER 4 ปีที่แล้ว +1

    thx so much

  • @Mr_Ginge_
    @Mr_Ginge_ 2 ปีที่แล้ว

    I am having issues with this. (error CSO117: 'Time' Does not contain a definition for 'deltatime')
    But that's not the only error.
    Its just filled with them.

    • @Alexshmalexx
      @Alexshmalexx 2 ปีที่แล้ว

      you have to capitalize it like deltaTime

  • @sorochinskkydm
    @sorochinskkydm 5 ปีที่แล้ว +1

    hey, i can stay on platform, but when i am starting to move form left to right, player moves like... 10fps, can you help me?
    this is my player script
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class playerCtrl : MonoBehaviour
    {
    public float speed;
    public float jumpForce;
    private float moveInput;
    private Rigidbody2D rb;
    private bool facingRight = true;
    public bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatIsGround;
    private int extraJumps;
    public int extraJumpsValue;

    private void Start()
    {
    rb = GetComponent();
    extraJumps = extraJumpsValue;
    }
    private void FixedUpdate()
    {
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    moveInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    if(facingRight == false && moveInput > 0)
    {
    Flip();
    }else if (facingRight == true && moveInput < 0)
    {
    Flip();
    }

    }
    void Flip()
    {
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
    }
    private void Update()
    {
    if(isGrounded == true)
    {
    extraJumps = extraJumpsValue;
    }
    if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    {
    rb.velocity = Vector2.up * jumpForce;
    extraJumps--;
    } else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
    {
    rb.velocity = Vector2.up * jumpForce;
    }
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
    speed = 10;
    }
    else if (Input.GetKeyUp(KeyCode.LeftShift))
    {
    speed = 5;
    }

    }



    }

  • @timo4024
    @timo4024 4 ปีที่แล้ว

    Nice video. My platform moves but I have one problem and I can't find out why it is like that. It doesn't detect that it is grounded if it is on the platform even though my script should work as it works at the horizontal moving platforms. This problem effects as well that the collider of my player doesn't work like it sould as it rotates instead of moving forwards

  • @Adem-rg1dv
    @Adem-rg1dv 3 ปีที่แล้ว

    Abi türk değilsin ama ben yine de yazayım, abi taşşaklarına kurban çok işime yaradı hiç bi yerde bulamamıştım eyw.

  • @mentionethan
    @mentionethan 5 ปีที่แล้ว +3

    Hey, thanks heaps for the tutorial but for some reason my platform just dissapears after a few seconds rather than moving. The box collider is still there and I can see it in the same place in the scene view but not in the game.

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      Does you scene has a background ??? It could be the layers, so sometimes the sprite of the platform is on a lower layer lvl than the background so the platform sprite wont show

    • @mentionethan
      @mentionethan 5 ปีที่แล้ว +1

      @@xlaugts151 When i started the game the platform was there then when it should have moved it disappeared, its fine now though i used a different method

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว +1

      @@mentionethan Good Job!

    • @SyahirahLee
      @SyahirahLee 4 ปีที่แล้ว

      @@mentionethan hey, mine happened the same thing as yours, do you think you could share how you solved it? thanks!

    • @mentionethan
      @mentionethan 4 ปีที่แล้ว

      @@SyahirahLee i would love to help but i can not remember what i did. Im pretty sure I found a different tutorial that did it slightly differently. Sorry I couldnt help

  • @sonradanfransiz
    @sonradanfransiz 3 ปีที่แล้ว

    Thanks, helped a lot!

  • @aqua6150
    @aqua6150 4 ปีที่แล้ว

    Thanks mate. Nice video.

  • @ZodakZach
    @ZodakZach 3 ปีที่แล้ว

    Plz explain your code my code is not the same as yours so when u put variables that u don’t explain I don’t know how to implement it into my code because you don’t explain

  • @thecynicalsprite1847
    @thecynicalsprite1847 5 ปีที่แล้ว +2

    this didn't help. mine just keeps going until it hits something and then stops. never goes back.

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      Hi Cynical,
      Check the positions, pos1 and pos2.
      If change direction only when hits something, it might be something with the pos1 and 2.
      Share the script and I will be able to help you.
      Thanks

    • @ferrarif430fordgt
      @ferrarif430fordgt 4 ปีที่แล้ว

      @@xlaugts151 friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them ..
      how do I double jump from the video code .. thanks

  • @edwardlau6937
    @edwardlau6937 3 ปีที่แล้ว

    the moving platform suddenly stop, in the video click the Moving_Platform gameobject , go to the transform position set z to zero

  • @СашаЕнот
    @СашаЕнот 4 ปีที่แล้ว

    It is work! Good Tutorial. Thanks you

  • @chrisc26frcrs76
    @chrisc26frcrs76 2 ปีที่แล้ว

    why the script didnt work with me? i've do all.

  • @Nothinginthere_78
    @Nothinginthere_78 3 ปีที่แล้ว +1

    its cool. thanks!!!

  • @wizzard8954
    @wizzard8954 3 ปีที่แล้ว +1

    i did everything like in the video but i had 22 errors

  • @swapty7860
    @swapty7860 5 ปีที่แล้ว +1

    I need help

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      I can help you, what's the problem ?

  • @shaycohen007
    @shaycohen007 5 ปีที่แล้ว +2

    great tutorials! if it's ok... as a part making 2d game, can you show how to make intro scene that the user ask to type his name so in the end of the level (in another scene) will be a massage "congratulations john you collect 5 coins".

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      Hi Shay,
      I will have a look about that, but I am sure it's not complicated.
      Thanks for the suggestion. :)

    • @memoal-said5111
      @memoal-said5111 5 ปีที่แล้ว

      Just use playerprefs. It'll save the users name once the user finishes typing on the input field (can be customized) or you can tell them to press a button or hit enter when their done.

    • @se9979
      @se9979 4 ปีที่แล้ว +1

      @@xlaugts151 how can i adapt that for a top down game? thx

    • @xlaugts151
      @xlaugts151  4 ปีที่แล้ว

      @@se9979 yes of course you can my friend :)

    • @se9979
      @se9979 4 ปีที่แล้ว

      Xlaugts how can I adapt it to a top down game? It's already moving, but I need my player to move with it, but your part for this is for a platformer?

  • @Sawdust_
    @Sawdust_ 2 ปีที่แล้ว

    thanks a lot!

  • @maph420
    @maph420 3 ปีที่แล้ว

    tysm really helpful! :D

  • @creeperfreaker3910
    @creeperfreaker3910 4 ปีที่แล้ว

    thanks, good video!

  • @owen7w7
    @owen7w7 2 ปีที่แล้ว

    thank you :)

  • @jeanbedry3941
    @jeanbedry3941 5 ปีที่แล้ว +1

    Very helpful thx !

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว

      Thank you for watching :)

  • @amadorgabriel_
    @amadorgabriel_ 5 ปีที่แล้ว +1

    Best Video, thanks

  • @luksay1767
    @luksay1767 4 ปีที่แล้ว

    Thank you

  • @ispyu1514
    @ispyu1514 4 ปีที่แล้ว

    Thank you!

  • @your_local_reptile6700
    @your_local_reptile6700 4 ปีที่แล้ว

    Mine just falls out the sky :s edit: it does work just not in the positions i told it to goes to random ones of its own accord. Edit again i fixed this by adding the script and collider to the parent instead of the child. I'm pretty sure you actually said to do this also xD also remember to turn gravity off

  • @varan9412
    @varan9412 5 ปีที่แล้ว

    public class MoveSnail : MonoBehaviour
    {
    public float speed = 1f;
    public int flip_cheak = 1; // если значение 1 объект переворачивается


    public Transform pos1, pos2, startPos;
    Vector2 nextPos;
    private void Start()
    {
    nextPos = startPos.position;
    }
    void Update()
    {
    if (transform.position== pos1.position)
    {
    nextPos = pos2.position;
    Flip();
    }
    if (transform.position == pos2.position)
    {
    nextPos = pos1.position;
    Flip();
    }
    transform.position = Vector2.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    }
    void Flip() // функция переворачивающая объект
    {
    if (flip_cheak == 1)
    {
    transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
    }
    }
    }

  • @tarsoltgmail
    @tarsoltgmail 2 ปีที่แล้ว +1

    THE CODE FROM VIDEO
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class platform : MonoBehaviour
    {

    public Transform pos1, pos2;
    public float speed;
    public Transform StartPos;
    Vector3 nextPos;
    void Start()
    {
    nextPos = StartPos.position;

    }
    // Update is called once per frame
    void Update()
    {

    if (transform.position == pos1.position)
    {
    nextPos = pos2.position;
    }
    if (transform.position == pos2.position)
    {
    nextPos = pos1.position;
    }
    transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    }
    private void OnDrawGizmos()
    {
    // Gizmos.DrawLine(pos1.position, pos2.position);
    }


    }

  • @MaulLerGamer
    @MaulLerGamer 5 ปีที่แล้ว +1

    Hey thanks

    • @xlaugts151
      @xlaugts151  5 ปีที่แล้ว +1

      Hey, Thank you!!

  • @rameoparmon1222
    @rameoparmon1222 5 ปีที่แล้ว +1

    Nice

  • @anicepineapple9067
    @anicepineapple9067 4 ปีที่แล้ว

    Didn't work for me :/

  • @arczio3226
    @arczio3226 4 ปีที่แล้ว

    Thanks you

  • @lamadoutnet
    @lamadoutnet 3 ปีที่แล้ว

    my platform is going down

  • @dilshatabdrashid1527
    @dilshatabdrashid1527 3 ปีที่แล้ว

    👍

  • @g-dev7336
    @g-dev7336 4 ปีที่แล้ว

    thank U 🌷

  • @beri4138
    @beri4138 4 ปีที่แล้ว

    This is not a good tutorial. Your code is bad.
    You are checking whether or not the platform's position is exactly equal to pos1 or pos2.
    In 99% of cases, the platform will never be at exactly that position. Since you're using MoveTowards, the platform will overshoot its destination, and then overshoot it by the same amount in the other direction, endlessly.

    • @beri4138
      @beri4138 4 ปีที่แล้ว

      @Hermes Belmont I literally did

    • @zKingRox
      @zKingRox 3 ปีที่แล้ว

      @Hermes Belmont i mean they kinda did

    • @ZodakZach
      @ZodakZach 3 ปีที่แล้ว

      It’s a vector 3 u can’t use >

  • @user-si8vn9jd3w
    @user-si8vn9jd3w 4 ปีที่แล้ว

    thank you