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 Maenor · Aug 16, 2015 at 07:26 AM · c#unity 5ai

How can i avoid the enemy to sight me through walls?

Hi there! I managed myself to find some resources around the internet and at the end this script came out! :3 Sadly, now i have the problem that i do not know how to avoid the enemy "Detecting" the player trough, for example a cube or a wall. Any ideas? =)

... Also, if for whatever reason the user getyour411 happens to be reading this i'll be glad for you to know that i never asked for a Script!! ¬¬ If so, i wouldn't bothered mattyman174 to explain me "HOW" not give me a way of avoid detection through walls.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform player;
     float distancefrom_player;
     public float look_range = 20.0f;
     public float agro_range= 10.0f;
     public float move_speed= 5.0f;
     public float damping = 6.0f;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         distancefrom_player = Vector3.Distance (player.position, transform.position);
     }
     void FixedUpdate()
     {
         if (distancefrom_player < look_range ) 
         {
             transform.LookAt(player);
         }
         
         if (distancefrom_player < agro_range) 
             
         {
             attack();
         }
     }
     
     void lookAt()
     {
         Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
     }
     
     void attack()
     {
         transform.Translate (Vector3.forward * move_speed * Time.deltaTime);
     }
 }
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
1
Best Answer

Answer by GuntranHoet · Aug 16, 2015 at 09:01 AM

Use a raycast from the enemy to the player. If the raycast hit's something other then the player or nothing; the player is out of sight. :)

Just make sure the raycast doesn't collide with the enemy itself ;)

Comment
Add comment · Show 8 · 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 Maenor · Aug 16, 2015 at 09:18 AM 0
Share

Wow xP It's the first time i dig into the "Raycast" o.O (I don't even know what it is) Would you be so cool to give me a link to the manual or api reference? =)

avatar image GuntranHoet · Aug 16, 2015 at 09:19 AM 0
Share

Sure: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

avatar image GuntranHoet · Aug 16, 2015 at 09:21 AM 0
Share

If you have different layers like: enemy, player, terrain, ... you can use a public Layermask variable to pick inside the inspector which layers your raycast can hit :)

avatar image Maenor · Aug 16, 2015 at 09:26 AM 0
Share

Thanks!! I'll take a look at it!! ;)

avatar image getyour411 · Aug 16, 2015 at 10:56 AM 1
Share

@$$anonymous$$aenor, it's not personal - even this question has been asked many times. Using the keywords from your title in Google yielded many duplicate questions/vids/tuts/etc. Your last two questions on this showed no effort or work of your own. At least here you've done some work, keep it up.

Show more comments

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

26 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

Related Questions

Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer

Nav Agent Moving Only Forwards And Backwards 0 Answers

How can i do a Very basic enemy AI or chase system? Unity 5 1 Answer

Distribute terrain in zones 3 Answers

Coroutines randomly stop working 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