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 HakanOzcan · Jan 29, 2015 at 02:25 PM · gameobjectrigidbodytransformpassthrough

Detect a GameObject Passing Between Other Two GameObjects?

Guys is there a way to detect a gameObject is passing between(Not inside) other two gameobjects? Whenever it pass between other two objects, it sends me true. All of these three gameObjects are moving continuesly.Not static.

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 HakanOzcan · Jan 29, 2015 at 11:17 AM 0
Share

Right now, I am using line segment intersection algorithm. However, there are certain situations that line s. - line s. intersection is not doing well.

avatar image meat5000 ♦ · Jan 30, 2015 at 12:27 PM 0
Share

Ok, what have you tried? Code and ideas :)

avatar image HakanOzcan · Jan 30, 2015 at 03:15 PM 0
Share

I will prepare a benchmark which compares 3 solution. 1. Line segment intersection currently I am using with the help of this(http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/) 2. SweepTest 3.Raycast. I will inform you with results. $$anonymous$$eanwhile, all other ideas are welcome:)

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Jan 30, 2015 at 02:45 PM

Assuming you're talking about 2D space, you can calculate the sign of the determinant of vectors (AB,AC), where C is the object you're testing to have crossed between the line AB. Pseudocode:

 Side = sign( (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x) )


This will be 0 if C is on the line from A-B, +1 on one side, and -1 on the other side.

Comment
Add comment · Show 4 · 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 HakanOzcan · Jan 30, 2015 at 03:05 PM 0
Share

Remember that the line from B to A is not an infinite line. That is why C can be on +1 side of the line BA but still it may not be passed between them. However your solution provide me another view of perspective.Thanks.

avatar image tanoshimi · Jan 30, 2015 at 03:13 PM 0
Share

I meant for you to test when the value changes from -1 to 1, or vice-versa. That would imply C had crossed the line AB.

avatar image HakanOzcan · Jan 30, 2015 at 03:18 PM 0
Share

Yes I take it, but still couldnt understand what if C becomes from -1 to +1 from right or left side of both A and B?

avatar image tanoshimi · Feb 13, 2015 at 01:25 PM 0
Share

"what if C becomes from -1 to +1 from right or left side of both A and B?" I don't understand. If the sign of C changes, it's crossed the line.

avatar image
0

Answer by Derek-Wong · Jan 29, 2015 at 03:31 PM

Have you try to use collider on these objects? E.g., let say, you need to detect when A have passed through B and C.

  1. add some collider components on 3 objects, set trigger on to let them pass through each other.

  2. on A , insert a script, define some bools, say, isPassedB, isPassedC

  3. in OnTriggerEnter(), set isPassedB to true when trigger with B, vice versa

  4. set an if statement in Update() to do something when isPassedB and isPassedC both true.

Hope it helps. Tell me if you need the code.

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 HakanOzcan · Jan 30, 2015 at 09:52 AM 0
Share

Thanks Derek for your answer. I already tried such kind of solution. But it didnt work in most cases.

avatar image
0

Answer by meat5000 · Jan 29, 2015 at 03:37 PM

A bit clunky and expensive but you could SweepTest between the two other objects

http://docs.unity3d.com/ScriptReference/Rigidbody.SweepTest.html

There is also SweepTestAll

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 HakanOzcan · Jan 30, 2015 at 09:54 AM 0
Share

I dont know what SweepTest is. I will definetely search about it. Thanks.

avatar image meat5000 ♦ · Jan 30, 2015 at 12:26 PM 0
Share

The link is there :)

avatar image
0

Answer by Hellium · Jan 29, 2015 at 04:12 PM

How about a Raycast between your two objects ? You check the nature of the object hit by the ray : if it is your second object, return false, if another object passes, return true :)

Unity's documentation :

  • Raycast : http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

  • RaycastHit : http://docs.unity3d.com/ScriptReference/RaycastHit.html (test on the transform.name for example)

  • Raycasting tutorial : http://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting

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 HakanOzcan · Jan 30, 2015 at 09:56 AM 0
Share

Ja ja, but not sure doing a raycasting in every Update frame is a good idea:/ If I find a optimized version of this solution, it will be the easiest and painless solution. Thanks man.

avatar image Hellium · Feb 13, 2015 at 05:37 PM 0
Share

Another very simple solution is to check if the cross product between the two vectors AB and AC equals 0 or not. See this page about the cross product for explanation (middle of the page).

With Unity, the cross product between two vectors is simply computed with the Vector3.Cross function : See doc.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need insight on strange (simple) 2D object transform bug 0 Answers

Take final position after a force applied to a GameObject 1 Answer

Player movement in the direction of rotation INACCURATE? 3 Answers

Simple - Blocking movement 1 Answer

Rigidbody Disable Velocity/Movement? 1 Answer


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