• 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 Feref2 · Oct 10, 2018 at 06:40 PM · pointsdistance check

[Still unsolved] Is there some way to cast lines and get the point where they intersect?

So I need the distance between two points.

alt text

However, this is harder when the objects are rotated, since the point I need to get is no longer the x and y coordinates of the others (1, 6).

alt text

Hope someone answers. Writing in paint was really hard...

captura-de-pantalla-7.png (170.7 kB)
captura-de-pantalla-9.png (164.3 kB)
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
0

Answer by IvovdMarel · Oct 10, 2018 at 11:59 PM

I recommend using transform.InverseTransformPoint to calculate the local position of the collision. You should only have to look at the x or z value then (as the origin would be 0,0,0)

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 Feref2 · Oct 11, 2018 at 02:25 AM 0
Share

Sorry, but how? I tried this and the collisions weren´t were they were supposed to:

 void OnCollisionStay2D (Collision2D coll)
 {
     for ( int l = 0; l < coll.contacts.Length; l++)
     {
         if (this.transform.position.x > coll.contacts[l].point.x)
         {
             leftPos = this.transform.InverseTransformPoint (coll.contacts[l].point);       
         }
     }
  
     for (int r = 0; r < coll.contacts.Length; r++)
     {
         if (this.transform.position.x < coll.contacts[r].point.x)
         {   
             rightPos = this.transform.InverseTransformPoint(coll.contacts[r].point);
         }
     } 
 
     diff = $$anonymous$$athf.Abs(rightPos.x - leftPos.x);
 
 }
avatar image IvovdMarel Feref2 · Oct 11, 2018 at 02:39 AM 0
Share
 void OnCollisionStay2D (Collision2D coll)
  {
      
      for ( int l = 0; l < coll.contacts.Length; l++)
      {
          Vector3 globalPos = coll.contacts[l].point;
          Vector3 localPos = this.transform.InverseTransformPoint(globalPos);
          if (localPos.x < leftPos.x)
          {
              leftPos = localPos;       
          } else if (localPos.x > rightPos.x)
          {    
             rightPos = localPos;
          }
      }
 
      diff = $$anonymous$$athf.Abs(rightPos.x - leftPos.x);
  
  }

avatar image IvovdMarel IvovdMarel · Oct 11, 2018 at 02:40 AM 0
Share

This should get the most left position and the most right position and calculate the difference. I didn't test it though so there might be some problems.

Show more comments
avatar image
0

Answer by Bunny83 · Oct 11, 2018 at 08:30 AM

The distance between two "points" of course requires you to use all coordinates. Just looking at the x value (no matter if it's local or world space) of course doesn't work in every case.


From your code it's not clear what you actually need. There could be more than 2 collision points and it's not clear which ones you actually want. If you want to find the two points which are the farthest apart it becomes a bit more complicated. You would need to check each collision point against every other to find the greatest distance.


Basically something like that:

 int count = coll.contacts.Length;
 Vector3 point0;
 Vector3 point1;
 float distSqr = 0;
 for ( int i = 0; i < count - 1; i++)
 {
     Vector3 p0 = coll.contacts[i].point;
     for (int n = i+1; n < count; n++)
     {
         Vector3 p1 = coll.contacts[i].point;
         float d = (p0-p1).sqrMagnitude;
         if (d > distSqr)
         {
             point0 = p0;
             point1 = p1;
             distSqr = d;
         }
     }
 }
 float distance = Mathf.Sqrt(distSqr);

Here "point0" and "point1" will contain the two points which are farthest apart from each other and "distance" will contain the distance between those two points. You get the same by using:

float distance = Vector3.Distance(point0, point1);
at the end

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 Feref2 · Oct 11, 2018 at 05:46 PM 0
Share

Well, thanks, but I actually need to do this: https://gamedev.stackexchange.com/questions/113149/how-can-i-squeeze-a-game-object-when-it-is-pressed-between-two-others/113290 I made some changes to that code, but as you can see, that doesn´t work if the obects are rotated. Getting the distance between the furthest points doesn´t work either. If the squeezers are not perfectly aligned, the distance between those collisions will be bigger than the actual object´s width. That´s why I wanted only the x distance. When the objects aren´t rotated is just x - x and ignoring y and z axis.

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

92 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

Related Questions

How do I create an effective spawn point script for multiple scenes and change music each scene? 1 Answer

Points when an enemy dies. 2 Answers

Move object around another custom-made object 1 Answer

Find two points which are on 120 degrees from a third point. 3 Answers

domination capture points 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