Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by LordTravolta · Jan 21, 2016 at 06:51 AM · c#2dmovementplayerpuzzle

How to move the player only 1 tile per buttonPress?

Hi.

I'm creating a 2D puzzle game where the player moves in a top-down world. Now, I'm having a bit of trouble in the movement section because I don't really know how to make the player move only 1 tile at a time.

This kind of movement is critical part of my game.

Thanks for any kind of infromation.

=========

PS. If possible, would love a example script. :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Veerababu.g · Jan 21, 2016 at 09:46 AM

vector2 target;

if (Input.GetKeyDown (KeyCode.UpArrow)) {

             y = moveDistance;

             x = 0;

         }

` else if (Input.GetKeyDown (KeyCode.DownArrow)) { y = -moveDistance; x = 0; } else if (Input.GetKeyDown (KeyCode.LeftArrow)) {

             x = -moveDistance;

             y = 0;

         } 

``else if (Input.GetKeyDown (KeyCode.RightArrow)) {

             x = moveDistance;

             y = 0;

         }


if (moveUp || moveDown || moveLeft || moveRight) {

             transform.position=new Vector2(transform.position.x+x,transform.position.Y+y);
         }

     }

note

// in the start assign how much distance you want to move .(if you ur tail 100*100 then moveDistance is 1.........

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
avatar image
0

Answer by toddisarockstar · Jan 21, 2016 at 02:14 PM

 var target:Vector3;
     target=transform.position;
 var go:Vector3;   
 var speed:float;
     speed=10;
 function Update () {
 go = Vector3.MoveTowards(go, target,speed*Time.deltaTime);
 gameObject.transform.position=go;
 
 if(Input.GetKeyDown("up"))   {target.y=Mathf.Round(transform.position.y)+1;}
 if(Input.GetKeyDown("down")) {target.y=Mathf.Round(transform.position.y)-1;}
 if(Input.GetKeyDown("right")){target.x=Mathf.Round(transform.position.x)+1;}
 if(Input.GetKeyDown("left")) {target.x=Mathf.Round(transform.position.x)-1;}
 }

this would keep his position rounded off to 1. this script assumes your sqaure positions are exactly 1 apart and 1 wide. i assume that your y is up. anyways this should give you the idea. if you want to slide to any square just change the target variable to match the new sqaure position in your script and and he will go there.

Comment
Add comment · Show 2 · 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 LordTravolta · Jan 23, 2016 at 08:37 AM 0
Share

Sorry, but as you can see at the tags of my post, I use C#.

avatar image ShadyProductions LordTravolta · Jan 23, 2016 at 10:55 AM 0
Share

all you have to do is change function to void and the var's to the correct type :P

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

81 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

Related Questions

Player loses momentum when landing 0 Answers

Player Movement Not Always Responding 1 Answer

2D Object following the cursor without glitchyness at the center 0 Answers

How to move an object on a moving platform with the platform 0 Answers

Problem with a multi-jump script ( C# ) 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