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 /
  • Help Room /
avatar image
0
Question by BlankenshipM · Jun 17, 2017 at 06:14 PM · 2dtranslateienumeratormovetowards

Super Basic 2D AI - NPC not completing MoveTowards

This is my code:

 public class Minion : MonoBehaviour {
     public float speed = 4f;
     private float distance;
     private int decide;
     private int direction;
     private float waittime;
     private float targetposition;
     // Use this for initialization
     void Start () {
         float step = speed * Time.deltaTime;
         distance = (float) Random.Range (1,6);
         decide = Random.Range (0,2);
         print ("decide "+decide);
         print ("distance " + distance);
         StartCoroutine (Move (decide, direction));
     }
 
     // Update is called once per frame
     void Update () {
     }
 
     IEnumerator Move (float decision, float dir){
 
         //waittime = Random.Range (10, 12);
 
         if (decide == 0) {
             direction = Random.Range (0, 2);
             if (direction == 0) {
                 transform.position = Vector2.MoveTowards (transform.position, new Vector2 (transform.position.x + distance, transform.position.y), speed * Time.deltaTime);
             } else {
                 transform.position = Vector2.MoveTowards (transform.position, new Vector2 (transform.position.x - distance, transform.position.y), speed * Time.deltaTime);
             }
         } else {
             yield return new WaitForSeconds (10);
         }
     
     }

Currently, I only start the Move coroutine in Start() for testing purposes. I expected my game object to move to the target position, but instead only moves like 0.01 or -0.01 in it's x position then stops. How do I fix my code such that the NPC goes all the way to the target position? When placed in Update(), the NPC begins to jitter left and right, but again, only by increments of 0.01.

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 BlankenshipM · Jun 20, 2017 at 01:57 AM

solved it haha thanks guys

     public float speed = 4f;
     private float distance;
     private int decide;
     private int direction;
     private float waittime;
     private bool moving = false;
     private bool waiting = false;
     private float targetposition;
     Rigidbody2D rigidboy;
     // Use this for initialization
     void Start () {
         distance = (float) Random.Range (1,3);
         rigidboy = GetComponent<Rigidbody2D> ();
     }
 
     // Update is called once per frame
     void Update () {
         print ("DECIDED " + decide);
         if (!moving && !waiting) {
             decide = Random.Range (0, 2);
         }
         if (decide == 0) {
             if (!moving) {
                 print ("LETSGETMOVING!");
                 targetposition = transform.position.x + distance;
                 direction = Random.Range (0, 2);
                 moving = true;
                 //StartCoroutine (Move (decide, direction));
             } else {
                 /*Mathf.Abs (targetposition - transform.position.x) >= 0.1*/
                         if (direction == 0) {
                             print ("MOVING RIGHT!");
                             if (Mathf.Abs (Vector2.Distance (transform.position, new Vector2 (targetposition, transform.position.y))) >= 0.01f) {
                                 print("NOT THERE YET RIGHT!");
                                 transform.position = Vector2.MoveTowards (transform.position, new Vector2 (targetposition, transform.position.y), speed * Time.deltaTime);
                             } else {
                                 moving = false;
                                 print ("GOT THERE RIGHT!");
                             }
                         } else {
                             print ("MOVING LEFT!");
                             if (Mathf.Abs (Vector2.Distance (transform.position, new Vector2 (-targetposition, transform.position.y))) >= 0.01f) {
                                 print ("NOT THERE YET RIGHT!");
                                 transform.position = Vector2.MoveTowards (transform.position, new Vector2 (-targetposition, transform.position.y), speed * Time.deltaTime);
                             } else {
                                 moving = false;
                                 print ("GOT THERE LEFT!");
                             }
                         }
 
                 
             } 
         
         } else {
             if (!waiting) {
                 waiting = true;
                 print ("WAITING!");
                 StartCoroutine (Wait ());
             }
         
 
 
         }
     }
     
     IEnumerator Wait(){
         yield return new WaitForSeconds (2);
             waiting = false;
     }
Comment
Add comment · 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

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

155 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

Related Questions

Trying to simulate SNES party snake, non-grid. 0 Answers

How do I bring an instantiated projectile back towards the player if the player tells it to? Similar to a boomerang. 0 Answers

[Solved] Sliding garage door using transform.translate. 1 Answer

How do I stop two NPCs from walking into each other and being unable to move? 0 Answers

Sprite not moving properly? 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