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 putlucky · Apr 14, 2014 at 05:20 PM · c#artificial intelligence

A Problem with my AI

Hello, I am trying to write a script in which the AI will follow the player and then when it reaches a certain distance stop. This stop will enable me to later add in an attack animation. However, I also want the enemy to CONTINUE to follow the player when it moves away, once again until it reaches that certain distance with which to stop and Attack. I currently have it so that the enemy follows the player and stops when it reaches too close. However, I don't have the second part, when the enemy continues to follow. Its a relativly short script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public GameObject target; 
     public bool foundPlayer; 
     public Vector3 myPosition;
     public float speed = 1.0f; 
 
     public EnemyAI myAI; 
 
 
     void Start () {
     // Locate the Player object. 
         if(GameObject.FindWithTag("Player"))
         {
             myAI = GetComponent<EnemyAI>(); 
             foundPlayer = true;
         }
 
     }
 
     void Attack() 
     {
         myPosition = transform.position;
         transform.position = Vector3.Slerp(myPosition, target.transform.position, speed * Time.deltaTime);
 
 
     }
 
     void Update () {
         if(foundPlayer == true) 
         {
 
             if(Vector3.Distance(myPosition, target.transform.position)<2.0f)
             {
                 myAI.enabled = false;
 
             }  else 
             {
                 Attack();
 
             }
 
         }
 
 
     }
 }
 

Any help you can give me would be awesome, thanks!

Comment
Add comment · Show 1
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 fafase · Apr 14, 2014 at 07:05 PM 0
Share

Have a look at this : http://unitygems.com/basic-ai-character/ it has exactly what you need, following, stop and attack and even filed of view and not seeing through walls.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Radivarig · Apr 14, 2014 at 06:57 PM

From what I see, your attack function is actually move function.

I would use something like this

 public float minDistance = 2f, maxDistance = 10f;
 
 void MoveToTarget()
 { 
     myPosition = transform.position;
     transform.position = Vector3.Slerp(myPosition,target.transform.position, speed * Time.deltaTime);
 }
 
 void Attack(){
 //set attack here
 }
 
 void Idle(){
 //set idle here
 }
 void Update()
 {
     float distToTarget = Vector3.Distance(this.position, target.position);
 
     if(distToTarget > minDistance)
     {
         if(distToTarget > maxDistance)
             Idle();
         else MoveToTarget();
     }
     else Attack();
 }

Now your AI will start moving to player when he gets in range and when he reaches he will attack, if the player moves away the AI will follow and repeat attack when near player again.

You can additionally add a timer for following, so the AI don't chase forever if attack is not called, or check if distance from a certain point from which AI is allowed to go exceeds your value then stop following and return back.

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 fafase · Apr 14, 2014 at 07:04 PM 0
Share

Well I would not use transform to move a character as it will imply ignore any obstacles. Your logic is right though.

avatar image Radivarig · Apr 15, 2014 at 05:47 AM 0
Share

I have taken this example because it is the simplest, and he already used it in his code, and if we wanted to be precise on the subject of moving AI then we would have to use navigation mesh which is a discussion on its own.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is this making my AI move? 0 Answers

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

Seek behavior has object following its target but doesnt point towards it, how do i make the object to point at the vector but without ruining its velocity? 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