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 LuigiRulz324 · Apr 30, 2017 at 10:19 AM · playerainavmeshagentenemyai

NavMeshAgent track Player when in Range?

Hello,

So I know absolutely nothing about coding and I need a simple Enemy Script where they just walk around randomly but when the player is close AKA in range, it moves towards them till the player exits their range and goes back to walking around. I'd really like to have code that I can copy and paste into unity.

Thank you for any help!

Comment
Add comment · Show 3
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 SohailBukhari · May 03, 2017 at 02:44 PM 0
Share

Use Physics.OverlapSphere

unity official documentation is available https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

avatar image LuigiRulz324 SohailBukhari · May 03, 2017 at 03:26 PM 0
Share

That doesn't answer my question.

avatar image SohailBukhari SohailBukhari · May 03, 2017 at 03:58 PM 0
Share

write code yourself i just share the link of how you can check whether player is in range.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by path14 · May 04, 2017 at 07:31 AM

You'll have to learn some coding to make a decent game (or whatever you want to do with it). I can point you in the right direction. Copy and pasting won't get you anywhere.

  • Attach a Nav Mesh Agent to the Enemy. Have it disabled.

  • Call the Nav Mesh Agent in a script attached to the Enemy with Vector3 Distance.

Something like this: (Javascript)

 public var player : Transform;  
 
 function Awake () {
 player = GameObject.FindGameObjectWithTag ("Player").transform;
 nav = GetComponent (UnityEngine.AI.NavMeshAgent);
 }
 
 function Update() {
     var distance = Vector3.Distance(player.position, transform.position);
     
         if(distance <= 30) {
            
            nav.enabled = true;
            nav.SetDestination (player.position);
         }
        
         else  {
            this.nav.enabled = false;
         
         }
 }

I don't have the code for random enemy movement, but that's quite easy to write.

There are plenty of tutorials that Unity provides, which can help you. Please take the time to learn. It will pay off in the long run.

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 saadnation55 · Jul 07, 2020 at 05:59 PM

well this might solve your problem

public class Follow : MonoBehaviour { // Start is called before the first frame update

 public List<GameObject> waypoint = new List<GameObject> ();
 NavMeshAgent nav;
 int index = 0;
 int length;
 bool range = false;
 void Start () {
     nav = GetComponent<NavMeshAgent> ();
     length = waypoint.Count;
     nav.SetDestination (waypoint[index].transform.position);

 }

 // Update is called once per frame
 void Update () {

     Collider[] colliders = Physics.OverlapSphere (transform.position, 10);
     for (int i = 0; i < colliders.Length; i++) {
         if (colliders[i].gameObject.name == "player") {
             Debug.Log ("0");
             range = true;
             nav.SetDestination (colliders[i].gameObject.transform.position);
             break;
         } else
             range=false;
     }

if(range==true) return; if (nav.remainingDistance <= nav.stoppingDistance) { if (index >= length - 1) index = 0; else index++; nav.SetDestination (waypoint[index].transform.position); }

 }

}

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Runaway AI 1 Answer

Enemy's NavMesh not working 2 Answers

NavMesh flee. Ai flee from player. 4 Answers

NavMeshAgent does not follow player 0 Answers

Make an enemy only rotate with navmesh agent 1 Answer


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