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

  Рет қаралды 45,675

samyam

samyam

Күн бұрын

Пікірлер: 110
@JackKell100
@JackKell100 3 жыл бұрын
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 жыл бұрын
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 KZbin journey, but don't change a thing about your informative and descriptive style. Got yourself a new, solid subscriber.
@PandaTheGFX
@PandaTheGFX 4 жыл бұрын
Cool!
@callindrill
@callindrill 3 жыл бұрын
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 6 ай бұрын
I can't believe that it took me five videos just to find one that uses the Tilemaps namespace, thank you.
@jacobhuppertz5625
@jacobhuppertz5625 2 жыл бұрын
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
@neisef
@neisef 3 жыл бұрын
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 жыл бұрын
😁
@bro-rubro
@bro-rubro Жыл бұрын
your content is simply amazing, love everything please leave it online forever cuz this is gold
@doge9203
@doge9203 4 жыл бұрын
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.
@Ufol
@Ufol 4 жыл бұрын
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 жыл бұрын
That works as well! Good catch
@AlCh3mi_MvP
@AlCh3mi_MvP 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 жыл бұрын
Totally agreed
@samyam
@samyam 4 жыл бұрын
Thank you so much!! I'm really glad they are helpful and enjoyable :)
@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 жыл бұрын
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 жыл бұрын
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!
@Piroshi001
@Piroshi001 3 жыл бұрын
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 ?
@starsquad9814
@starsquad9814 4 жыл бұрын
Oh my god thank you so much for this tutorial. I was struggling to get this to work for a while
@pmgjwg
@pmgjwg 3 жыл бұрын
Exactly what I was looking for - super easy to understand. Thanks!
@BonkerzGamer119
@BonkerzGamer119 6 ай бұрын
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.
@jaytwist8738
@jaytwist8738 8 ай бұрын
How would I handle this if my sprites are taller than wider?
@jessemauritz1
@jessemauritz1 4 жыл бұрын
lowkey helped me in the miz jam im working on thank you!
@samyam
@samyam 4 жыл бұрын
Good luck on the jam!
@jessemauritz1
@jessemauritz1 4 жыл бұрын
samyam thank you!
@tommallama9663
@tommallama9663 4 жыл бұрын
Very cool :O I had no idea you could use the tilemap to move the character in that way!
@PCB389
@PCB389 4 жыл бұрын
Great job, and well explained. Thanks for your useful video. 10/10
@lukass1604
@lukass1604 4 жыл бұрын
Great work! Really well explained.
@HadiLePanda
@HadiLePanda 4 жыл бұрын
Awesome tutorial, really love the way you explain ^^
@DinoSharkCobra
@DinoSharkCobra 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 kzbin.info/aero/PLKUARkaoYQT2nKuWy0mKwYURe2roBGJdr
@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.
@flyer261
@flyer261 2 жыл бұрын
у меня проблемы с шестиугольными сетками
@Imagino1234
@Imagino1234 3 жыл бұрын
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 KZbin. 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 жыл бұрын
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 6 ай бұрын
@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));
@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 жыл бұрын
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.
@Qlawen
@Qlawen 3 жыл бұрын
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 жыл бұрын
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!
@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
@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
@lucascaldeira2600
@lucascaldeira2600 3 жыл бұрын
Great Tutorial ! can you make the player move to diagonal cells with this setup ?
@samyam
@samyam 3 жыл бұрын
Yes! You can pass in the input vector to your Move function with the direction you’d like to move it in :)
@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
@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 3 жыл бұрын
Make sure you named it exactly Main with capital letters, also try regenerating the script.
@Boxsteam
@Boxsteam 3 жыл бұрын
@@samyam oh my bad, I forgot to tell you I fixed it! Sorry, I am very dumb 😂
@samyam
@samyam 3 жыл бұрын
No worries!
@LucioGD
@LucioGD 2 жыл бұрын
@@Boxsteam Hey, I'm getting that exact error and I'm not sure why. Do you remember how you fixed it?
@midnataka
@midnataka 3 жыл бұрын
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 3 жыл бұрын
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.
@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
@saviocallado5045
@saviocallado5045 Жыл бұрын
THANKS
@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 жыл бұрын
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
@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.
@LegendSeekerX
@LegendSeekerX 4 жыл бұрын
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 4 жыл бұрын
Oops! Thanks for the heads up 🙂
@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 жыл бұрын
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/
@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.
@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
@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.
@cupnooodle
@cupnooodle 4 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
@@samyam Ooh thanks for the prompt reply. Will investigate
@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 жыл бұрын
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 жыл бұрын
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.
@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!
@mnicodev
@mnicodev 3 жыл бұрын
Nice tutorial but how to move two players simultaneously and not collide each other? Thx!
@samyam
@samyam 3 жыл бұрын
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.
@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?
@DinoSharkCobra
@DinoSharkCobra 4 жыл бұрын
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
@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 kzbin.info/www/bejne/mGGkgpprj7mjl8U
@335haan5
@335haan5 3 жыл бұрын
you kinda sound like the guy who says lemons in one of gingerpale's videos
@sparrowhawk2419
@sparrowhawk2419 4 жыл бұрын
nice
@aikou2886
@aikou2886 3 жыл бұрын
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 kzbin.info/www/bejne/faquc6J6e8SCrtE
@aikou2886
@aikou2886 3 жыл бұрын
@@samyam Thanks! I'll give it a watch.
@cosmic4453
@cosmic4453 4 жыл бұрын
Only Likes!!!!
@AverageTV
@AverageTV 2 жыл бұрын
There is no such thing as create input action
Mouse Click Movement in Isometric Tilemap - Unity Tutorial
15:17
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 195 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 28 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 40 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
i paid Fiverr devs to make a game in Unreal Engine 5
10:05
Unity's New Input System:  The Definitive Guide
32:07
DmanGames
Рет қаралды 34 М.
Pathfinding - Understanding A* (A star)
12:52
Tarodev
Рет қаралды 139 М.
TOP DOWN Movement - Unity Tutorial
16:17
Sasquatch B Studios
Рет қаралды 17 М.
2D Movement in Godot 4 using TileMap
19:27
Retrobright
Рет қаралды 36 М.
How to use Unity's New INPUT System EASILY
9:32
BMo
Рет қаралды 242 М.
How to Do Grid Based Movement [Unity Tutorial]
9:38
Comp-3 Interactive
Рет қаралды 66 М.
Learn Bevy 0.11 By Making a Game! (part 1)
13:01
Logic Projects
Рет қаралды 46 М.
Unity Grid Based Movement System: Part 1 Setup
18:12
StringCode
Рет қаралды 7 М.
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 195 МЛН