• 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 question was closed Jan 10, 2019 at 02:48 PM by el-mas-pro-470 for the following reason:

Other

avatar image
Question by el-mas-pro-470 · Jan 08, 2019 at 12:12 PM · playerenemycollision detectionenemy aihide

Enemy ai Ignore walls! help!

Hello, it turns out that I have created an AI script that works like this: the enemy has a distance of detection of the player, when the player enters that area, the enemy looks at it until the player moves away, but it turns out that the enemy detects my player through the walls or colliders (I mean, if I hide behind a wall he detects me the same), does anyone know anything? Thank you!

 #pragma strict
 
 var Target : Transform;
 var Animador : Animator;
 var Distance : float = 30;
 var rotationDamping : float = 2;
 
 function Start ()
 {
 Animador.SetBool("Aiming", false);
 
 }
  
  function Update()
  {
      var distance = Vector3.Distance(Target.position, transform.position);
  
      if(distance <= Distance)
      {
          LookAtTarget();
          Animador.SetBool("Aiming", true);
      }   
      if(distance > Distance)
      {
          Animador.SetBool("Aiming", false);
      }   
 
     }
  
  function LookAtTarget()
  {
      var dir = Target.position - transform.position;
      dir.y = 0;
      var rotation = Quaternion.LookRotation(dir);
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
  }
 
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Klarzahs · Jan 08, 2019 at 01:00 PM

Hi,

you could add a raycast before you actually turn towards the player. This can quickly become resource intense, especially if you have multiple enemies and call it in every Update() call.

You can use the following c# code snippet (untested)

 void Update() {
     var distance = Vector3.Distance(Target.position, transform.position);

     if (distance <= Distance && IsPlayerVisible()) {
           ...
     }
     ...
 }

 boolean IsPlayerVisible(){
     Vector3 dir = Target.position - transform.position;
     //save the first hit into a variable.
     RaycastHit hit;
     if(Physics.Raycast(transform.position, direction, out hit)){
             //eg check the tag of "hit.transform" - is it the player or a wall?
             //if it is a wall, return false
     }
 }

Remember, Multiple raycast per Update cycle are bad practice. You can add a counter, that gets increased every update and calls the raycast at value X.

Hope this helps, sorry, that I cant provide you with the javascript code

Comment

People who like this

0 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 el-mas-pro-470 · Jan 08, 2019 at 01:12 PM 0
Share

How to assembly the script? i am a begginer from C#

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Animator Animador;
     public GameObject EnemyObject;
     public float Distance;
     public float RotationDamping;
     public Transform Target;
     public Vector3 direction;
 
     void Update() {
         var distance = Vector3.Distance(Target.position, transform.position);
         if (distance <= Distance && IsPlayerVisible()) 
         {
             LookAtTarget();
         }
     }
 
     void LookAtTarget()
     {
         var dir = Target.position - transform.position;
         dir.y = 0;
         var rotation = Quaternion.LookRotation(dir);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * RotationDamping);
     }
     bool IsPlayerVisible(){
 
         Vector3 dir = Target.position - transform.position;
         //save the first hit into a variable.
         RaycastHit hit;
         if(Physics.Raycast(transform.position, direction, out hit)){
             //eg check the tag of "hit.transform" - is it the player or a wall?
             //if it is a wall, return false
         }
     }
     }
avatar image Klarzahs el-mas-pro-470 · Jan 08, 2019 at 01:25 PM 0
Share

Like this ;)

 ...
 private int delayCount = 0;
 private int maxDelay = 10;
 private bool wasLastVisible = false;
 
 ...
 
 bool IsPlayerVisible() {
         delayCount++;
         if (delayCount == maxDelay) {
             //reset the count
             delayCount = 0;
 
             Vector3 dir = Target.position - transform.position;
             //save the first hit into a variable.
             RaycastHit hit;
             if (Physics.Raycast(transform.position, direction, out hit)) {
                 if (hit.transform.tag.Equals("Player")
                     wasLastVisible = true;
                 else
                     wasLastVisible = false;
             }
         }
         return wasLastVisible;
 
     }


You'll need to find a good value for the maxCount. Too low will cost you resources due to many raycasts, too high and youll have a "lag" in your game - Even though the player now might be visible, you still take the old result.

avatar image el-mas-pro-470 Klarzahs · Jan 08, 2019 at 02:25 PM 0
Share

Thanks you!

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

When the enemy character shoots, the bullet won't go to the position of my player! 2 Answers

How Do I Ignore a Collision Between an Enemy and the Player? 1 Answer

Run coroutine only when player stays inside collider? 0 Answers

Enemy not taking damage on collisions. 2 Answers

NavMeshAgent does not follow player 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