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 SoylentRobot · Apr 11, 2014 at 07:33 PM · raycastenemyraycastingsight

Enemy line of sight

I'm having trouble with raycasting! Basically, my enemy has a sphere collider I'm using as its maximum sight distance, and when the player enters this, it tells the enemy to chase the player. That part works fine, but the problem is that the enemy can "see" the player through walls because the collider goes through walls. I'm not fussed about field of view since these enemies are supposed to have a 360 degree line of sight. I'm trying to use raycasts so the enemy will ignore the player if there is a wall in between them, but nothing I'm trying has worked so far. Here's my enemy sight code, shamelessly looted from the stealth tutorial.

 {
     private GameObject playerTarget;
     private SphereCollider col;
     
     void Awake ()
     {
         playerTarget = GameObject.FindGameObjectWithTag ("Player");
         col = GetComponent<SphereCollider>();
     }
 
     void OnTriggerEnter (Collider other)
     {
         if(other.gameObject == playerTarget)
         {
             Vector3 direction = playerTarget.transform.position - transform.position;
             {
                 RaycastHit hit;
                 
                 if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))
                 {
                     if(hit.collider.gameObject == playerTarget)
                     {
                         Debug.Log ("player in sight");
                         //Stuff happens that makes the enemy chase the player
                     }
                 }
             }
         }
     }
 }

This code doesn't work 100% of the time. Sometimes I get the debug "player in sight" sometimes I don't, if the player goes into the collider when a wall is between them, nothing happens, but re-entering the collider with nothing in the way still has nothing happen. It's like the raycast happens only once, regardless of how many times the player enters and exits the collider sphere. Trouble is the tutorial never really explains exactly what a raycast is and how they work. Are they linear beams or do they fill the entire sphere?

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
0

Answer by fafase · Apr 11, 2014 at 07:36 PM

Your problem is that the raycast is only used when the player enters the trigger, then you would need the player to get out and back in to call a new raycast.

You should either set a boolean and use it in the update or check for distance instead of using collision.

See this as well,that may help you http://unitygems.com/basic-ai-character/#Sight

Comment
Add comment · Show 5 · 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 robertbu · Apr 11, 2014 at 08:58 PM 0
Share

Alternately you could use OnTriggerStay() ins$$anonymous$$d of OnTriggerEnter() if @falfase's diagnosis of the problem is correct. But his suggestion of just using distance (Vector3.Distance()) is a better solution to the problem than a trigger collider.

avatar image SoylentRobot · Apr 13, 2014 at 06:30 PM 0
Share

thanks, i'll look into these. if it is using OnTriggerStay, wont that mean it will send out a raycast if there is any object in the trigger sphere? wont that strain the program if there is a lot of raycasts going on from multiple enemies?

avatar image fafase · Apr 14, 2014 at 04:32 AM 0
Share

Raycast is just about arithmetic so it won't affect much. Of course if you call it 1000 times per frame then you may lose a few fps. The bonus of using a method ins$$anonymous$$d of an event is that you ca control the frequency using InvokeRepeating. So ins$$anonymous$$d of calling every frame you can define it to be x times per second only.

avatar image SoylentRobot · Apr 16, 2014 at 10:52 PM 0
Share

that article you've linked has gone completely over my head haha. Changing the code to OnTriggerStay() doesn't seem to have made any different either. is the stuff in Physics.Raycast() maybe the issue? do I have to specify an angle for the raycast to be cast in, or is the code already doing that? can i have the raycast cover the entire sphere collider?

avatar image SoylentRobot · Apr 20, 2014 at 10:01 PM 0
Share

I've figured out what was wrong, the transform.up was causing the trouble. removing that from the physics.raycast fixed it. now my enemies can see the player. Thanks for the help!

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

21 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

Related Questions

Raycast target sight? 0 Answers

Raycast to OVRplayercontroller,Raycast to OVRplayercontroller - bugged 1 Answer

How to stop enemies from shooting each other 1 Answer

Raycast stealth enemy? 1 Answer

Can someone help me to understand why this raycast is not working properly? 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