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 Nelis · Feb 19, 2013 at 05:04 PM · 2dmovementdistance

2D one distance at a time movement

Hello, I am trying to make a 2D game. But i can't find out how you can make a player move one distance at a time. By that I mean when you press the right arrow key your character moves form x to x + 1, then waites and if you are still holding it you move again 1 'block' to the right. So you are always above a block. Excpect when you are moving. I hope you understand.

-Nelis

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
0
Best Answer

Answer by xikky · Feb 19, 2013 at 05:24 PM

Not sure what you mean exactly, but I think this helps. This example uses WaitForSeconds function which pauses the Update for the delay given as the parameter. You can surely figure out the rest of the code.

 private var moveCheck:boolean = true;
 private var delay:int = 2;
 
 function Update() {
 
    if (moveCheck)
       Move();
 }
 
 function Move() {

    moveCheck = false;
    yield WaitForSeconds(delay);
 
    //Put code for movement here
 
    moveCheck = true;
 }


Comment
Add comment · Show 5 · 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 Nelis · Feb 19, 2013 at 06:38 PM 0
Share

Thank you! :D And to clarify i mean a movement like in this flashgame : http://www.xgenstudios.com/game.php?keyword=motherload

avatar image xikky · Feb 19, 2013 at 07:02 PM 0
Share

Glad to help. It would be nice to turn the answer into the solution :) and such comments post them by the 'add new comment' for the sake of a well organised community ;)

avatar image Nelis · Feb 19, 2013 at 07:29 PM 0
Share

How do you turn the answer into the solution? This is my first question ever :)

avatar image robertbu · Feb 19, 2013 at 07:31 PM 0
Share

Click on the checkmark next to the answer you accept.

avatar image Nelis · Feb 20, 2013 at 11:32 AM 0
Share

Thank you!

avatar image
0

Answer by robertbu · Feb 19, 2013 at 06:43 PM

Here is a starting point. I wrote it as an answer to another question, but I think it gives you the motion you are looking for. Attach it to a cube and use the arrow keys to move.

  public class MoveTheCube : MonoBehaviour {
  
         Vector3 v3Delta = Vector3.zero;
         Vector3 v3Accl = new Vector3(.005f, 0.0f, 0.0f);
         float fFrictionFactor = 0.98f;
         float fMaxSpeed = 0.5f;
  
         void Update () {
            if (Input.GetKey(KeyCode.RightArrow))
              v3Delta += v3Accl;
            if (Input.GetKey (KeyCode.LeftArrow))
              v3Delta -= v3Accl;
  
            v3Delta *= fFrictionFactor;
            v3Delta.x = Mathf.Clamp (v3Delta.x, -fMaxSpeed, fMaxSpeed);
  
            transform.Translate (v3Delta);
         }
  
 }
Comment
Add comment · Show 1 · 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 Nelis · Feb 19, 2013 at 07:25 PM 0
Share

Thank you

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

10 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

Related Questions

Move objects infinite based on distance and not an invoke 2 Answers

How to I get my 2d gameobject to move towards another gameobject when it is clicked? 0 Answers

Help Wanted. Switching localscale starts unwanted animation state. 1 Answer

2D Sprite leaves a trail when moving diagonally 1 Answer

resizing spherecast radius over distance? 4 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