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 CinnoMan · Feb 22, 2014 at 04:18 PM · sizedistance

Calculating distances: where's the error?

I have two movable objects. At the press of a button, I create the vector "distance" between them and get it's magnitude. Then I move a disabled box collider to the point halfway between those two objects (objectA.transform.position - distance.magnitude/2), rotate it appropriately and enable the collider. So far so good.

Now, when I set the collider's size (distance.magnitude/2, someValue), something weird happens: when the two objects are close together, the collider is too small, when the objects are far apart it's too large, reaching beyond the objects' origins. I expected the collider to extend always from one object's transform position to the other's.

 distance = new Vector3 (playerA.transform.position.x -playerB.transform.position.x, playerA.transform.position.y -playerB.transform.position.y, playerA.transform.position.z -playerB.transform.position.z);
 
 //setting the collider's position halwfway between A & B
 linkObject.transform.position = (playerA.transform.position - distance / 2);
 
 // rotating collider
 float angle = Vector3.Angle (distance, Vector3.right);
 float sign = (Vector3.Dot (distance, Vector3.down) > 0.0f) ? -1.0f : 1.0f;
 float finalAngle = sign * angle;
 linkObject.transform.eulerAngles = new Vector3 (0f, 0f, finalAngle);
 
 //this is the bit I don't understand why it doesn't work
 boxCollider2D.size = new Vector2 (distance.magnitude / 2, 0.5f);
 
 //scaling this doesn't work correctly either, btw
 linkObject.transform.localScale = new Vector3 (distance.magnitude , 0.1f, 0.1f);  


What am I missing?

Comment
Add comment · Show 4
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 KillMobil · Feb 22, 2014 at 04:33 PM 1
Share

to caclulate the distance why dont you use distance = Vector3.Distance (playerA.transform.position , playerB.transform.position);

then is it a 3d or a 2d Game? Cause you are adjusting a boxCollider2D!

avatar image whydoidoit · Feb 22, 2014 at 04:36 PM 0
Share

Are you both scaling and setting the size of the collider? that would be bad.

avatar image CinnoMan · Feb 24, 2014 at 02:43 PM 0
Share

thanks for your responses.

@$$anonymous$$ill$$anonymous$$obil: It's a 2D-Game. I'm not using Vector3.Distance because I need to find the vector I called "distance" between playerA and playerB anyway to calculate the rotation of the object I'm enabling between the two of them (thus making it visible). For some reason I can't remember right now I had to use vector3s ins$$anonymous$$d of vector2s as I had expected for a purely 2D game.

@whydoidoit Well, I'm scaling the object that has the collider component, in order to adjust its sprite size (which feels like a hack, but it was the best way I knew to "create" a visible link of variable size between two objects). Isn't the collider's size independent of the transform it's attached to, though? Is it relative? Will test.

avatar image whydoidoit · Mar 01, 2014 at 02:08 PM 0
Share

Well the scale and the size are related and multiplied - it sounds like just leaving the size and setting the scale is what you want.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CinnoMan · Mar 01, 2014 at 01:38 PM

bump

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

21 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

Related Questions

Difficulties in Changing the Scale of an Object 2 Answers

Scaling Planets based on Distance from Camera 1 Answer

Handling iPhone / iPad screen sizes? 0 Answers

how to reduce library size? 2 Answers

Change the size of a custom font in a Canvas text 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