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 /
  • Help Room /
avatar image
0
Question by n-carlson1 · Jul 04, 2016 at 12:58 PM · transformmovefollowchase

How to fix my chasing/attacking code?

So how the code currently works is, the enemy will find the player and move towards him. When he finds him, he will stop and then start attacking. If the player moves away though, the enemy will stop attacking and just sit there until the player comes back into range. How can I fix it so that when the player moves out of range, the enemy starts chasing again and then attacks as normal?

  float moveSpeed = 3f;
 float rotationSpeed = 3f;
 float attackThreshold = 3f; //distance within which to attack
 float chaseThreshold = 10f; //distance within which to start chasing
 float giveUpThreshold = 20f; //distance beyond which AI gives up
 float attackRepeatTime = 1f; //time between attacks

 bool attacking = false;
 bool chasing = false;
 float attackTime;
 Transform target; //the enemy's target
 Transform myTransform; //current transform data of the enemy

 void Update()
 {
 //rotate to look at the player
         float distance = (target.position - myTransform.position).magnitude;
         if (chasing)
         {
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         }
         //move towards the player
         if (chasing == true && attacking == false)
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         //give up if too far away
         if (distance >= giveUpThreshold)
         {
             chasing = false;
         //    attacking = false;
         }
         //attack, if close enough, and if time is OK
         if (distance <= attackThreshold && Time.time >= attackTime) //if attacking we want to stop moving
         {
             //attack here
             bossAttack.Attack();
             attackTime = Time.time + attackRepeatTime;
             print("Attacking!");
             attacking = true;
           //  anim.SetTrigger("AutoAttack");
             chasing = false;
         }
         else
         {
             //not currently chasing.
             //start chasing if target comes close enough
             if (distance <= chaseThreshold)  //if he gets to chase, and then you move out of range again, he won't chase again. he will only attack if comes into range again
             {
                 chasing = true;
               //  attacking = false;
                 //   print("Chasing!");
             }
         }
     }


I think that's all the necessary relevant code.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Different jump height 1 Answer

Hey Guys How to make a script '**private Transform Waypoint;**', 0 Answers

Transform.position of object not the same as shown on the inspector 1 Answer

How could I make an object move from left to right and it will come back from right? 0 Answers

Move to object if collision is triggered 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