Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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
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
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
Add comment · 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 $$anonymous$$inRange variable and change line 27 to:

 if (Range <= $$anonymous$$axRange && Range >= $$anonymous$$inRange) 

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.

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

Moving Player Up and Down in 2D 1 Answer

how to move an object in 2D sense its position? 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