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 Jason991 · Dec 09, 2013 at 03:49 AM · c#aipathfindingwaypoint

Enemy go haywire after player approaching it, how to properly implement waypoint and chase player.

Hi guys i have assign in a way that if within the range the enemy will chase the player and if out of range will back to its waypoint. The problem is when it back to its waypoint and while my player near the enemy, it will go out of direction and other way then only back and chase player. Do you guys know what is the problem?

Enemy.cs

     public bullettower1 bulletPrefab = null;
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     
     float range = 10.0f;
     float range2 = 15.0f;
     float range3 = 4.0f;
     float range4 = 20.0f;
     
     
     float nextFire = 0;
     float fireRate = 1.5f;
     float stop = 3.0f;
 
     
 Transform myTransform;
     
 public Transform[] waypoints;
      public int waypointId = 0;
     
     
     
     [SerializeField]
     public Transform hand;
     
     
     
     void Awake(){
         
         myTransform = transform;
     }
 
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         animation.CrossFade("idle");
         target = go.transform;
         Patrol ();
 
         
     }
     
     
     
     void Patrol(){
         
         
         
         if (Vector3.Distance(transform.position, waypoints[waypointId].position) < 2){
             
             waypointId++;
             
             if (waypointId >= waypoints.Length) waypointId = 0;
         }
             animation.CrossFade("run");        
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(waypoints[waypointId].position - transform.position), rotationSpeed*Time.deltaTime);
            transform.position += transform.forward * moveSpeed * Time.deltaTime; 
             
             
             
             
         }    
     
     
     
     
     // Update is called once per frame
     void Update () {
         
       
     float distance = Vector3.Distance(myTransform.position, target.position);
     if (distance <= range2 &&  distance >= range){
     
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     
     }
  
  
     else if(distance <= range && distance > stop){
   
     //move towards the player
     animation.CrossFade("run");        
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     
    
     }
     else if (distance <=stop) {
     
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     }
     
     else if (distance >= range) {
     
     animation.CrossFade("idle");
         }
 
  
     if (distance <= range3)
         {
             
             animation.CrossFade("shoot");
             if (Time.time > nextFire)
     {
             nextFire = Time.time + fireRate;
             GameObject g = (GameObject)Instantiate(bulletPrefab.gameObject, hand.position, Quaternion.identity);
             
             bullettower1 b = g.GetComponent<bullettower1>();
 
                    
             b.setDestination(target.transform);    
                 
             
             
             
                 
                 
     }    
             
             
         }
         
         else if (distance <= range4)
         
         {
             Patrol();
         
     
         
         
     }
 }
     
 }
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

Answer by J-R-Wood · Dec 09, 2013 at 05:19 AM

Buying the AI made simple from Walker Boys studio is a great investment, could save you some time.

Comment
Add comment · Show 1 · 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 Jason991 · Dec 09, 2013 at 05:28 AM 0
Share

hmm this is another solution as well, i know that my knowledge towards program$$anonymous$$g is still very limited but i can feel that im close to making this work.

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

17 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

Related Questions

RTS dynamic formation width and length 0 Answers

Send an enemy back to its spawn point using waypoints 2 Answers

How can I prevent a character from ignoring it's waypoint path? 1 Answer

Boids/Flocking Tutorial 0 Answers

AI Pathfinding using RayCast 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