Create a 2D grid movement player controller script in Unity

Поділитися
Вставка
  • Опубліковано 21 лис 2024

КОМЕНТАРІ •

  • @moriyainstitute990
    @moriyainstitute990 3 місяці тому

    It is very useful to me! Thank you for your guide!

  • @user-ve9xq7le5y
    @user-ve9xq7le5y 5 місяців тому

    Thanks for the amazing tutorial. Happy to report it still works, but how and where would you implement "flipping" the X axis of a 2D sprite when moving left/right?

    • @FirnoxGames
      @FirnoxGames  5 місяців тому +1

      Let's assume your sprite is facing to the right by default, then we want to set the spriteRenderers flipX parameter to true when you are moving left, and false when moving right. For up/down you can just leave it alone and it'll stay whatever way it was. Obviously to do this you'll need to get a referenece to the sprite renderer in your script. The other option is to set the x-scale to negative which flips the game object. As ever more than one way to do things in Unity.

  • @mistycomet7501
    @mistycomet7501 Рік тому

    This is such a useful video thanks, however how would one create world borders to prevent the player from leaving the map? Thanks a lot

  • @gerarddilk4862
    @gerarddilk4862 10 місяців тому

    Thanks for the Video!

  • @mnicodev
    @mnicodev Рік тому

    Great video!! How can multiple player movements be implemented? For example, if two players are moving asynchronously, how can they avoid colliding with each other and other obstacles? Thanks

    • @FirnoxGames
      @FirnoxGames  Рік тому

      There a bunch of ways you can do this. But ultimately you want a IsThisPositionOccupied() function that you can pass your target location into and it should return True or False. Which you check before calling the move coroutine. How to tell if a position is occupied you have two main ways of doing this. Either you do it with the physics engine. Before moving you cast a 2D ray 1-unit in the direction you wish to move, if it hits another collider then you can't move to it (the issue here is if you have multiple things moving at once and trying to go to the same square). The alternative is to maintain a dictionary of each objects location, that's an efficient lookup that says if the target area contains an item or not. Then you just have to update this each time something moves.

    • @mnicodev
      @mnicodev Рік тому

      @@FirnoxGames many thanks for taking the time to reply my question. Maybe the dictionary way should be better, I tried in the past with physics and async don’t work very well, sometime players occupied the same position in the grid. Hope you take time and make a tutorial about this new approach because at least in UA-cam I haven’t found how to do it. Thanks!