• 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
Question by fr0stk1ller · Apr 22, 2014 at 04:39 PM · 2djavascriptmovementtracking

Why does my enemies' movement change whenever my player moves toward and away from the enemies' current position?

Whenever I stand still, the enemy starts moving toward me, but whenever I start moving toward it, it starts moving away, and when I move away from the enemies' position, it starts moving more quickly toward my position.

 var Enemy : GameObject;
 
 var Player : GameObject;
 
 var Range : float;
 
 var MaxRange : float;
 
 var Speed : float;
 
 
 function Start () 
 {
 
     Enemy = GameObject.FindGameObjectWithTag("Enemy");
 
     Player = GameObject.FindGameObjectWithTag("Player");
 
 
 }
 
 function Update () 
 {
 
     Range = Vector2.Distance (Enemy.transform.position, Player.transform.position);
 
     if (Range <= MaxRange) 
     {
         transform.Translate(Vector2.MoveTowards (Enemy.transform.position, Player.transform.position, MaxRange) * Speed * Time.deltaTime);
     }
 }
Comment

People who like this

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

Answer by robertbu · Apr 22, 2014 at 04:46 PM

I assume this is your script on the enemy? It is strange to do a FingGameObjectWithTag() to find the game object this script is attached to. The real problem here is your use of Translate(). Translate() movements are relative, and, by default, they are local. Your MoveTowards() is using world positions. The typical use of MoveTowards() would be

 transform.positon = Vector2.MoveTowards (transform.position, Player.transform.position, Speed * Time.deltaTime);

'Note that I've removed your 'MaxRange'. Speed will be Units per second.

Comment

People who like this

0 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 fr0stk1ller · Apr 23, 2014 at 02:42 PM 0
Share

Thank You so much! Can you help me create a limit to how close the enemy can get to my player?

avatar image robertbu · Apr 23, 2014 at 04:19 PM 0
Share

There is a simple fix that causes and immediate stop. Create a MinRange variable and change line 27 to:

 if (Range <= MaxRange && Range >= MinRange) 

If you need something more complex, open a new question please. Typically new issues should be asked as new question to keep the Q and A clean for people searching out answers on Unity Answers.

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

20 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

Related Questions

Locking move direction and rotation in JavaScript 1 Answer

How to create random movement in 2D 2 Answers

My controls are inverted when facing sideways. 1 Answer

how to move an object in 2D sense its position? 0 Answers

Moving an object with a collider in 2d? 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