• 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 Skyfalcon1990 · Apr 16, 2014 at 09:31 AM · 2dmovementspritelerpmovetowards

Moving 2D sprite along a path

I have 10 x 10 grid with some items (Gems). Player drags cell Item from one place to another. I have already found Vector3 positions where I should move my game object. As I understand I should do this in Update method by calling Vector3.MoveTowards or Vector3.Lerp or changing position directly, but I have started using Unity not so long ago and I didn't quite get how should I do this. So I have List with positions where I should move my object alt text (Debug.DrawLine)

How can I successively move Item from one point to another and what method I should use to do this?


UPD. Seems this is working for me. Maybe someone have more correct way to do this

 void Update () {
         if (currentItem != null) {
             currentItem.transform.position = Vector3.MoveTowards(currentItem.transform.position, nextCellPosition, Time.deltaTime * 4.0f);
 
             if (currentItem.transform.position == nextCellPosition) {
                 activePath.Points.RemoveAt(0);
 
                 if (activePath.Length > 1) {
                     nextCellPosition = cells[activePath.Points[1].y, activePath.Points[1].x].transform.position;
                 } else {
                     cells[activePath.Points.First().y, activePath.Points.First().x].Item = currentItem;
                     currentItem = null;
                 }
             }
         }
     }

screenshot 2014-04-16 12.09.00.png (74.3 kB)
Comment
Add comment · Show 3
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 Benproductions1 · Apr 17, 2014 at 06:27 AM 0
Share

Do you mean actually moving along a path over time or just changing the position?

avatar image Skyfalcon1990 · Apr 21, 2014 at 07:16 AM 0
Share

Yep, I need to move sprite along the path

avatar image robertbu · Apr 21, 2014 at 07:22 AM 1
Share

If the path is known (i.e. this question is not about searching out a viable path), then the movement is just a waypoint movement. There are lots of posts on UA about waypoints, plus I believe there is at least one waypoint script in the Unity Wiki.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

2D Sprite leaves a trail when moving diagonally 1 Answer

Sprite is not shown moving 1 Answer

Moving 2D Sprite Player 0 Answers

Beginner using the animation controller to get a sprite walking left and right..Need some help going from here 0 Answers

Joystick 2D Sprite Rotation and Movement 0 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