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 Vice_Versa · Jul 13, 2017 at 04:09 PM · raycastraycastingwallsight

how to use raycasting for enemy sight (C#)?

The issue im having is that my enemy can see through walls. Ive been trying to do this for hours and cant quite get it working. Ive seen several other posts about raycasting, but alot of them are outdated or using javascript. Ive tried just regular raycasting to see if it can hit the player, but it results with the enemy seeing him through the walls. I also tried making an array of all the objects that are hit and then checking to see if the player is closer than the others, but this seemed to make the enemy glitch out to See the player and not see the player every frame. Does anybody know how to do this?

as of right now this is what i have

 RaycastHit[] hits;
             hits = Physics.RaycastAll (transform.position + Vector3.up * 1.5f, transform.forward, sightDistance);
         for (int i = 0; i < hits.Length; i++) {
             if (hits [i].collider.gameObject.Equals (player)) {
                 Debug.Log ("I C YOU");
                 animator.SetBool ("isAlert", true);
                 playerDistance = Vector3.Distance (transform.position, player.transform.position);
             } else {
                 otherDistance = Vector3.Distance (transform.position, hits [i].transform.position);
             }
         }
         if (otherDistance < playerDistance)
         {
             animator.SetBool ("isAlert", false);
             Debug.Log ("NOPE");
         }
     }
     }

This is the closest i can get to making it work. the enemy sees the player and starts shooting, then immediately stops seeing the player even when hes right in front of him. i need him to see the player when hes in the line of sight, and need him to stop seeing th eplayer if he moves behind a wall

Comment
Add comment · Show 1
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 Jwizard93 · Jul 13, 2017 at 04:23 PM 0
Share

Can we see your code? Raycast should stop at the walls provided the walls have a collider. Raycast all will go straight through the walls and there is a chance the returned array of colliders hit will not be in the order that they were actually hit.

3 Replies

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

Answer by Jwizard93 · Jul 13, 2017 at 05:13 PM

Use Raycast instead of Raycast all. Raycast all goes through everything. and returns everything that is hit. You are iterating through everything that was hit to see if the player is in there and he always will be provided he is within sightDistance. .Make sure obstacles also have colliders.

Comment
Add comment · Show 2 · 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 Vice_Versa · Jul 13, 2017 at 05:36 PM 0
Share

I tried regular raycasts and he was having difficulty even seeing the player. I need him to be able to detect walls in case the player walked behind one. The problem im having with this script is that the enemy thinks that the walls are closer than the player even though they are behind him

avatar image Jwizard93 · Jul 13, 2017 at 05:52 PM 1
Share

In you original attempt is it possible that there are walls both between the player and the enemy and walls behind the player but still within sight distance? In that event otherDistance will be set to the distance of the furthest wall hit and the player will still be targeted.

When you tried using just Raycast ins$$anonymous$$d of RaycastAll were you hitting the enemy itself? It may be best to start the ray at a position a little bit in front of the enemy and outside his own collider.

avatar image
0

Answer by pyroantimonic_entity · May 14, 2018 at 11:33 PM

https://www.youtube.com/watch?v=xppompv1DBg&t=147s Watch the whole video

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 Ex6tra · Jun 10, 2020 at 01:37 PM

I think your problem comes from RaycastAll returning arrays that are not in order. you should use something like System.Array.Sort(hits, (x, y) => x.distance.CompareTo(y.distance)); to order your RaycastAll array and then check if the first Hit is on the player.

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

100 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

Related Questions

Enemy line of sight 1 Answer

Raycast target sight? 0 Answers

Raycast to OVRplayercontroller,Raycast to OVRplayercontroller - bugged 1 Answer

Can RaycastHit2D.point be inside a BoxCollider2D if raycast was performed from outside that collider? 1 Answer

What's an easy way to align a UI element with a Raycast? 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