• 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 LethalSalad · Nov 21, 2018 at 08:40 PM · c#raycastraycastingdetectionempty game object

How to check if an empty GameObject is inside an object?

I'm working on a small project, where I'm creating a zombie-shooter-esque game as first project.

The enemies have two empty GameObject (origins) on either side of them, and every frame it performs a raycast out of both origins and checks if any see anyt$$anonymous$$ng, and reacts accordingly.

The issue is however that sometimes an origin point ends up wit$$anonymous$$n an obstacle, after w$$anonymous$$ch the raycast won't detect anyt$$anonymous$$ng. (Because of the same reason when you put a camera in an object and can still see out of the object). I tried fixing t$$anonymous$$s by making it check if the origin is inside of an obstacle, before realizing I had no idea how to do that. So does anyone have any advice? I thought about adding an "ontriggerenter"-esque statement into the if-statements that check w$$anonymous$$ch colliders $$anonymous$$t, but don't know how to do that, and whether the empty GameObject will even detect anyt$$anonymous$$ng, because of the fact that it's a collider-less GameObject

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by NinjaISV · Nov 22, 2018 at 06:19 PM

You could easily check if the object is already wit$$anonymous$$n the collider's bounds (https://docs.unity3d.com/ScriptReference/Collider-bounds.html)

 if (collider.bounds.Contains(object.transform.position) {
       // do stuff
 }

Or you could check if the object is wit$$anonymous$$n a certain distance of your collider:

 public float range = 0.5f;
 
 private void Update () {
       // checks if the object is wit$$anonymous$$n 0.5 unit of the collider object
       if (AbsoluteDifference(collider.transform.position, object.transform.position) <= range) {
            // do stuff
       }
 }
         
 private float AbsoluteDifference (Vector3 a, Vector3 b) {
      return (Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z));
 }

Hope t$$anonymous$$s helps!

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 LethalSalad · Nov 24, 2018 at 10:57 AM 0
Share
avatar image Igor_Vasiak LethalSalad · Nov 24, 2018 at 12:53 PM 0
Share
avatar image
0

Answer by Cornelis-de-Jager · Nov 21, 2018 at 09:06 PM

Why not just add a collider then?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

142 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 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

Raycast causes all enemies to attack 1 Answer

What's wrong with my RaycastHit2D? 1 Answer

Another Raycast Issue. 0 Answers

Raycast not detecting a hit from far away, it only detects when close. 0 Answers

Problem with raycasting, checking for ground doesn't work 3 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