Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Poi7ioN · Oct 06, 2019 at 07:27 AM · movement2d gamegrid

Help with Player Movement..

Hello there, i'm working on a 2d grid based movement game. I've created a dynamic node generator script which places the cells based on custom grid size and size of cells are also variable. Here is a image for your reference. alt text

 // Update is called once per frame
     void Update()
     {
         if(canMove)
         {
             transform.position = Vector3.MoveTowards(transform.position, nextDirection, speed * Time.deltaTime);
         }
     }
 
     private void CheckSwipeDirection(SwipeData swipeData)
     {
         if(swipeData.swipeDirection == SwipeDirection.Up)
         {
             count += cols;
             MovePlayer(count);
         }
         else if(swipeData.swipeDirection == SwipeDirection.Down)
         {
             count -= cols;
             MovePlayer(count);
         }
         else if(swipeData.swipeDirection == SwipeDirection.Left)
         {
             count--;
             MovePlayer(count);
         }
         else if(swipeData.swipeDirection == SwipeDirection.Right)
         {
             count++;
             MovePlayer(count);
         }
     }
 
     private void MovePlayer(int position)
     {
         nextDirection = new Vector3(cells[position].transform.position.x, cells[position].transform.position.y, transform.position.z);
         canMove = true;
     }


This is the updated Code for player movement which translate from one cell to another but in this situation i'm unable to warp the player from one side of the screen to another as it only moves towards next cell taking the array index from the list. I've already written the script for warping and it works fine if i were to move player with velocity component, but then the grid movement is broken. Please can some one help me out..!

gridtest.png (112.5 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Oct 06, 2019 at 11:48 AM 0
Share

Hello there.

I think you are giving to much information, making the post "heavy" to read. Try to simplify it so more people will read it and maybe someone have an answer for you.

Goiod luck!

avatar image Poi7ioN tormentoarmagedoom · Oct 06, 2019 at 01:26 PM 0
Share

Thanks for the advice , i've made some changes in my code and tried my best to simplify my problem. Please take a look and help me out here :)

avatar image Magso · Oct 06, 2019 at 02:12 PM 0
Share

Are you asking how to move to the opposite side if there aren't any more cells in swipeDirection? e.g. takeaway 5 if count is a multiple of 5?

 if(swipeData.swipeDirection == SwipeDirection.Right)
 {
     if(count % 5 == 0)
     {
         count -= 5;
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mouton · Oct 06, 2019 at 03:00 PM

You need to check if the player tries to get out the boundaries of the map. This can be easily achieved by using the modulo operator.

For horizontal movement, you need to check the position is a multiple of the width:


 if (swipeData.swipeDirection == SwipeDirection.Right)
 {
     map_index++;

     if (map_index % map_width == 0)
     {
         map_index += map_width;
     }
 }
 if (swipeData.swipeDirection == SwipeDirection.Left)
 {
     map_index--;

     if (map_index % (map_width - 1) == 0)
     {
         map_index -= map_width;
     }
 }


For vertical movement, you need to check the position is a multiple of the height, and add the width:


 if (swipeData.swipeDirection == SwipeDirection.Up)
 {
     map_index += map_width;

     if (map_index >= map_index.Length)
     {
         map_index -= map_index.Length;
     }
 }
 if (swipeData.swipeDirection == SwipeDirection.Down)
 {
     map_index -= map_width;

     if (map_index < 0)
     {
         map_index += map_index.Length;
     }
 }


Edit


To achieve the effect in the video you linked, you have to invert the vertical/horizontal offset of the player when he reaches screen borders. Here is an example:


  if (map_index % map_width == 0)
  {
      var vertical_offset = map_height - Math.floor(map_height / map_index);

      map_index = vertical_offset * map_width;
  }
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Poi7ioN · Oct 06, 2019 at 03:42 PM 0
Share

Thanks for the reply, but there is a problem with this code i want the player to travel across the side of the screen and pop out on the other side but with this code it just travels through the view area and sits on the opposite cell. How can i achieve that ?

avatar image Mouton Poi7ioN · Oct 06, 2019 at 04:14 PM 0
Share

What do you mean by "screen" and "view" ?

avatar image Poi7ioN Mouton · Oct 06, 2019 at 04:29 PM 0
Share

Let see, when i swipe right while on the right most cell of screen the player should warp to the opposite side and same goes for all four sides. But with this code the player move towards the opposite cell but not by warping. Does this helps ? Sorry for my bad explanation i'm kinda new XD.

Show more comments
avatar image Poi7ioN · Oct 06, 2019 at 05:25 PM 0
Share

Sorry but that didn't work , how will assigning cell index value will help to warp the player i don't understand . Here is the code which moves player in 4 direction with warping.

 void Update()
     {
         if(can$$anonymous$$ove)
         {
             transform.Translate(nextDirection * Time.deltaTime * speed);
         }
     }
 
     private void CheckSwipeDirection(SwipeData swipeData)
     {
         if(swipeData.swipeDirection == SwipeDirection.Up)
         {
             $$anonymous$$ovePlayer(Vector3.up);
         }
         else if(swipeData.swipeDirection == SwipeDirection.Down)
         {
             $$anonymous$$ovePlayer(Vector3.down);
         }
         else if(swipeData.swipeDirection == SwipeDirection.Left)
         {
             $$anonymous$$ovePlayer(Vector3.left);
         }
         else if(swipeData.swipeDirection == SwipeDirection.Right)
         {
             $$anonymous$$ovePlayer(Vector3.right);
         }
     }
 
     private void $$anonymous$$ovePlayer(Vector3 direction)
     {
         nextDirection = direction;
         can$$anonymous$$ove = true;
     }

Here just tell me how to take input when player is on cell center and assign direction than i'm done . Thanks

avatar image Mouton Poi7ioN · Oct 06, 2019 at 05:37 PM 0
Share

Don't change cell indexes, change the player index according to the map.

Sorry I thought (from your code and your screenshot) that your player was bound to the grid. If it is not, you just have to change the position of the player. To do so, all the code is provided in the video you linked.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move the character to bottom left/right? 0 Answers

Rotating a tilemap with ruletiles 0 Answers

How do I stop momentum in 2D sidescroller? 1 Answer

How to smooth out character Roll in a Top-down 2D movement 0 Answers

2D Character Jumps only 1 time 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges