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
3
Question by Gatsua · Jun 25, 2014 at 09:35 AM · c#2dmovement

make enemy chase player in topdown 2d

so I am trying to make the enemy chase the player and this is how far I have gotten:

 public Transform target;
 public float speed = 2f;
 private float minDistance = 1f;
 private float range;

 void Update ()
     {
         range = Vector2.Distance(transform.position, target.position);
 
         if (range > minDistance)
         {
             Debug.Log(range);
 
             transform.Translate(Vector2.MoveTowards(transform.position, target.position, range) * speed * Time.deltaTime);
         }
     }

But how come this guy is not moving a pixel? This script I place on the enemy prefab and the target is the player.

Comment
Add comment · Show 3
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 Cataclysm Entertainment · Jun 25, 2014 at 12:15 PM 0
Share

Hi, I'm not an expert, but you can also use Nav$$anonymous$$esh2D : AssetStore Link, and in this case, you call the specific function from Nav$$anonymous$$esh2D !

Or try with 3d vector, but with the z value at 0.

Hope this help you ;)

avatar image Gatsua · Jun 26, 2014 at 12:46 AM 0
Share

if I change "range" to "2" in $$anonymous$$oveTowards() the enemy will just run off in a direction and not stop, I wonder why..

avatar image caleb4204 · Jun 03, 2016 at 08:01 PM 0
Share

is there a way to make the enemy face the character, and than walk towards him, I have been trying to figure this out, I found raycasting method that will make the enemy see the character, and tthen execute the code in this page, but I cant make him look at and walk, he just hovers motionless

2 Replies

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

Answer by Maerig · Jun 26, 2014 at 01:10 AM

This is clearer in Vector3.MoveTowards reference, but Vector2.MoveTowards returns a point in-between the current and target transforms, so you can't use it this way with Translate.
Just assign the position returned by MoveTowards :

 transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);


Comment
Add comment · Show 5 · 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 Gatsua · Jun 26, 2014 at 07:59 AM 0
Share

I tried this and now its moving towards position 0.0 ins$$anonymous$$d of the players position.

avatar image Maerig · Jun 26, 2014 at 08:22 AM 0
Share

It does work for me. Did you check the player was properly referenced as target in the inspector ? Do you have other scripts which make the enemy move ?

avatar image Gatsua · Jun 27, 2014 at 02:47 AM 0
Share

great, thanks very much ! you were correct ^^ I had another object referenced :S

avatar image mohsalim · Jul 27, 2015 at 01:01 AM 0
Share

Thank you! This works perfectly

avatar image flynde · Jun 17, 2019 at 11:11 AM 0
Share

Why does this not work if I've a prefab enemy and prefab player ? It works with prefab player and not prefab enemy ?

avatar image
0

Answer by Lexumi_ · Nov 21, 2019 at 10:41 PM

but now the enemys go to that point where the player started but when I move the player they all go to that point not the Player how can i fix that?

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Uneven speed in 2d movement script 2 Answers

Bullet not moving from script 3 Answers

Stop Player movement when bool changes 2D 3 Answers

Emerging Gap when moving my "Snake" 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