• 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 JFGames · May 07, 2018 at 06:32 PM · transform.position

Vector3.MoveTowards Not working.

So I'm making a script that has an enemy navigate a maze using Vector3.MoveTowards and an array of transforms. The script detects if the enemy has the same x and y position as the current target point and if it does it moves towards the next one, and that part works fine. The problem is when it reaches some points on the map it refuses to change target, despite having the same x and y values as that point. I've been trying to figure this out for days now and I just can't get it working. Here's the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyNavigation : MonoBehaviour {
 
     public Transform[] Points;
     public float MoveSpeed;
     public int CurrentPoint;
     private bool TimeToMove;
 
     void Start ()  {
 
         CurrentPoint = 0;
         Debug.Log("StartPoint");
         transform.position = new Vector3(Points[0].position.x, Points[0].position.y, 0);
 
     }
 
     void Update()
     {
 
         if (transform.position.x == Points[CurrentPoint].position.x & transform.position.y == Points[CurrentPoint].position.y & CurrentPoint < Points.Length & TimeToMove == false)
         {
             CurrentPoint = CurrentPoint + 1;
             TimeToMove = true;
             StartCoroutine(WaitForBool());
             Debug.Log("Reached Point...");
         }
 
         transform.position = Vector3.MoveTowards(transform.position, Points[CurrentPoint].position, MoveSpeed * Time.deltaTime);
 
     }
 
     public IEnumerator WaitForBool ()
     {
         yield return new WaitForSeconds(0.2f);
         TimeToMove = false;
     }
 
 }

Thanks in advance. =)

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
Best Answer

Answer by Nomenokes · May 08, 2018 at 01:06 AM

One:

Due to floating-point error and Time.deltaTime differences if lagging, the positions may never be truly equal, just off by like .00001 or something. You should use a buffer so it will return true if within a certain area, not exactly on top of the point.

Two:

It's good practice to use conditional AND operator (&&) instead of binary AND (&).

Example:

 float buffer = .1f; //If within .1 meters, it will return true
 
 if ( 
 TimeToMove == false  /*you should probably put these ones first so it won't cause errors*/
 && CurrentPoint < Points.Length
 && Mathf.Abs(transform.position.x - Points[CurrentPoint].position.x) < buffer
 && Mathf.Abs(transform.position.y - Points[CurrentPoint].position.y) < buffer
 )

Hope that solves your stuff,

Nomenokes

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 JFGames · May 08, 2018 at 01:22 AM 1
Share

It works perfectly now! =) Thanks.

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

82 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

Related Questions

transforming position to the location of a variable 1 Answer

Transform position over X amount of time , how ?? c# 2 Answers

Fade From Position into Animation 0 Answers

Why can't I set transform.position on a child object? 2 Answers

Select Object and move on Right click Position - Error! 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