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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by KM Studios · Aug 23, 2012 at 05:35 AM · c#ainavigationpro

Unity Pro Navigation AI c# animations and stopping?

So I am using Unity pro and I have a terrain with a navmesh bake and my monster follows the player, but of course he just runs right through him. Also, he is always chasing the player no matter how far away.

I was wondering how I can stop the enemy when he gets within a certain distance of my player (just simple colliders I suppose?).

More importantly I have walking and attacking animations and I was wondering how I can play those when he is moving and when is attacking. I know how to write the code for c# but I pretty confused on how or where to implement it.

Also, I had this working before on a different script, but now that I have the Pro version everything is different with AI. I had it so that the AI would check the player's flashlight component to see if it was enabled or not, and when it is, the enemy can start to follow from a farther distance, otherwise the enemy has to be much closer. I know how to write the codes for that, I think, but how can I get my enemy to NOT move based on an if statement? The AI follow that is below I found on an online tutorial but it doesnt really help as far as telling the enemy what to do when he has reached the player and etc.

So far I just have this to get the AI to follow:

 using UnityEngine;
 using System.Collections;
 
 public class navmeshagentai : MonoBehaviour 
 {
     public int distanceaway = 80;
     public GameObject go;
     public Transform m_Player;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         
         GetComponent<NavMeshAgent>().destination = m_Player.position;
     }
     }

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
Best Answer

Answer by fafase · Aug 23, 2012 at 05:46 AM

Ok, add a Character Controller to your enemy and fetch the controller to move him with Move().

Look there:http://answers.unity3d.com/questions/229547/enemies-dont-collide.html

for your enemy to stop chasing you you have two possibilities:

1-Using Vector3.Distance(transform,transform) like so:

 if(Vector3.Distance(transform.position,player.position)< range)Attack();

You might want to cut the AI with InvokeRepeating

 var player:Transform;
 var chase:boolean;
 var range:int;
 
 function Start(){
 player=GameObject.Find("Player");
 chase = false;
 InvokeRepeating("CheckDistance",1.0,1.0);
 }
 function Update(){
 if(chase)Attack();
 }
 function CheckDistance(){
 if(Vector3.Distance(transform.position,player.position)<range)chase =true;
 else chase =false;
 }

This way you check only once every second.

2- Using a trigger sphere, add a sphere collider to your enemy prefab and tick IsTrigger. The radisu of the sphere determines the distance the enemy starts to chase:

 var player:Transform;
 var chase:boolean;
 function Start(){
 player=GameObject.Find("Player");
 chase = false;
 }
 function Update(){
     if(chase)Attack();
 }
 function OntriggerEnter(other:Collider){
 if(other.gameObject.tag=="Player")chase=true;}
 function OntriggerExit(other:Collider){
 if(other.gameObject.tag=="Player")chase=false;}

it is all in UnityScript but C# works on the same principle.

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 KM Studios · Aug 23, 2012 at 02:52 PM 0
Share

Alright I see what you mean, I'll try this later today and let you know what works

avatar image KM Studios · Aug 24, 2012 at 06:46 AM 0
Share

I ended up doing something different but this helped me reach that conclusion so thank you

avatar image fafase · Aug 24, 2012 at 06:53 AM 0
Share

NO worries, how different? That could be interesting to know.

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

8 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

C# multiple AI Waypoint tutorial 0 Answers

A node in a childnode? 1 Answer

Coroutine to WaitForSeconds() on enemy AI causing all instances of AI to pause. Solution? 1 Answer

Problems when i have multiple enemies on the same scene C# 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