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 Vel_1828 · Jun 29, 2014 at 12:17 AM · raycaststealthline of sight

Line Of Sight - should work, but doesn't

I'm working on simpliest line of sight mechanic for enemies. It consists of few elements: - In front of the enemy there is a Trigger Sphere. If player enters it, the trigger sends message to Enemy, that player is in range of sight. - Then, Enemy has a simple Raycast script to check, if there is something between him and Player. If there isn't, he starts chasing the player.

Simple as that. Everything works great through the Sphere Trigger, but Raycast doesn't. It just does nothing. No detection of player at all.

The Script for raycast detection:

 function CheckLineOfSight(){
 
     var hit : RaycastHit;
     var rayDirection = Player.transform.position - transform.position;
       Debug.Log("2");
          if (Physics.Raycast (transform.position, rayDirection, hit)) {
                 if (hit.transform.tag == "Player") {
          Debug.Log("3");
                     GETHIM();
                 }
         }
         
 }



Debug Log "2" works and shows up when Player enters Trigger Sphere. Debug Log "3" doesn't show at all. No errors too.

Researching google gave me no working answers.

Am I missing something?

Comment
Add comment · Show 3
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 IvovdMarel · Jun 29, 2014 at 12:35 AM 1
Share

Use linecast - http://docs.unity3d.com/ScriptReference/Physics.Linecast.html to see where your cast is going. I think rayDirection is not created right.

avatar image Kiwasi · Jun 29, 2014 at 12:46 AM 0
Share

Sometimes issues occur if your ray cast starts inside a collider.

avatar image EvilTak · Jun 29, 2014 at 04:32 AM 0
Share

Try and remove that if statement after raycast and just log something when ray cast is successful.

 if (Physics.Raycast (transform.position, rayDirection, hit)) {
         Debug.Log("something");
         }

This way you will be able to see whether your ray cast was successful or not. If it was, then there is some problem in your tag checking statement. I think it should be gameObject.tag ins$$anonymous$$d of transform.tag.

1 Reply

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

Answer by Vel_1828 · Jun 29, 2014 at 09:54 AM

AAAAH, works now! "Sometimes issues occur if your ray cast starts inside a collider." THAT was the issue. The Rayast was hitting the Trigger Sphere. I just set the trigger to layer "Ignore Raycast" and now it works perfectly. Thanks a lot to everyone! The Linecast will be most useful in the future too. :)

Comment
Add comment · Show 1 · 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 Kiwasi · Jun 29, 2014 at 09:56 AM 0
Share

There is also an option buried somewhere (use google to find it) that makes the raycast automatically ignore all triggers.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Ai Field of View 0 Answers

List of objects in line of sight - most optimized way to write it? 0 Answers

raycast hit not detecting && Nav mesh agent ai stealth 1 Answer

RayCast2D not returning values as expected 0 Answers

What is the best way to do stealth in Unity? 4 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