Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 effekt · Jun 08, 2015 at 07:26 AM · 2dmovementtutorial

Making sure playermodel only moves 1 unit

I am doing a school assignment based off of the Unity3D tutorial 2D RogueLike (all code available here)

In my version though, the movement is not turnbased and I am having some trouble figuring it out.

If I I make the player lose its turn after movement, the character will stay perfectly aligned with the grid. But if I let the player keep his turn, the character moves in increments not in alignment with anything(ór how long the computer think I held down the movebutton for).

I tried to fix it with this:

     GameManager.instance.playersTurn = false;

     Thread.Sleep(500);

     GameManager.instance.playersTurn = true;

and it sort of 'fixes' it. It now only moves 1 unit, provided I don't hold down the movebutton for longer than the 500ms, hereafter it seems to 'lose sync' with the board again... Also, it now takes 500ms from i press move, till the character actually moves.

Here is an example of how it looks if i just quickly tap the button to move(so its in sync) alt text

and here is one when i held down the button for more than the 500ms ( out of sync) alt text

So the specific question.

How do I make sure that the character only moves 1 tile/unit with a single button press?

I would greatly appreciate a solution to this

insyncexample.png (55.0 kB)
outofsyncexample.png (54.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Jun 08, 2015 at 08:27 AM

Thread.Sleep will pause the execution of everything, so it's not a very good idea.

What you want is for your script to know which square it's supposed to go to, and then move towards that if you're not there yet.

So if your player character is at 5/5, and the player presses "right", you don't want to try to move the player character 1 to the right, you want to move it towards 6/5. Something like:

 int myXCoordinate;
 int myYCoordinate;

 void Update() {
     if(Input.GetKeyDown(KeyCode.Right)) {
         myXCoordinate++;
     }

     ...

     Vector2 wantedPosition = new Vector2(myXCoordinate, myYCoordinate);
     transform.position = Vector2.MoveTowards(transform.position, wantedPosition, Time.deltaTime * moveSpeed);
 }

You'll have to handle stuff like being out of bounds or whatever, but that should be enough to get you started.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D UFO Tutorial - Stop the Ship 1 Answer

Moving player between pre-determined points 1 Answer

Moving Up, Down, Left and Right via Wasd and the arrow btns?? in 2d 2 Answers

When gamepad is connected, character plays walking animation,When gamepad connected, character walks on it's own 0 Answers

Have 2D Sprite Follow the Mouse in Straight Line 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