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 JackoMiG · Jun 03, 2014 at 11:26 AM · ainavmesh

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

Hello,

I am relatively new to coding and am trying to make this AI script work with navmesh so that an object 'Enemy' does the following;

  • 1) looks at the player within a certain distance

  • 2) Moves towards the player within a certain

  • 3) once it is doing 2 and reaches 1.5 away from the player it stops

  • 4) it continuously follows the player until the player gets a certain distance away from it

  • 5) it moves around objects -

I did have 1 to 4 working properly but since ive added the Nav Mesh part to my script the Enemy completes 1 and 2 and 5 but not 3 or 4, it just follows the player continuously. Here is my script for the enemy object, the 'function chase' is the part I changed in order for the Nav Mesh thing to work. Note the lines I commented out:

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var chaseRange = 15.0;
 var attackRange = 1.5;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 var attackRepeatTime = 1;
 
 var TheDamage = 40;
 
 private var attackTime : float;
 
 var controller : CharacterController;
 var gravity : float = 20.0;
 private var MoveDirection : Vector3 = Vector3.zero;
 
 function Start ()
 {
     attackTime = Time.time;
 }
 
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         lookAt();
     }
     
     if (Distance > lookAtDistance)
     {
         renderer.material.color = Color.green;
     }
     
     if (Distance < attackRange)
     {
         attack();
     }
     else if (Distance < chaseRange)
     {
         chase ();
     }
 }
 
 function lookAt ()
 {
     renderer.material.color = Color.yellow;
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function chase ()
 {
     renderer.material.color = Color.red;
     
     //moveDirection = transform.forward;
     //moveDirection *= moveSpeed;
     GetComponent(NavMeshAgent).destination = Target.position;
 //    moveDirection.y -= gravity * Time.deltaTime;
     //controller.Move(moveDirection * Time.deltaTime);
 }
 
 function attack ()
 {
     if (Time.time > attackTime)
     {
         Target.SendMessage("ApplyDamage", TheDamage);
         Debug.Log("The Enemy Has Attacked");
         attackTime = Time.time + attackRepeatTime;
     }
 }
 
 function ApplyDamage ()
 {
     chaseRange += 30;
     moveSpeed += 2;
     lookAtDistance += 40;
 }

If there is any lack of clarity feel free to ask, thanks.

Comment
Add comment · Show 1
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 ownerfate · Jun 03, 2014 at 06:01 PM 0
Share

ah, i had something close to this, my script is much different than yours but $$anonymous$$e took advantage of the Distance function.

 var ObjectName : String; // name of the object the enemy will follow.
 
 var target : Transform; // this is the enemy.
 
 private var myTransform : Transform; // this is the auto filled when the game starts. ( no need to fill out anything there.
 
 var moveSpeed = 3; //how fast the enemy will move, ( if you have Nav$$anonymous$$esh agent, ingor this.
 
 var rotationSpeed = 3; //how fast the enemy turns ( doesn't seem to work.
 
  var Distance : int; // this is what you want. ( this is the distance you'll set to engage and disengage the player.
 
 
 function Update(){
    
 Walk();
 EnemyStop(); 
 
 }
 
 
 
 function Walk(){ // follows player when in range.
 if ( Vector3.Distance(target.position, transform.position ) < Distance){
 
     transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));
     
 gameObject.GetComponent(Nav$$anonymous$$eshAgent).enabled = true;
     gameObject.GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
 
 
 
     } 
     }
 
 function EnemyStop(){ // Stops the enemy when Player is out of range.
 
 if ( Vector3.Distance(target.position, transform.position ) > Distance){
 
 gameObject.GetComponent(Nav$$anonymous$$eshAgent).enabled = false; // now this is line not how i stopped my characters (mess around with it to see what works, and if anyone else has better ways than $$anonymous$$e, go with theirs).
 
 
 }
 }
 
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ohlin · Nov 18, 2014 at 10:47 AM

 var target : Transform; //drag player here in inspector
 var Distance;
 var chaseDistance : float = 10.0;
 var closeStopDistance : float = 2.0;
 
 
 var agent: NavMeshAgent; //drag navmesh enemy here in inspector
 
 function Start(){
 
 }
 
 
 function Update()
 {
 
     Distance = Vector3.Distance(target.position, transform.position);
     
     if (Distance < chaseDistance){
         
         agent.SetDestination(target.position);
     }
     if (Distance < closeStopDistance){
         agent.Stop(false);
     }

     //etc... just fill in all the distance based actions...    
     // if (enemyAgro = 10 && Distance < 3.0) then attackfunction() etc.        
                     
 
 }
Comment
Add comment · Show 2 · 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 ownerfate · Nov 18, 2014 at 10:56 AM 0
Share

thanks, this helps with some other things i was wondering about.

avatar image ownerfate · Nov 18, 2014 at 11:00 AM 0
Share

also looking over what i wrote my script was kinda obsolete after i reworked my enemy npcs and such, to work with animators ins$$anonymous$$d of individual models.

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

23 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

Related Questions

How to make enemy AI with NavMesh 2 Answers

Navmesh Trouble 1 Answer

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

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

Unity 3D Audio Clip Disabled 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