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 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 anything, and reacts accordingly.

The issue is however that sometimes an origin point ends up within an obstacle, after which the raycast won't detect anything. (Because of the same reason when you put a camera in an object and can still see out of the object). I tried fixing this 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 which colliders hit, but don't know how to do that, and whether the empty GameObject will even detect anything, 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 within 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 within a certain distance of your collider:

 public float range = 0.5f;
 
 private void Update () {
       // checks if the object is within 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 this 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

But wouldn't that only work from the perspective of the obstacles, not the enemy itself? That would mean that I'd have to add a script checking for origin points to every single obstacle, which doesn't seem like a very effective way to solve it. (I now notice that I miswrote what I was trying to say, I meant that sometimes the origin point would end up inside the obstacle, though that would usually end with the enemy ending up inside the obstacle.)

avatar image Igor_Vasiak LethalSalad · Nov 24, 2018 at 12:53 PM 0
Share

That solution would probably end up working for both. The one script per object solution is the best, I$$anonymous$$O. You could just fire the check once the point/enemy/obstacle interacts with each other.


Also, you should probably test between Vector3.Distance and AbsoluteDifference to know which performs the best here.


And, about you not having Trigger Colliders in your points, why is that? You could just add one, put that object in a different layer, and change your Raycasts so that they ignore that specific layer. And edit the Layer $$anonymous$$atrix so that unwanted objects don't mess with that layer.


Also, about adding an OnTriggerEnter statement to your ifs, just make a bool that turns on on OnTriggerEnter and turns off on OnTriggerExit.


Hopefully, this will somewhat help you! xD

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.

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

Can't find a clone object with RayCast? [Solved] 2 Answers

Help finding out which side of a cube a raycast hits 1 Answer

Instantiate an object at hit position without it intersecting anything 1 Answer

RayCasting Mouse 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