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 /
avatar image
0
Question by donutkeith · Nov 26, 2012 at 07:35 AM · aienemyspherecast

How can I to tell if something blocks the vision of my enemy?

I have an enemy that follows a player if the player is withing a cone of vision. I need a way to stop the enemy from chasing the player if the player is behind something. I am trying to make a ray or spherecast that will go between player and enemy when the player is in the view of the enemy and if the ray or spherecast hits an object before hitting the player then the enemy should not know that the player is there. Here is a chink of the code that I am talking about.

 if((Vector3.Angle(targetDirection, front) <= (fieldOfView / 2.0)) && ((pToE).magnitude <= appHorizon ))
    {
       //Debug.Log("Saw The Play Theif");
       if(!seePlayer)
       {
          seePlayer = true;
       }
       if((!sawPlayer) && (seePlayer))    
       {
          sawPlayer = true;
       }
       //Casts a elongated sphere with    origin               radius         direction        distance      layermask[NOT USED OPTIONAL] [pToE is player to enemy (vector3)]   
       if(Physics.SphereCast(transform.position, transform.localScale.y, pToE, thingSeen, Vector3.Distance(playerPos, transform.position)))
       {
          if(thingSeen.collider.gameObject.tag == player.tag)
          {
             Debug.Log("found player");
          }
          else
          {
             Debug.Log(thingSeen.collider.gameObject.tag);
          }
       }

For some reason the player.tag is only shown for a few frames and then it goes away even if the player is still in the view of the enemy. In other words the "Found player" message only appears briefly and then goes away when the player is in the field of view. The Enemy does not see walls because the Debug.Log(thingSeen.collider.gameObject.tag); returns "untagged" message and never "cube" which is what every object other then player is named.

Comment
Add comment · Show 2
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 fafase · Nov 26, 2012 at 09:34 AM 0
Share

This tutorial http://unitygems.com/basic-ai-character/ has pretty much what you need. At the end is explained how to get the NPC not to see you if you are behind a building or any other collider.

avatar image donutkeith · Nov 28, 2012 at 02:15 AM 0
Share

Thanks for the link this tutorial ended up helping me with some directional stuff.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by sharpshot124 · Nov 26, 2012 at 09:46 AM

send a ray cast from the enemy to the player and see if the object it hits is the same as the player.

  RaycastHit hit = new RaycastHit();
     

 if(Physics.Raycast(enemy.transform.position, enemy.transform.position - player.transform.position, out hit))
 {
      if(hit.gameObject.tag == player.tag && Vector3.Angle(enemy.transform.forward, enemy.transform.position - player.transform.position) <= fieldOfView / 2)
           seePlayer = true;
 }
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 fafase · Nov 26, 2012 at 12:11 PM 0
Share

Yes, but I would say linecast does the same faster. Simply because you supply a start and end position (yours needs a tiny arithmetic). Then the linecast simply returns true if something collides (yours needs to check who collides :)). This is $$anonymous$$or issues, but since it is in the Update, I would consider them. Also, linecast would not need the hit parameter to be filled in.

 if(!Physics.Linecast(transform.position, player.position, layer$$anonymous$$ask)){
     //NPC sees the player
 }
 
avatar image sharpshot124 · Nov 26, 2012 at 06:21 PM 0
Share

@fafase yes you are right i only recently found Linecast in the docs so im still in the habit of doing it my way, silly me :p

avatar image donutkeith · Nov 28, 2012 at 02:16 AM 0
Share

Thanks very helpful.

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

13 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

Related Questions

Spherecast if hit.distance <=, move the otherway 1 Answer

SphereCastAll sees for miles 2 Answers

How to make an object jump? 0 Answers

switch prefab on collision 1 Answer

Need help with my enemy spawn script. 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