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 /
  • Help Room /
avatar image
0
Question by danimiroiu5 · Oct 16, 2021 at 10:14 PM · enemy ai

How can i add movement range to my enemy script ?

hi im really new to unity so i would like to know how can i add movement range to my enemy im using the following script

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;

public class NewBehaviourScript : TargetScript { public Animator animator; public float health = 100; public NavMeshAgent navMeshAgent;

 bool isDead;
 float coolDown = 0.5f;
 Transform target;

 public GameObject deadEffect;
 // Start is called before the first frame update
 void Start()
 {
     target = GameObject.FindGameObjectWithTag("Player").transform;
 }

 // Update is called once per frame
 void Update()
 {
     if (navMeshAgent.remainingDistance < 2 && !isDead)
     {
         animator.SetTrigger("Attack");
     }


     if (isHit && coolDown <= 0 && !isDead)
     {
         Debug.Log("Hit");

         health -= 10;
         coolDown = 0.5f;

         if (health <= 0)
         {
             animator.SetTrigger("Dead");
             navMeshAgent.isStopped = true;
             isDead = true;
             StartCoroutine(Dead());
         }
         else
         {
             animator.SetTrigger("Hurt");
             navMeshAgent.isStopped = true;
         }

         isHit = false;

     }
     else if (coolDown <= 0)
     {
         if (!isDead)
         {
             navMeshAgent.isStopped = false;
             navMeshAgent.SetDestination(target.position);
         }
     }


     if (coolDown > 0)
     {
         coolDown -= Time.deltaTime;
     }
 }

 IEnumerator Dead()
 {
     yield return new WaitForSeconds(0.5f);
     GameObject _effect = Instantiate(deadEffect, transform.position, Quaternion.identity);
     Destroy(_effect, 3f);
     Destroy(gameObject);
 }



}

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 zORg_alex · Oct 17, 2021 at 10:50 AM

  else if (coolDown <= 0)
       {
           if (!isDead && Vector3.Distance(target.position, transform.position) < 10f)
           {
               navMeshAgent.isStopped = false;
               navMeshAgent.SetDestination(target.position);
           } else
               navMeshAgent.Stop();
       }

I guess this way. You should declare some stop distance field and use it instead of 10.

So every frame you are setting agents target and make it not stop... This is funny, like you push it with a whip every frame ))) stop this. He also have feelings... May be. I'd do a state bool, like

 public bool chasing;

And when

 if (!chasing && Vector3.Distance(target.position, transform.position) < ChaseDistance)

set it true and start chasing by setting agents target.

And check to stop chasing like

 if (chasing && Vector3.Distance(target.position, transform.position) > ChaseDistance)

set chasing false and make agent stop. This way it will do these things only once that distance get crossed, instead of every frame.

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 zORg_alex · Oct 17, 2021 at 10:51 AM 0
Share

Or did you wanted it to stop from wandering too far from it's origin?

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

171 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 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

Detect direction of enemy with MoveTowards 1 Answer

I'm making an RPG game, but there is a proplem, while the enemy is chasing and touched the player, he also push the player down like this. Do you know how to fix it ??? PLS HELP ME :( 0 Answers

How do i stop my enemy from not colliding? 1 Answer

Enemy AI movement Problems 1 Answer

Making a Box Collider sword do Damage 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