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 gtaharaedmonds · Sep 12, 2016 at 04:41 AM · raycastcollider2dtriggersraycasthit2d

Issue with Raycasts in Unity 2D

 using UnityEngine;
 using System.Collections;
 
 public class EnemySight: MonoBehaviour
 {
     public GameObject player;
 
     public float fovAngle;
     public bool playerInSight;
 
     void OnTriggerStay2D(Collider2D other)
     {
         if (other.gameObject == player)
         {
             Vector2 playerDir = other.transform.position - transform.position;
             float angle = Vector3.Angle(playerDir, transform.right);
 
             if (angle < fovAngle / 2)
             {
                 RaycastHit2D hit = Physics2D.Raycast(transform.position, playerDir.normalized);
 
                 if (hit.collider.gameObject == player)
                 {
                     Debug.DrawRay(transform.position, playerDir.normalized, Color.green);
                     print("halp meh");
                 }
             }
         }
     }
 }

This is a script for an enemy view script for a 2d top down stealth game. The part that isn't working is the last bit involving the raycasts. Everything works up to the hit.collider.gameObject == player. Nothing prints and the debug ray is not drawn. If I change it to hit.collider != null it "works", but the raycasts hit the enemy itself. There are no actual errors, and the variables are all defined in-editor. Any help/comments are appreciated. Relatively new to Unity so it's probably a dumb mistake.

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 SorenaCoder · Sep 12, 2016 at 10:38 PM 0
Share

try to move 24th Line to somewhere above this condition:

 if (hit.collider.gameObject == player)

Then take a look and see where the ray is going and to what object is hitting.

sorry if I had mistakes in my sentences im not native English speaker

2 Replies

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

Answer by gtaharaedmonds · Sep 13, 2016 at 06:44 AM

The reason it wasn't working is the ray was hitting the enemy (where the script was attached to). All I had to do was add a layer mask that ignored the enemy.

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 Bigiansen · Sep 12, 2016 at 09:43 PM

Instead of comparing the actual gameobject, set the layer for the player and compare that instead.

If your player collider is in a nested gameobject, the comparison will be false.

 if(other.gameObject.layer == LayerMask.NameToLayer("Player"))
 {
 //...
 }

Also try debugging the ray before any comparison is made:

Instead of this:

 if (angle < fovAngle / 2)
  {
             RaycastHit2D hit = Physics2D.Raycast(transform.position, playerDir.normalized);
      
             if (hit.collider.gameObject == player)
              {
                 Debug.DrawRay(transform.position, playerDir.normalized, Color.green);
                 print("halp meh");
             }
  }

It should look like this:

 if (angle < fovAngle / 2)
                  {
                      RaycastHit2D hit = Physics2D.Raycast(transform.position, playerDir.normalized);
      Debug.DrawRay(transform.position, playerDir.normalized, Color.green);
                      if (hit.collider.gameObject == player)
                      {
                          
                          print("halp meh");
                      }
                  }








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 gtaharaedmonds · Sep 13, 2016 at 04:43 AM 0
Share

Turns out the reason it wasn't working is the ray was hitting the enemy (where the script was attached to). All I had to do was add a layer mask that ignored the enemy. Thanks for the replay though.

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

CircleCast hits emitter with Collider2D 0 Answers

Is my 2D Raycast set up correctly? because it does not seem to hit anything when i use a layer mask. 1 Answer

Raycasthit2D return wrong collider 1 Answer

2D Raycast effect only what it hits? 2 Answers

Get 2D Collider with 3D Ray 2 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