• 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 audibles · Jan 27, 2014 at 05:50 AM · movementcoroutinevectorfloatingpoint

Movement to vector3 stuck in a loop

I'm having trouble with a script that moves an object to a specific vector. The following method is a couroutine that is passed the target's position. The execution hangs up at the point where the Debug action is called. Is this an error having to do with floating point comparisons? Any help is appreciated, thanks in advance.

     IEnumerator GoTo (Vector3 target)
     {
         bool walking = true;
         
         while (walking == true)
         {
             float targetDistance = (Vector3.Distance (transform.position, target));
             Vector3 targetLocation = new Vector3 (target.x, transform.position.y, target.z);
             
             if (targetDistance >= 3.0f)
             {
                 MoveTo (targetLocation);
                 RotateTo (targetLocation);
                 Debug.Log (targetDistance);
             }
             else
             {
                 while (targetLocation - transform.position != Vector3.zero && transform.rotation != Quaternion.LookRotation (targetLocation - transform.position))
                 {
                     RotateTo (targetLocation);
                     yield return 0;
                 }
 
                 TargetReached (gameObject);
                 walking = false;
             }
             
             yield return 0;
         }
     }

     void MoveTo (Vector3 targetLocation)
     {
         transform.position = Vector3.MoveTowards (transform.position, targetLocation, Time.deltaTime * myParams.speed);
     }
     
     void RotateTo (Vector3 targetLocation)
     {
         if (targetLocation - transform.position != Vector3.zero)
         {
             Vector3 turnDirection = Vector3.RotateTowards (transform.forward, targetLocation - transform.position, Time.deltaTime * myParams.turnSpeed, 1.0f);
             transform.rotation = Quaternion.LookRotation (turnDirection);
         }
     }
 
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
1

Answer by Drakulo · Jan 27, 2014 at 09:00 AM

You should not use strict comparison between vectors for managing movements. Even when using Lerp for smoothing, Vectors will never be exactly equals. Consider using precision instead :

 if(targetLocation - transform.position <= precision)
 {
   // Under a given precision, you consider that the target is reached
 }

For your infinite loop, I guess the problem comes from the movement speed. In your MoveTo method, you apply a speed Forward. If the speed is too high, the movement will do some roundtrip move around the target... forever.

The trick to prevent this loop would be te detect that the GameObject is near it's target and apply another speed multiplier, decreasing while it approaches the target. This will result in a speed reduction over the last few steps so you will not move farther than the target.

Comment
Add comment · Show 4 · 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 audibles · Jan 28, 2014 at 07:55 PM 0
Share

C# does not like me using operators on floats.

error CS0019: Operator >=' cannot be applied to operands of type UnityEngine.Vector3' and `UnityEngine.Vector3'

avatar image Drakulo · Jan 28, 2014 at 08:01 PM 1
Share

Oh yes. Sorry I wrote that too quickly. You have to compute the distance between the target and the actual positition. Here is the code :

 if(Vector3.Distance(targetLocation, transform.position) <= precision)
 {
   // Under a given precision, you consider that the target is reached
 }
avatar image audibles · Jan 31, 2014 at 02:28 AM 0
Share

Is this functionally different than what I already have?

avatar image Drakulo · Jan 31, 2014 at 08:21 AM 0
Share

Positions will not be exaclty equals but that's something completely invisible for the player. So no, this will be the same.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

MoveTowards inside Coroutine 2 Answers

[ANSWERED]Move object without Update or coroutine 2 Answers

Object keeps orbiting target location in a Coroutine 0 Answers

Move player left or right after x seconds 1 Answer

How do I make an Enemy charge through a point? 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