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 /
avatar image
1
Question by Ondrak500 · Aug 22, 2019 at 11:46 PM · animationmovement scriptenemy ai

How to make enemy detect a player in a certain range?

Hello i created a CSharp code that should cover my enemy AI (enemy detects player in his range, follows him and if my character gets out of enemy detection range he stops following my character). Every works fine except that enemy follows him even outside the Enemy detection range that i set trought the my script. I need my Enemy to follow the player in certain range .

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyRange : MonoBehaviour
 {
     
     public float speed;
     public float range;
     public Transform player;   
     
         
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (Vector3.Distance(player.position, transform.position) <= range)
 
         {
             transform.position = Vector2.MoveTowards(transform.position, player.position, 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

3 Replies

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

Answer by omegaaicoding · Aug 23, 2019 at 12:22 AM

I can't see your other scripts or game objects that could be affecting this. All I can see in your Update method is a condition that starts the enemy following the player. There isn't anything to deactivate the following. Try adding "if (vector3.distanct(...,...) > range) {//go do something else, start patrolling waypoints, etc.}"

Comment
Add comment · Show 3 · 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 Ondrak500 · Aug 23, 2019 at 10:32 AM 0
Share

Hello thank you for your answer. I tried what you said but it seems like it doesnt work(maybe iam doing something wrong?)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : $$anonymous$$onoBehaviour
 {
 
     public Transform player;
     private float range = 1f;
     public float speed;
     public Animation anim;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Vector2.Distance(transform.position, player.position) <= range)
         {
             transform.Translate(Vector3.$$anonymous$$oveTowards(transform.position, player.position, speed * Time.deltaTime));        
     
         }
         else
         {
             if(Vector2.Distance(transform.position, player.position)  > range)
             {
                 anim = GetComponent<Animation>();
 
             }
         }
 
     }
 }
 

avatar image omegaaicoding Ondrak500 · Aug 24, 2019 at 09:27 PM 0
Share

I don't think the structure of your code will ever run the second if-statement. Because the first if-statement has executed, the else-if will never execute.

Try removing the else and adding a debug log just to make sure the code line runs...if it does, then you will be left to whatever you want the enemy action to be.

'// Update is called once per frame void Update() { if (Vector2.Distance(transform.position, player.position) <= range) { transform.Translate(Vector3.$$anonymous$$oveTowards(transform.position, player.position, speed * Time.deltaTime));
}

          if(Vector2.Distance(transform.position, player.position)  > range)
          {
                 Debug.log("Enemy is out of range and code executed.");
                  anim = GetComponent<Animation>();
                 /*create a different action later on on what you want the enemy to do, make sure you change the transform of the enemy in some way or it will still follow you AND run your animation at the same time.*/

 
          }
      }
 
  }

} `

avatar image Ondrak500 omegaaicoding · Aug 25, 2019 at 01:02 PM 0
Share

Hey man thanks for the answer. I forgot that i need to add the "else" statement to stop the following. So thanks.

avatar image
1

Answer by logicandchaos · Feb 15, 2021 at 04:47 PM

You can also use OverlapCircle() to get players in range.

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
avatar image
0

Answer by quimerico · Feb 15, 2021 at 03:45 PM

Awesome answer, I added the sigle line of the if statement to the stalker code I made from a tutorial, and it worked perfectly! So simple :}

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

281 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 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

How do I stop moving when attacking? 1 Answer

Can I make animations snap to a frame? 1 Answer

Character Animator controller/movement 1 Answer

The problem of Enemy scale when he follow the player (script) . How to fix? 0 Answers

How to Apply Animations to Script 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