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 /
  • Help Room /
avatar image
0
Question by mghffel · Dec 28, 2016 at 05:27 AM · movementmovedirection

How can I get my object to move in only one direction at a time?

I am in the very basic stage of learning unity and am in a top down 2D setting. I am trying to get my object to move left a specific number of units and then down a specific number of units and then stop, but what it is doing is just moving diagonal and does not stop.

This is the script attatched to my object.

 public class Movement : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         int leg=0;
         switch (leg)
         {
             case 0:
                 MoveRight(20);
                 leg++;
                 break;
             case 1:
                 MoveDown(20);
                 leg++;
                 break;
             default:
                 break;
         }
 
         MoveDown(20);
         MoveRight(20);
     }
 
     void MoveRight(int amount)
     {
         for (int i = 0; i < amount;i++)
         {
             transform.position = new Vector3(transform.position.x + .001f, transform.position.y);
         }
     }
 
     void MoveLeft(int amount)
     {
         for (int i = 0; i < amount; i++)
         {
             transform.position = new Vector3(transform.position.x - .001f, transform.position.y);
         }
     }
 
     void MoveUp(int amount)
     {
         for (int i = 0; i < amount; i++)
         {
             transform.position = new Vector3(transform.position.x, transform.position.y+.001f);
         }
     }
 
     void MoveDown(int amount)
     {
         for (int i = 0; i < amount; i++)
         {
             transform.position = new Vector3(transform.position.x, transform.position.y - .001f);
         }
     }
 }


Comment
Add comment · Show 1
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 AlwaysSunny · Dec 28, 2016 at 05:27 AM 0
Share

This should be rejected for unformatted code, but I'll fix it for you. Remember to highlight pasted code and click the 101010 button.

Just so you know, you shouldn't make a habit of expecting volunteers to debug beginner's code. Perhaps the next guy will be feeling more charitable. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ScaniX · Dec 28, 2016 at 01:19 PM

You have not added any condition that would make your movement stop. So for every frame (because you are using the Update() callback) the object moves 0.4f (2 * 20 * 0.01f) to the right and 0.2f (1 * 20 * 0.01f) down.

Your switch statement does not work, because leg is a local variable with the value of 0 as it gets initialized each frame like this. You would need to move it to a member variable to make that work.

Having a fixed size step per frame will not give you a nice movement anyway, but you should look up tutorials for that separately.

I'm giving you a very simple example on how to get a left/down movement with least possible changes to your existing code:

 private int leg = 0;
 private int amount = 20;
 
 void Update() {
      switch (leg)
      {
          case 0: // go left
              MoveLeft(1); // only one step, loop in method can be removed
              if (--amount == 0) { // switch to next step if amount is depleted
                  amount = 20; // back to 20, which is the amount we want to go down
                  leg++;
              }
              break;
          case 1:
              MoveDown(1);
              if (--amount == 0)
                  leg++;
              break;
          default:
              break;
      }
 }
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

6 People are following this question.

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

Related Questions

Rigidbody FPS Controller isn't Moving. 0 Answers

Path Movement created from user input.mouse and player follows it. 0 Answers

Issues w/ 2D shmup ship "lean" animations on diagonals 1 Answer

MoveTowards is curving for no reason 0 Answers

GetKeyDown breaks jumping vs GetKey 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