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 predragstojic · May 02, 2020 at 06:10 PM · movementplayerunity 2d

Move player on predetermined looping path

Hi all,

I am a begginer developer and I am having difficulties coding the following problem( 2D):

  1. While the button down is pressed, the player should move:

  2. from the starting position down -2 spaces

  3. then up 2 spaces

  4. loop this down 2, up 2 spaces until the button is released

  5. Continue on the current y coordinate (x never changes)

  6. when pressed again, the movement should repeat from the new y coord.

The opposite should work if button Up is pressed.

If tried implementing it with Mathf.Sin(Time.time), but if I repeat the button press, instead of restarting and going down, it remembers the last played position and continues going i.e. up, instead of starting down.

Any help is very much appreciated!

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 Cassos · May 02, 2020 at 06:25 PM

Damn, I'll write you some code :P

     public void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
             StartCoroutine(MoveOnPath(currentIndex));
     }
 
     [Header("Movement")]
     public float moveSpeed = 10;
     public float maxDistanceToTarget = 0.2f;
     public bool autoContinue;
     public bool isRelativeMovement; // Relative to players position
     [Space]
     public Vector2[] path;
     int currentIndex;
 
     public IEnumerator MoveOnPath(int pathIndex)
     {
         currentIndex = pathIndex;
 
         if (path.Length <= pathIndex) // Stops the Courotine if the pathIndex is higher than the counter of the path positions
             yield break;
 
         Vector2 tarPos = path[currentIndex];
         if (isRelativeMovement)
             tarPos += (Vector2)transform.position;
 
         while (Vector2.Distance(transform.position, tarPos) > maxDistanceToTarget) // Moves to target as long as the position is out of reach (maxDistanceToTarget)
         {
             transform.position = Vector3.MoveTowards(transform.position, tarPos, Time.deltaTime * moveSpeed);
         }
 
         int nextIndex = currentIndex++;
         if(nextIndex >= path.Length)
             nextIndex = 0; // If the last point is reached it restarts from the beginning
 
         if (autoContinue)
             StartCoroutine(MoveOnPath(nextIndex)); // Restarts the whole process with the next waypoint!
     }
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 predragstojic · May 03, 2020 at 10:39 AM 0
Share

Thanks for the answer! I'll give a try today.

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

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

Related Questions

Fuel bar doesn't work! 1 Answer

Player Going through colliders 1 Answer

Orbit player around center point 1 Answer

Implement moveSpeed to this object script? 1 Answer

GUI movement Control. Please Help!!! Thanks 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