Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Jackop222 · Jul 14, 2015 at 02:48 PM · 2daimovetowards

Why do the enemies not follow me?

 using UnityEngine;
 using System.Collections;
 
 public class EAI : MonoBehaviour {
     public GameObject targetPos;
     public float speed = 2f;
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
 
 
         float step = speed * Time.deltaTime;
         //transform.position = Vector3.MoveTowards(targetPos.gameObject.transform.position.x, targetPos.gameObject.transform.position.y, step);
         transform.position = Vector3.MoveTowards (transform.position, targetPos.gameObject.transform.position, step);
     }
 }
 

Comment
Add comment · Show 7
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 Jackop222 · Jul 14, 2015 at 12:56 PM 0
Share

For speed i have 1 (They are moving, just towards the center of the screen) and for targetPos i have set the play ship.

avatar image NoseKills · Jul 14, 2015 at 05:54 PM 0
Share

Are you seeing any errors in the Console window?

avatar image Jackop222 · Jul 14, 2015 at 05:56 PM 0
Share

No i am not

avatar image maccabbe · Jul 14, 2015 at 05:57 PM 0
Share

Debug.Log(transform.position+" "+targetPos.gameObject.transform.position+" "+step);

avatar image Jackop222 · Jul 14, 2015 at 06:00 PM 0
Share

Yes, what am i looking for?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by wesleywh · Jul 14, 2015 at 06:43 PM

Well if that code doesn't work then try this other code out:

 this.transform.position = Vector3.Lerp(transform.position, targetPos.transform.position, step);

To have it follow a prefab you can't just drag your prefab into the inspector since that doesn't work, sad day I know. To get around this problem you will have to find your prefab as soon as it enters the game by its TAG or NAME. Here is some example code of how I might find the prefab by its tag:

   void Update () {
       if(FindTheTarget("Player") != null){
          float step = speed * Time.deltaTime;
          targetPos = FindTheClosestTarget("Player");
          transform.position = Vector3.MoveTowards (transform.position, targetPos.transform.position, step);
       }
   }
 GameObject FindTheTarget(TagToSearch:String){ 
      GameObject target; 
      target = GameObject.FindGameObjectWithTag(TagToSearch); 
      return target;
  }

This is counting on the fact that there is only going to be one player. If you want to have more than one player to check for then you will need to make target an array and loop through that array for the closest one.(That is if you wanted to find the closest target).

Let me know how this goes for you.

An alternative way of writing this would be the following:

  void Update () {
        GameObject target = GameObject.FindGameObjectWithTag("Player"); 
        if(target != null){
           float step = speed * Time.deltaTime;
           transform.position = Vector3.MoveTowards (transform.position, target.transform.position, step);
        }
    }
Comment
Add comment · Show 13 · 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 Jackop222 · Jul 14, 2015 at 06:46 PM 0
Share

No its not static, and changing the code didnt help, sorry :( but thanks!

avatar image wesleywh · Jul 14, 2015 at 06:54 PM 0
Share

I added some new code for you to try out.

avatar image Jackop222 · Jul 14, 2015 at 07:22 PM 0
Share

No sorry this just makes them move towards the center of the screen quicker, the center of the screen is where the player starts, so the position of the player must not be updating?

avatar image wesleywh · Jul 14, 2015 at 07:37 PM 0
Share

Yes, either you don't update your characters position or you are watching the wrong character. Try dragging your object back into the targetPos slot in the inspector again just to make sure you didn't put the wrong character to watch there.

avatar image Jackop222 · Jul 14, 2015 at 07:44 PM 0
Share

I have the correct character, but idk whether i should select the character from in game, the on in the hierarchy, or the prefab? As it does not let me select the hierarchy one!

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Ways to find bounds of the platform 1 Answer

MoveTowards... a smoother way? 2 Answers

How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers

some piston "ai"? 1 Answer

conserve momentum of vector3.moveTowards in 2D game 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