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 Mimesis_Games · Jul 19, 2017 at 09:37 AM · vector3mobilelerpswipemovetowards

How to move an object from another position using Vector3.moveTowards precisely and slowly by swiping (mobile)

I want to move an object to another position precisely and slowly. I was at first using Vector3.Lerp but I read that it never reaches the point precisely or it slows down as it approaches the target point. Here is my code:

 if ((lp.x > fp.x)) 
 {  //If the movement was to the right)//Right swipe
     Debug.Log ("Right Swipe");
     objMover ();
 } 
 
 ......
 
 void objMover()
     {
         if (myTransform.position == pos1) {
             float step = Time.deltaTime * 2.0f;
             myTransform.position = Vector3.MoveTowards(pos1,pos2,step);
     }
 

When I use Lerp, it reaches maybe 1/3 of the way to the target position. When I use move towards, it doesn't move at all. I also tried checking using the Vector3.distance to repeat the moveTowards but fails.

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 Garazbolg · Jul 19, 2017 at 01:25 PM

I don't know where you read that Lerp isn't precise but I don't think its the case. But about the slowing down part it just depend on the way you implement it.

Also Lerp and MoveTowards behave the same. They aren't Coroutine though, you need to call them for each movement. All they do is something like :

 Vector3 Vector3_Lerp(Vector3 a, Vector3 b, float step){
         return a + (b - a) * Mathf.Clamp01(step);
     }

Now back to your code :

 if (myTransform.position == pos1) {

Will only make you object move once. The next frame when it started moving toward pos2 it won't be on pos1.

I think what you want to do is use Coroutine like this :

 if ((lp.x > fp.x))
             {  //If the movement was to the right)//Right swipe
                 Debug.Log("Right Swipe");
                 StartCoroutine(objMover());
             } 
 
     IEnumerator objMover(Vector3 pos1, Vector3 pos2, float speed)
     {
         float step = 0;
         while(step < 1){
             step += Time.deltaTime * speed;
             myTransform.position = Vector3.MoveTowards(pos1, pos2, step);
                     yield return null;
         }
         myTransform.position = pos2;
     }

And you might want to prevent your player from doing anything until it is over, a simple flag should do the trick.

I hope it helped you. Good luck !

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 Mimesis_Games · Jul 19, 2017 at 02:30 PM 0
Share

Thank for answering! I am still starting out with Unity, thanks for the help. Although, I still need to study what is coroutine.

I ended up using lerp, but you have enlightened me about "it only runs one frame". I'll try using that code though!

I was just thinking, can I use if (myTransform.position == pos1){ if (myTransform.Distance(pos1,pos2) <= 0){ //working lerp code } }

would it do the trick of continually running the lerp?

avatar image Garazbolg Mimesis_Games · Jul 20, 2017 at 09:12 AM 0
Share

No because myTransform.position == pos1will only be true when your gameObject is EXACTLY at that position, so the next frame it won't be true.

But you can use this type of condition to check if your object has reached its destination. For exemple with a basic waypoint system :

 public Transform[] targets;
     private int currentIndex = 0;
     public float distanceToTarget = 0.5f;
     public float speed = 2f;
 
     void Update()
     {
         if (Vector3.Distance(transform.position, targets[currentIndex].position) < distanceToTarget)
             currentIndex = (currentIndex + 1) % targets.Length;
 
         //$$anonymous$$ove
         Vector3 from = transform.position;
         Vector3 to = targets[currentIndex].position;
         transform.position += (to-from).normalized*speed*Time.deltaTime;
     }


And don't forget to accept the answer if it helped 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

103 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

Related Questions

3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers

Move Transform to Target in X seconds 3 Answers

Vector3.MoveTowards and Quaternion.RotateTowards 1 Answer

Vector3.Lerp result in "laggy" movement while running on iOS devices 2 Answers

Smoothly moving a 2D gameobject along a vector path 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