Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 bywewe · Oct 09, 2016 at 11:32 PM · javascriptainavmeshdistancefollow player

Need support on my basic navmesh ai script to follow the player at a certain distance

I know this is kind of a noob question but I really need some quick help, I have this simple AI script that allows an object to follow the player on a navmesh, but for the life of me I cant figure out how to get the object to only follow me when I am in a certain distance of it, and when I am out of reach, the object will stop following the player. The javascript i have is below.

 var target : Transform;
 var navComponent : NavMeshAgent;
 var anim : Animator;
  
 function Start () {
 target = GameObject.FindWithTag("Player").transform;
         navComponent = this.transform.GetComponent(NavMeshAgent);
         anim = GetComponent("Animator");
 }
  
 function Update () {
  
         if(target) {
        
                 navComponent.SetDestination(target.position);
                 anim.SetFloat("speed",1);
         }
 }
 
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
1
Best Answer

Answer by Geometrical · Oct 09, 2016 at 11:48 PM

Here's some spoon feeding for you:

  var target: Transform;
  var navComponent: NavMeshAgent;
  var anim: Animator;
  var followDistance: float; // the distance in which to follow the player
  
  function Start() {
      target = GameObject.FindWithTag("Player").transform;
      navComponent = this.transform.GetComponent(NavMeshAgent);
      anim = GetComponent("Animator");
  }
  
  function Update() {
      if (target) {
          var distanceToTarget = Vector3.Distance(transform.position, target.transform.position);
          if (distanceToTarget <= followDistance) {
              navComponent.Resume();
              navComponent.SetDestination(target.position);
              anim.SetFloat("speed", 1);
          } else {
              navComponent.Stop();
              //anim.SetFloat("speed", 0);
          }
      }
  }





Comment
Add comment · Show 3 · 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 bywewe · Oct 10, 2016 at 12:06 AM 0
Share

@Geometrical thank you for the reply and the script but i get an error telling me at lines 14,17: UCE0001: ';' expected. and tells me to insert one at the end, but there are already semicolins at the end of those lines, what could be wrong?

avatar image Geometrical bywewe · Oct 10, 2016 at 12:08 AM 1
Share

Updated the code, try again. Select my answer as the answer if it works.

avatar image bywewe · Oct 10, 2016 at 12:21 AM 0
Share

@Geometrical thanks, it works now. I plan on learning basic scripting like this soon so I don't need to ask for help. $$anonymous$$uch appreciated.

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

127 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

Related Questions

Help with Unity 2D AI 0 Answers

NavMesh issue 0 Answers

Calculate distance between player and object 1 Answer

Problem with NavMesh 0 Answers

Random.insideUnitSphere: 180° possible? 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