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 Nodgorod · Sep 27, 2016 at 10:38 AM · collidertriggerplayerenemynavmeshagent

NavMesh problem it does not respond corectly

I have a GameObject Enemy which is supposed to chase the player all around the game board using its NavMeshComponent. The concept is: the enemy has a SphereCollider of radius 6 and everytime the player enters that collider, the enemy starts chasing him for as long as the player finds itself in the enemy's collider. Once the player exists the SphereCollider, the enemy enters an idle state and stays this way until the enemy detects the player again. I will provide you with the script for the enemy instance:

using UnityEngine; using System.Collections; using UnityEngine.UI; public class TrollMovement : MonoBehaviour {

 private NavMeshAgent agent;
  GameObject target;
 public Image img;
 public int healthEnemyPoints=100;

 bool follow=false;




 void Start () 
 {
     agent = GetComponent<NavMeshAgent> ();
     target = GameObject.Find ("Player");
     img.enabled = false;

     agent.speed = 15f;


     agent.acceleration = 50f;

     agent.destination = target.transform.position;
 }
 

 
 void OnTriggerEnter(Collider other)
 {

     if (other.CompareTag ("Player"))
     {
         follow = true;
         img.enabled = true;

     }
     if (other.CompareTag ("Bullet"))
         healthEnemyPoints -= 1;


 }

 void OnTriggerExit(Collider other)
 {
     if (other.CompareTag ("Player")) {
         Debug.Log ("A iesit playerul");
         img.enabled = false;
         follow = false;
         agent.enabled=false;

     }
 }

 void Update ()
 {

     if (healthEnemyPoints <= 0) {
         agent.enabled=false;
         Destroy (gameObject);

     }
     /*if (Input.GetKeyDown (KeyCode.Space))
         //Destroy (this.gameObject);*/

     if (follow == true) {
         agent.enabled = true;
     
     }
 }

}

To clarify, the Image here is used just to show me whether the triggers are corectly triggered. My problem would be that the enemy, after one cicle of active-idle state, starts behaving strangely. Firstly, it doesn't start chasing the player immeadiately and morever, once it detects the player, it chases him only a fraction of move, after which the enemy enters its idle state.

I'm terribly sorry for my coding style, but I would like to know what could be changed about my code in order to achieve the perfect performance of my enemy. Should you have another idea of script, please post it!

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to handle multiple colliders for one enemy and bullet. 3 Answers

Will an empty gameObject block my trigger? 1 Answer

Decreasing Player Health On Collision with Enemy 2 Answers

Enemy's NavMesh not working 2 Answers

Problem with Flashlight in Horror Game 2 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