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 Xetros · Aug 30, 2015 at 09:19 PM · aienemyunresponsive

NPC AI Unresponsive after spotting player once

Hi there,

I am working on an enemy AI for my game. So far I managed to get most of it working. The enemy patrols to certain waypoints which is perfectly fine. In one of my scripts I have a variable called playerInSight. This variable turns to true whenever the player enters a collider which is on one of the child objects of the enemy. I just made a cover system where it finds the nearest possible cover and moves towards that position. If I tick the variable related to that process in the inspector the enemy goes into cover perfectly (which is the playerAware variable). However, if the playerInSight boolean is set to true once in the scene, the enemy becomes unresponsive and doesn't continue with his patrol and he is also not able to get into cover anymore (since he is unresponsive to anything except for the playerInSight variable). Because of this, I think the problem lays in the script where that variable is created. This is the script, its called EnemySight:

 #pragma strict
 
 var playerInSight : boolean = false;
 var playerAware : boolean = false;
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.tag == "Player")
     {
         playerInSight = true;
         playerAware = true;
     }
 }
 
 function OnTriggerExit(other1 : Collider)
 {
     if(other1.tag == "Player")
     {
         playerInSight = false;
     }
 }

Another possibility is that there is something wrong in the scripts which handles the shooting and rotating towards the player of the enemy. I'll post this script here aswell:

  #pragma strict
  
 var enemySight : EnemySight;
 var readyFire : boolean = false;
 var rotationSpeed : float = 5.0;
  
 private var anim : Animator;
 private var player : GameObject;
 private var playerStats : PlayerStats;
 
 function Start ()
 {
     anim = GetComponent(Animator);
     player = GameObject.Find("Player");
     playerStats = player.GetComponent(PlayerStats);
     enemySight = GetComponentInChildren(EnemySight);
 }
 
 function Update()
 {
     if(enemySight.playerInSight)
     {
         var direction : Vector3 = player.transform.position - transform.position;
         var rotation = Quaternion.LookRotation(direction);
         
         rotation.z = 0.0;
         rotation.x = 0.0;
         
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
         readyFire = true;
     }
     
     else
     {
         readyFire  = false;
     }
     
     if(readyFire)
     {
         anim.SetBool("Shooting", true);
         playerStats.playerHealth -= 50 * Time.deltaTime;
     }
     
     else
     {
         anim.SetBool("Shooting", false);
     }
 }
 

I feel like i've tried everything to fix this issue without results. Is anyone willing to give me a hand with this? If I need to post some of my other scripts which work together with these two to actually make the AI, i'll post them here aswell.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xetros · Aug 31, 2015 at 12:21 PM

For people who stumble upon the same problem, managed to fix it by restarting the NavMeshAgent with the Resume() function.

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
avatar image
0

Answer by Rob Howard · Aug 31, 2015 at 12:21 PM

@Xetros So I'm pretty new to Unity, but I'll take a stab at it. I think it has something to do with all the AI behavior being placed inside Update. Update being a big loop, it's probably messing with everything.

I'm just guessing, but I think you might want to use a custom event to trigger the AI behavior, so when the player hits the trigger, an event fires. The enemy AI has an event listener on it that modifies it's behavior, in essence, calling your code you wrote under Update.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is the Player Being Looked at 1 Answer

EnemyAI C# Error 1 Answer

How to make an AI like slender ? 1 Answer

AI Following Problem 1 Answer

sprite changes z position without any input to do so 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