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 /
avatar image
0
Question by GraviterX · Jan 21, 2014 at 02:42 AM · ainavmesh

Navmesh Trouble

Hi all. I'm starting to learn Unity's navmesh system, and I've been trying to apply it to an AI script I have already scripted. (Below) How can I make it so that it uses the navmesh agent in this script? Thanks!

 var agent : NavMeshAgent = GetComponent.<NavMeshAgent>();
  
 //wandering vars
 public var wanderSpeed = 2.0;
 public var wanderRotSpeed = 5.0;
 public var wanderRadius = 10.0;
 public var wanderRayDistance = 5.0;
 public var wanderPauseMin = 2.0;
 public var wanderPauseMax = 6.0;
 private var basePosition : Vector3;
 private var currentDestination : Vector3;
 var walk : AnimationClip;
  
 //chase vars
 var chaseDistance : float = 10.0;
 var chaseSpeed : float = 3.0;
 var chaseRotSpeed : float = 5.0;
 var run : AnimationClip;
  
 //attack vars
 var attackDistance : float = 3.0;
 var attackRate : float = 0.25;
 var attack : AnimationClip;
  
 //state setup
 var state : aiState;
  
 InvokeRepeating("StateLogic", 0.0, 0.05);
  
 function Start(){
     if(target == null)
         target = GameObject.FindWithTag("Player").transform;
     ChooseNextDestination();
     yield StateMachine();
 }
  
 function StateLogic(){
     var distanceToTarget = (target.position - transform.position).sqrMagnitude;
     if(distanceToTarget <= attackDistance*attackDistance)
         state = aiState.attacking;
     else if(distanceToTarget <= chaseDistance*chaseDistance)
         state = aiState.chasing;
     else
         state = aiState.wandering;
 }
  
 function StateMachine(){
     while(true){
         switch(state){
             case aiState.wandering:
                 yield Wander();
                 break;
             case aiState.chasing:
                 Chase();
                 break;
             case aiState.attacking:
                 yield Attack();
                 break; 
         }
         yield;
     }
 }
  
 function Wander(){
     RotateToward(currentDestination, wanderRotSpeed);
     MoveForward(wanderSpeed);
     //BroadcastMessage("PlayAnimation", "walk");
     var destPosZeroY = currentDestination;
     var currentPosZeroY = transform.position;
     destPosZeroY.y = 0;
     currentPosZeroY.y = 0;
     if((destPosZeroY - currentPosZeroY).magnitude < 1.0){
         yield WaitForSeconds(Random.Range(wanderPauseMin, wanderPauseMax));
         ChooseNextDestination();
         animation.Play("walk");
     }
 }
  
 function ChooseNextDestination(){
     var randOffset : Vector2 = Random.insideUnitCircle * wanderRadius;
     currentDestination = basePosition + new Vector3(randOffset.x, transform.position.y, randOffset.y);
     Debug.DrawLine(transform.position, currentDestination, Color.white);
 }
  
 function Chase(){
     RotateToward(target.position, chaseRotSpeed);
     MoveForward(chaseSpeed);
     animation.Play("run");
  }
  
 function Attack(){
     yield WaitForSeconds(attackRate);
     animation.Play("attack");
 }
  
 function RotateToward(targetPos : Vector3, rotSpeed : float){
     targetPos.y = transform.position.y;
     var rotation = Quaternion.LookRotation(targetPos - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);     
 }
  
 function MoveForward(moveSpeed : float){
     transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
 }
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 mossman3333 · Jun 22, 2014 at 06:44 PM

Replace RotateToward and MoveForward calls with a single call to agent.SetDestination with a target 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

19 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

Related Questions

Make AI object with Nav Mesh work with script to stop following player? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I limit the path of a NavMeshAgent? 0 Answers

How to add variables to Components (Nav Mesh Agent)? 0 Answers

How can i modify the navMesh path? 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