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
0
Question by AsAnAsterisk · Jul 14, 2017 at 08:11 AM · scripting problemaidetection

How to make enemy chase for longer distances than it's initial "notice" range

I have an enemy, that begins chasing the player when they get within a certain distance (in this case 5) however if the player exceeds the 5 distance while being chased the enemy will stop chasing. I want to make it so that, when the player enters the 5 distance, the enemy begins chasing them, but the enemy doesn't let up until the player is, say, 20 away, far greater than its initial notice range, but I'm unsure how to accomplish this. (There's more code than this but I just picked out the relevant portion).

 public class WastrelChase : MonoBehaviour
 {
 
     public Transform player;
     private Animator anim;
     private Rigidbody myRigidbody;
     private Vector3 moveDirection;
     public CapsuleCollider cc;
     public BoxCollider pc;
     public Slider healthbar;
     public Collider Attack;
     public GameObject hurt;
     public GameObject lunge;
     public GameObject growl;
     public GameObject shriek;
     
     
     void OnTriggerEnter(Collider attack)
     {
       if (attack.gameObject.tag == "PlayerAttack") {
      
           healthbar.value -= 15;
           anim.SetBool("IsInjured", true);
          }
          
       
      }
 
     // Use this for initialization
     void Start()
     {
         anim = GetComponent<Animator>();
         myRigidbody = GetComponent<Rigidbody>();
         cc.enabled = false;
         pc.enabled = false;
         transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         moveDirection = Vector3.zero;
 
         if (Vector3.Distance(player.position, this.transform.position) < 5)
         {
             Vector3 direction = player.position - this.transform.position;
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                         Quaternion.LookRotation(direction), 0.1f);
 
             anim.SetBool("IsIdle", false);
             if (direction.magnitude > .8)
             {
                 moveDirection = direction.normalized;
                 anim.SetBool("IsWalking", true);
                 anim.SetBool("IsStriking", false);
                 anim.SetBool("IsInjured", false);
             }
             else
             {
                 anim.SetBool("IsStriking", true);
                 anim.SetBool("IsWalking", false);
             }
 
 
         }
         else
         {
             anim.SetBool("IsIdle", true);
             anim.SetBool("IsWalking", false);
             anim.SetBool("IsStriking", false);
         }
         if (healthbar.value <= 0){
             anim.SetBool("IsVulnerable", true);
             }
          else
          {
             anim.SetBool("IsVulnerable", false);
          }
     }
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
0

Answer by MennoB · Jul 14, 2017 at 05:26 PM

I would create an "isFollowing" bool; and then use it as below. I also took the liberty to create separate functions for the different behaviours in Update(), this is what I would do but your preference may differ.


public class WastrelChase : MonoBehaviour
{
 public Transform player;
 private Animator anim;
 private Rigidbody myRigidbody;
 private Vector3 moveDirection;
 public CapsuleCollider cc;
 public BoxCollider pc;
 public Slider healthbar;
 public Collider Attack;
 public GameObject hurt;
 public GameObject lunge;
 public GameObject growl;
 public GameObject shriek;
 private float triggerDist = 5;
 private float maxDist = 20;
 private bool isFollowing = false;

 void OnTriggerEnter(Collider attack)
 {
 if (attack.gameObject.tag == "PlayerAttack") {

 healthbar.value -= 15;
 anim.SetBool("IsInjured", true);
 }


 }

 // Use this for initialization
 void Start()
 {
 anim = GetComponent();
 myRigidbody = GetComponent();
 cc.enabled = false;
 pc.enabled = false;
 transform.GetComponent().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
 }

 // Update is called once per frame
 void Update()
 {
 moveDirection = Vector3.zero;

 if (!isFollowing) 
 {
 NotFollowing ();

 if (Vector3.Distance (player.position, this.transform.position) < triggerDist) 
 {
 isFollowing = true;
 }
 }
 else 
 {
 Following ();

 if (Vector3.Distance (player.position, this.transform.position) > maxDist) 
 {
 isFollowing = false;
 }
 }

 CheckHealth ();
 }

 private void NotFollowing()
 {
 anim.SetBool("IsIdle", true);
 anim.SetBool("IsWalking", false);
 anim.SetBool("IsStriking", false);
 }

 private void Following()
 {

 Vector3 direction = player.position - this.transform.position;
 direction.y = 0;

 this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
 Quaternion.LookRotation(direction), 0.1f);

 anim.SetBool("IsIdle", false);
 if (direction.magnitude > .8)
 {
 moveDirection = direction.normalized;
 anim.SetBool("IsWalking", true);
 anim.SetBool("IsStriking", false);
 anim.SetBool("IsInjured", false);
 }
 else
 {
 anim.SetBool("IsStriking", true);
 anim.SetBool("IsWalking", false);
 }


 }

 private void CheckHealth()
 {
 if (healthbar.value <= 0){
 anim.SetBool("IsVulnerable", true);
 }
 else
 {
 anim.SetBool("IsVulnerable", false);
 }
 }
}


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 Bieere · Jul 14, 2017 at 01:20 PM

So i would recommend looking into a finite state machine to achieve the simple behaviour that you're looking for, where the idle, chase, and seek are all separate behaviours.

With that you can trigger a chase state, which will directly follow the player for that minimum view threshold, and once out of that state, move to a Seek state, which would move towards a direction for X amount of time, until either that enemy has either noticed the player, and has reentered a chase state, or has lost the player, and will revert to its idle state.

Unity does have an example of this, but there loads of resources online.

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 BIGTIMEMASTER · Jul 14, 2017 at 02:25 PM 0
Share

The GamerToGameDeveloper S3 tutorial on youtube covers a system like Bieere described.

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

145 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

Related Questions

Use of an AI script, and having it NOT go through walls using a character controller 0 Answers

AI targeting error 2 Answers

Making a queue for NavMeshAgents 0 Answers

Enemy AI, detect player when seen 2 Answers

Modify player controller script to chase player instead 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