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 Kexon · Dec 30, 2014 at 03:57 AM · enemydetectionrangeenemyai

2D Enemy AI range problem

Hi, I am currently learning programming and making a 2D rpg game. I've encountered a problem which I haven't been able to fix for 2 whole days.. It's really getting to my nerves now and I can't find any solution. The problem is that the enemy detects the player/target, only if it was spawned near the player. If I spawn it far away and go near it, it doesn't detect my player. And when it has detected, it only moves to the position it detected the player and gets stuck there

Here's a gif:
alt text As you can see, the enemies are moving where I was. It's like the script is only checking the code once. Here's the code:

 public class EnemyAI : MonoBehaviour {
 [SerializeField]
 private float maxSqrDistance = 16, //4 meters 
 moveSpeed = 1f;
 public Transform target;

 void Update () {
 
     //Vector between the 2 entities
     Vector3 dir = target.position - transform.position;

     float distance = dir.sqrMagnitude, //Length squared because a sqrt is expensive
     dot = Vector3.Dot(transform.forward, dir.normalized);
     
     if (distance < maxSqrDistance)       //Is the distance less than what we want?
     
     {
         Debug.Log("Within range!");
         //Move in the direction, so towards the player
         transform.position += dir.normalized * moveSpeed * Time.deltaTime;
     }
 }


 }

Thanks for reading.

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

2 Replies

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

Answer by Limesta · Dec 30, 2014 at 05:19 AM

You don't update public Transform target.

Your AI is going off of the 0'th frame, and any frames afterward are not detected.

So simply add code at the beginning of your update frame that updates the position.

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 Kexon · Dec 30, 2014 at 12:13 PM 0
Share

How can I do that?

avatar image Kexon · Dec 30, 2014 at 01:33 PM 0
Share

Nvm, I solved it. Thanks!

avatar image
0

Answer by Regalith · Dec 30, 2014 at 04:22 AM

First off that private variable doesn't need to be serialized. My advice would be to switch the target variable type to GameObject. My guess is it's not updating the reference to your player since a transform was passed in as a public variable. Try this and see what happens:

  public class EnemyAI : MonoBehaviour 
  {
 
  private float maxSqrDistance = 16 //4 meters 
  public float moveSpeed = 1f; //So you can tweak move speed in the editor or in enemy prefabs
  public GameObject target;
  
  void Update () {
  
      //Vector between the 2 entities
      Vector3 dir = target.transform.position - transform.position;
  
      float distance = dir.sqrMagnitude, //Length squared because a sqrt is expensive
      dot = Vector3.Dot(transform.forward, dir.normalized);
      
      if (distance < maxSqrDistance)       //Is the distance less than what we want?
      
      {
          Debug.Log("Within range!");
          //Move in the direction, so towards the player
          transform.position += dir.normalized * moveSpeed * Time.deltaTime;
      }
  }
  
  
  }


Finally, why not use Vector2 instead of Vector3? It looks like a 2D game, so you dont need to keep track of a 3'rd axis.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy VISION range? Help! 0 Answers

How to make enemy check if it doesn`t see player for certain times it changes state 1 Answer

How to correctly do an enemy ranged attack script? 0 Answers

How to make a game object detect if any of multiple game objects are in range 2 Answers

Enemy Patrol - Random Walk 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