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 Michaeldearmas · May 07, 2019 at 11:24 PM · movement scriptcodepage

Trying to implement movement similar to Mega Man Battle Network,Trying to implement specific kind of movement from Mega Man Battle Network series

Hey guys, i'm new to all this stuff and i'm trying to implement movement like in the Mega Man Battle Network series, in those games you move on 3x3 grid. Each input on the direction pad, moves you to another tile in the grid.

I have semi working code, however i'm getting weird instances where pressing in a different direction from the last direction moves you diagonally. This is my code:

private void Update() { gameObject.transform.position = IncrementInputPos(); }

 public Vector3 IncrementInputPos()
 {
     Vector3 originPoint = new Vector3(0, 0, 0);
     Vector3 playerPos = gameObject.transform.position;

     if (Input.GetKeyDown(KeyCode.D))
     {
         playerPos += originPoint + new Vector3 (1f,playerPos.y,playerPos.z);
     }

     if (Input.GetKeyDown(KeyCode.A))
     {
         playerPos -= originPoint + new Vector3(1f, playerPos.y, playerPos.z);


     }

     if (Input.GetKeyDown(KeyCode.W))
     {
         playerPos += originPoint + new Vector3(playerPos.x, 1f, playerPos.z);
     }

     if (Input.GetKeyDown(KeyCode.S))
     {
         playerPos -= originPoint + new Vector3(playerPos.x, 1f, playerPos.z);
     }

     return playerPos;
 }


What do you think is happening here? Also, is this the best way to implement movement considering I want the tiles in the grid to interact with the player (say if they are frozen or on fire or whatnot)?

all the best, Thanks!

Comment
Add comment · Show 6
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 DCordoba · May 08, 2019 at 11:00 AM 0
Share

maybe you are adding too much things? try:

 /// <summary>
 /// The distance betwwen the center of a square to the center of square next
 /// </summary>
 public float distanceBetween = 1f;
  public Vector3 IncrementInputPos()
  {
      Vector3 originPoint = new Vector3(0, 0, 0);
      Vector3 playerPos = gameObject.transform.position;
      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
      {
          playerPos += Vector3.right *  distanceBetween;
      }
      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
      {
          playerPos += Vector3.left *  distanceBetween;
      }
      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
      {
          playerPos += Vector3.forward *  distanceBetween;
      }
      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
      {
          playerPos += Vector3.right *  distanceBetween;
      }
      return playerPos;
  }


on my experience using offset variables like originPoint made a mess on your code. if you set up the player properly at start, you can add/substract fixed positions from he, if you move the grid through scene, consider using a parent from both (grid and player) and use the same code with transform.localPosition ins$$anonymous$$d of transform.position

avatar image Michaeldearmas DCordoba · May 08, 2019 at 06:51 PM 1
Share

Thank you for your help! I'll play around with this

avatar image Michaeldearmas DCordoba · May 08, 2019 at 07:14 PM 0
Share

I got it! It seems perhaps that feeding the position of the player into the additive equation at each key press was doing odd stuff, this worked:

public Vector3 IncrementInputPos() {

     Vector3 playerPos = gameObject.transform.localPosition;
     

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
     {
         playerPos = playerPos + Vector3.right;
     }

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
     {
         playerPos = playerPos + Vector3.left;


     }

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
     {
         playerPos = playerPos + Vector3.up;
     }

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
     {
         playerPos = playerPos + Vector3.down;
     }

     return playerPos;
 }


thanks agan!

avatar image Michaeldearmas Michaeldearmas · May 08, 2019 at 07:34 PM 0
Share

now I gotta figure out how to add clamps to this stuff

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

106 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

Related Questions

endless runner movement problem 3 Answers

Script to Move different parts of Gantry robot in X,Y,Z axis in sequence 2 Answers

Momentum with Character Controller 1 Answer

I want to figure how to change this asset script for person controls to not rotate 360 0 Answers

Movement Scripts interfering with each other 1 Answer


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