Grid-based Movement using Tilemap and Input System - Unity Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

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

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

    3:45 Instead of enabling the XYZ constraints on the Rigidbody2d you can simply change the, "Body Type" property to "Static" instead of "Dynamic".

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

    Been combing through various 'old' tutorials on grid movement before the new input system, which I didn't know existed before seeing your videos, and your tutorial has felt more instructive.
    Don't know where you are in your TH-cam journey, but don't change a thing about your informative and descriptive style. Got yourself a new, solid subscriber.

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

    I have been struggling to find up to date tutorials. Finally found your channel after much googling around... I've subbed and plan to start consuming the backlog. :D

  • @reshirom2009
    @reshirom2009 4 หลายเดือนก่อน

    I can't believe that it took me five videos just to find one that uses the Tilemaps namespace, thank you.

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

    OH MY GOSH I HAVE NEVER FOUND A SOLID FIX TO THE LINES AND ALL OF A SUDDEN YOU GET IT DONE FIRST 5 MINUTES! I love you. Easily the best tutorial ever

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

    Very very nice, although I think in this case(I haven't tested this) you don't need colliders on the col layer since you are already checking if there is a tile on that cell, and not checking if that cell has a collider.

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

    I absolutely love your tutorials, and explanations! I have used your vids as reference many times so far on my journey! Just wanted to say a heartfelt thanks, and I wish i could subscribe more! Keep it up, and im very sure you will be up there with all the bigger unity content creators as soon as that yt algorithm catches on!

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

      Totally agreed

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

      Thank you so much!! I'm really glad they are helpful and enjoyable :)

  • @bro-rubro
    @bro-rubro ปีที่แล้ว

    your content is simply amazing, love everything
    please leave it online forever cuz this is gold

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

    I literally cried after it started working from following your tutorial. I tried other tutorials but they always tended to leave out certain details where you pretty much had to guess what they did in between.

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

      😁

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

    Another excellent tutorial. Really helped me because I imported my maps from Tiled and didn't know how to put collisions on them so this really helped me out.

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

      Awesome!

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

    Awesome tutorial as I start out in this. Just a suggestion: When you include Tilemap in the title but dont actually go into (even if it's just a quick show) of actually creating/painting the map you use in the tutorial and instead refer to another video, it breaks the momentum a bit. Especially for someone new who starts this tutorial but then has to jump into another one in order to continue. Not a negative at all and since you already had the video, I can see why it made sense to link to that but if you could even briefly show something here, it would've helped with the flow.
    Again, thanks for the amazing tutorial and it's really nice to see new/updated tutorials vs super old ones.

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

      Great point. I wanted to keep this video scoped and concise but I’ll make sure to keep that in mind.

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

      @@samyam Thanks for replying! I've been going through a lot of your tutorials today and am learning a lot!

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

    Exactly what I was looking for - super easy to understand. Thanks!

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

    Instead of "Freeze Position X, Y " and "Freeze Rotation Z" on
    RigidBody 2D you can change "Body Type" from Dynamic to Static.

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

      That works as well! Good catch

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

    Oh my god thank you so much for this tutorial. I was struggling to get this to work for a while

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

    Great job, and well explained. Thanks for your useful video. 10/10

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

    lowkey helped me in the miz jam im working on thank you!

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

      Good luck on the jam!

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

      samyam thank you!

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

    Great work! Really well explained.

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

    Awesome tutorial, really love the way you explain ^^

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

    Very cool :O
    I had no idea you could use the tilemap to move the character in that way!

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

    I'm one year later but this is an awesome tutorial. I just have a silly question. Why do you need colliders if you block the player movement by code? is it because it would be useful further ?

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

    Thank you for making this tutorial! I love how easy it is to understand and follow along; it's reminiscent of Brackeys' tutorials back when he was active on TH-cam.
    I know this tutorial is primarily for 2D top-down games, but do you think it is possible to implement grid based movement in a 3D game (such as in a first person dungeon crawler)? Naturally there would need to be some changes to the code, but it seems that it wouldn't be too difficult to switch the Y-axis to the Z-axis so that one could move forward, backward, left and right. What are your thoughts on such an approach?

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

      Yeah that could work, you might have to build your own grid system to support 3D tiles but that shouldn’t be too bad! Code Monkey has an excellent tutorial on making a custom grid system.

    • @dark31521
      @dark31521 4 หลายเดือนก่อน

      @Imagino1234
      Can you tell me how you handled it? I really want to know. Thank you
      it's my code,but it didn't work and my Orientation set to "ZX"
      Vector3Int gridPosition = ground[0].WorldToCell(new Vector3(transform.position.x + direction.x, 0, transform.position.z + direction.z));

  • @BonkerzGamer119
    @BonkerzGamer119 3 หลายเดือนก่อน

    this is a great tutorial but im struggling to convert the code to work if i hold down a key to keep moving. any advice, the doucmentation confuses me.

  • @saviocallado5045
    @saviocallado5045 ปีที่แล้ว

    THANKS

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

    Howdy. Just wanted to say great tutorial! I noticed that your "Making a 2D Level in Unity with the Tile Palette" link actually points to your InputSystem tutorial. Regards

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

      Oops! Thanks for the heads up 🙂

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

    I was literally saying I was going to give up on getting this type of movement and then I see this tutorial uploaded only 3 days ago, what a Godsend. Question though, how would I tinker with this to make it function on a grid with larger cell sizes? Like a 3 x 3 grid with rectangular cells, how would I get the character to the middle of a cell on the grid?

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

      You can move the character by 3 instead of 1 in the controller.
      Here's also a handy function:
      docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.GetCellCenterWorld.html

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

    it's a good material to grasp the basics of grid movement in unity using tilemaps,
    however the only thing that bothers me a lot in this tutorial (not only in this tutorial but in most tutorials in general) is that the solution doesn't scale well.
    there's too much resposability in the player controller, it handles input, it checks for available positions in the tilemap, it moves, this solutoin will get wild when implementing more entities for the map.
    I know it's supposed to be simple because it's a tutorial.
    As I said it's a good material. keep it up

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

      I recommend having an input manager where you instantiate the controls there and just read them in the player. You can also have a tilemanager with a list of tilemaps that checks whether you can move there (like a helper class). But as you said this is meant to be a simple tutorial to get you started.

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

    Will you possibly make a tutorial on grid movement, while holding down the movement input? Or rather it continues to move to the next grid cel while holding after waiting an amount of time, instead of going off to space. :P I can't find anywhere how I'd adapt this to do that.

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

      You could do something in a coroutine which you would start when the input what started being pressed (instead of the performed callback they have a started and also a canceled callback)
      Waitforseconds seconds = new Waitforseconds(1f)
      while (Input.readvalue() is not Vector2.zero)
      Move()
      . Yied return seconds ; // wait one second
      You can also keep a reference to the coroutine and call the stopcoroutine in the canceled callback
      Let me know if that helps!

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

      @@samyam Interesting, however I'm not sure how to get the player's input into the coroutine function. Pass a reference? Also, I can't seem to find out how to use the started or canceled callbacks correctly.

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

      The started and canceled are the same as performed you just switch the name. And you do controls.actionmap.action.ReadValue()
      To get the input value in the current frame. I have several other videos using the input system, if you'd like to check it out th-cam.com/play/PLKUARkaoYQT2nKuWy0mKwYURe2roBGJdr.html

  • @jaytwist8738
    @jaytwist8738 5 หลายเดือนก่อน

    How would I handle this if my sprites are taller than wider?

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

    Very awesome and informative video! :D Do you have any tips for animating the player when using this system? I can't seem to get my animations to play at all

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

      Hmm the animations don’t play, how are you calling it? The animation system should work the same and be called the same - anim.Play(“”) etc.

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

    I need help with the sorting layers, the option for Rendering Layer Mask isn't there for me and the ground and collision keep appearing over my character than underneath. The movement and everything else works, it's just solving how to sort the layers I'm stuck on.

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

      Hi, did you set the sorting layer and order in layer in the Tilemap Renderer component? The higher the number the more on top a tile map will be, so you’d use a higher number for your player as an example.

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

    Thanks for sharing! Question, do you need to explicitly unsubscribe from events or do they automatically clean up at some point?

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

      c# has a garbage collector built in, but it’s always good practice, especially when disabling scripts and re-enabling them will cause you to subscribe twice and will have the function you call triggered twice if you didn’t unsubscribe

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

    Hi Samyam! Right now the character is 'teleporting' to the new position as that's the new reference point you're setting it as. How would you make the character 'transition/walk' to the new location while staying in the grid?

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

      You could have a coroutine and lerp or smoothdamp or tween between the current and next value. And while it’s doing that you can play a walking animation.

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

      @@samyam Thanks for pointing me in the right direction, got it working!

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

    now to compare your code to mine.....
    Edit:
    @11:10 and this is where my method falls apart :D:D
    I might have to refactor all of my stuff to copy this, this is so better than my craziness! :D
    I use a raycast which gets passed the direction. The raycast looks for a certain layer. If the layer is not there, then it passes the direction back to a movement function. But as i have scaled up my game, i use the raycast to check for special types of tags, or layers and it may allow you to move, or may teleport you or different things based on the tile the raycast hits.

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

      Might be better in the long run to refractor to simplify movement. You could also do away with raycasts and just use the tilemap information with the position to check if an item is there

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

    For some reason Unity will not let me create an input action asset at all. (Edit: never mind, had to install the package :P )

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

      Thanks for pointing this out, was wondering why!
      For anyone who may need slightly more help, the actual unity reference is great for this: docs.unity3d.com/Packages/com.unity.inputsystem@0.9/manual/Installation.html

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

    Hi ! Thank you so much for this video.
    I have a question! In this case, is that useful to add a Tilemap Collider & Composite Collider while the "collision" is managed by the script? I'm doing a puzzle game without any use of physics, I don't know if I have to add these colliders components! Thank you!

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

      In that case you don’t need the colliders, they are good if you want to check collision between two things :) Although note, if you are using raycasts the object the raycast is colliding against does need a collider.

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

    Great Tutorial ! can you make the player move to diagonal cells with this setup ?

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

      Yes! You can pass in the input vector to your Move function with the direction you’d like to move it in :)

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

    Hello, how would I make it so the player keeps moving when the movement keys are held. Also, is there a way to incorporate a smoothDamp or something into this so the player doesn't just teleport to the adjacent tile? Thanks for the tutorial.

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

      Coroutines are going to be your best friend here
      docs.unity3d.com/Manual/Coroutines.html
      You can make a coroutine to start a timer before the player is able to press again, and you can make another coroutine for moving from one tile to another over time
      gamedevbeginner.com/coroutines-in-unity-when-and-how-to-use-them/

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

    Awesome tutorial, just subbed! Super quick question, I can very rapidly move in straight lines (horizontal or vertical), however some keys don't register when you try to move horizontal, followed immediately by vertical or vice versa. Do you know if this is a limitation of the Input system or the script? Thanks!

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

      There seems to be a similar bug report open forum.unity.com/threads/bug-with-two-keyboard-keys-pressed-or-released-simultaneously.706955/
      Try changing the default press/tap time to lower. Also maybe you can check in every frame to see if it has been pressed instead of using an event callback (using controls.Main.Movement.triggered)?

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

      @@samyam Ooh thanks for the prompt reply. Will investigate

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

    so my cell size is much smaller than 1 since i had to scale it to my sprites... How can i adjust the movement to be set onto the cellsize?

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

      You can get a reference to the grid and then get it's cell size
      docs.unity3d.com/ScriptReference/Grid-cellSize.html
      Then in the code you can move depending on how big the cell size is.

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

    How would you set the reference of the ground and collision tilemap on code if there are multiple tilemaps?

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

      You could have a List of Tilemaps if you have multiple collision ones and loop through each one checking if there isn't a collider in that grid space

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

    Just wondering, is it possible to stop the camera from following the player once they reach certain cells?

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

      If you are using Cinemachine you can have one camera following the player and another that's just positioned at the location you need. Then you can switch between them once you reach the location. Unity will blend to the newly activated camera. If you arent using Cinemachine then you have to write the logic itself, when it reaches a cell position at a certain location and ignore the player.

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

    I love your tutoriales!I have a question and I hope you can answer. I'm struggling with the new input system. I'm working on a 2D RPG and I migrated to the new input system. Before i used Input.GetAxisRaw in order to know the player's facing direction my question is how can i reproduce the axis raw with the new input system?

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

      Thanks! The example in the video returns with -1, 0, or 1 when the button is being pressed down. You can also read the value at any moment using controls.Main.movement.ReadValue()
      Let me know if that helps

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

      I am new myself, but to tag onto what samy said.
      You would then run that through the normal speed * time.DeltaTime bit, so rather than jumping 1 unit hat a time it is a smooth movement.
      There are several other youtube videos out that there use new input system for standard movement.
      This is the FIRST video that covered grid based movement.

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

    Nice tutorial but how to move two players simultaneously and not collide each other? Thx!

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

      Hm you can have another player with the same script, should work the same, and then you can add another check in the code to make sure that there is not another player in that tile by checking the position (you can have a reference to the other player)

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

      @@samyam Thanks for the reply. In my case, I have been tagging the player as a obstacle, and it works but the two players don't move at the same time. One of them, and depending the size of the collider, wait a little bit until the other player moves. I'll try your method then.

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

    I am having an error for the main
    "Assets\Scripts\PlayerController.cs(32,18): error CS1061: 'PlayerMovement' does not contain a definition for 'Main' and no accessible extension method 'Main' accepting a first argument of type 'PlayerMovement' could be found (are you missing a using directive or an assembly reference?)"
    Any advice?

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

      Make sure you named it exactly Main with capital letters, also try regenerating the script.

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

      @@samyam oh my bad, I forgot to tell you I fixed it! Sorry, I am very dumb 😂

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

      No worries!

    • @LucioGD
      @LucioGD ปีที่แล้ว

      @@Boxsteam Hey, I'm getting that exact error and I'm not sure why. Do you remember how you fixed it?

  • @335haan5
    @335haan5 3 ปีที่แล้ว

    you kinda sound like the guy who says lemons in one of gingerpale's videos

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

    у меня проблемы с шестиугольными сетками

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

    Hey so I've gotten my character to move but after he moves once unity says the ground hasn't been assigned, whats going on?

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

      Maybe you forgot to assign the ground in the inspector for the player controller component?

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

      @@samyam see i thought that too but even in play mode the ground stays in the inspector as it telling me its not there

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

      Try double clicking the error and see what line it takes you to, there seems to be a null reference somewhere

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

    nice

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

    Is there a way to make the player jump 0.5 of a tile instead of 1 tile?

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

      Make sure the direction is normalized then divide it by 2
      direction.normalized / 2 - will be .5

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

      @@samyam Thank you! But it does make the collisions a bit off. Like it's also dividing the size of the collision tile map? I'll try to look into it.
      Edit: Got it working!

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

    Is it work on isometric grid?

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

      I haven’t tested it but it should work, you might have to change how much the player moves w/ the arrow key press
      I have a separate video on isometric movement with mouse click here if you are interested
      th-cam.com/video/b0AQg5ZTpac/w-d-xo.html

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

    I'm not sure. I think your tutorials are nice (or at least what I've seen so far ) but I don't like this new inout system. The "old one" seemed much simpler to me.

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

      That’s okay! It’s up to your preference at the end. The new system is great for extra processing and features, and cross platform support
      I have a video here where I show why to use the input system if you are interested
      th-cam.com/video/GyKBoDF_Oxo/w-d-xo.html

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

      @@samyam Thanks! I'll give it a watch.

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

    how to make a player have a movement limit like they can only move to 3 boxes at a time. and the boxes that the player can move to are highlighted

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

      You can keep track of how much the player has moved in an integer and then reset it once. condition has been met. If you mean move three tiles with one key press then you can use a coroutine and use waitforseconds to wait a small amount before hoping to the next tile (and continue until the threshold 3 is reached). As for highlighting you can have another sprite on top of the tilemap and have it be at the location the player is moving. Hope that helps!
      lukashermann.dev/writing/unity-highlight-tile-in-tilemap-on-mousever/

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

      @@samyam Thanks

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

    Only Likes!!!!

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

    There is no such thing as create input action

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

    anyone knows how can I add an object and push it by the player? It's really getting over my head

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

      If you want it to use physics you'll need to make the objects rigidbodies. But if you want to do it in a grid like fashion, before moving the player in the direction it's moving, check if there's an object on that tile that can be moved, if so, then translate the object in the direction the player is moving, one tile forwards, similar to how we move the player.

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

      @@samyam that sounds sooo complex to me, as a code newbie :(. how would this go in code?

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

      In the move function, you can check if there’s an object in the way and move that object in the direction as well (for example if the direction is upwards (0,1) then both the player and cube will move upwards). to check if there’s an object it’s similar to the canmove function except you have to check the tilemap where the object should be on. Does that help a bit?