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 ultrasilence · Feb 02, 2013 at 07:41 PM · c#distance

Easiest way to find a length I need?

I've got 2 objects that I need to connect with a prefab cylinder that acts as a wire. What I thought what would work makes it too long though :/

Thought I could just apply a^2 + b^2 = length I need squared, but it still comes out a bit too long.

wireTest = Instantiate (wire1, wirePos, Quaternion.identity) as GameObject; wireTest.transform.Rotate(new Vector3(0,0,270)); wireTest.transform.localScale += new Vector3(0, GetLength (getSide1, getSide2), 0);

And the method:

float GetLength(float side1, float side2) { return Mathf.Sqrt (side1 + side2); }

side1 and side2 are simply (x2 - x1)^2 and (z2 - z1)^2. Where have I gone awry here, and is there an easier way?

EDIT:

void Start ()

{ // Putting the two children into variables. line1a = GameObject.Find ("Poles/pole1/line1"); line1b = GameObject.Find ("Poles/pole2/line1");
// Height of all wires. wireHeight = line1a.transform.position.y;
// returns magnitude of two vector3's. length = GetLength (line1a.transform.position, line1b.transform.position);
// Used to calculate where the center is between two poles. float dist1 = (line1a.transform.position.x + line1b.transform.position.x) / 2; float depth1 = (line1a.transform.position.z + line1b.transform.position.z) /2;
// Vector3 to be used in placing wire in center between two poles. Vector3 wirePos = new Vector3(dist1, wireHeight, depth1);
// Instantiating wire, rotating, and attempting to get it correct length, but not angle yet. wireTest = Instantiate (wire1, wirePos, Quaternion.identity) as GameObject; wireTest.transform.Rotate(new Vector3(0,0,270)); wireTest.transform.localScale += new Vector3(0,length , 0); }

// Returns magnitude between two vector3's.

float GetLength(Vector3 obj1, Vector3 obj2) { return new Vector3(Mathf.Abs (obj2.x - obj1.x),0, Mathf.Abs (obj2.z - obj1.z)).magnitude; }
Comment
Add comment · Show 1
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 ultrasilence · Feb 02, 2013 at 06:56 PM 0
Share

Sorry about that formatting, it looked correct when I posted it :/

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Feb 02, 2013 at 07:47 PM

Assuming the wire is one unit long to start and you have two the points p1, p2 as Vector3's, the scale will be (p2 - p1).magnitude.

Comment
Add comment · Show 3 · 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 ultrasilence · Feb 02, 2013 at 09:00 PM 0
Share

I have em as just the distance between x coordinates and z coordinates. But I'll just get the vector distance and try magnitude real quick and see if it works out!

avatar image robertbu · Feb 02, 2013 at 09:16 PM 0
Share

I'm not sure how your code is setup, but the transform.position of these children will be in world space.

avatar image ultrasilence · Feb 02, 2013 at 10:26 PM 0
Share

Edited in more code so hopefully my ignorance can be cured.

avatar image
0

Answer by Wolfram · Feb 02, 2013 at 10:34 PM

Your length is correct, even the first version. But you are adding that length onto localScale, instead of multiplying/setting it.

So if you start from a unit scale (1,1,1), adding (0,length,0) results in a scale of (1,1+length,1). Use this instead:

 wireTest.transform.localScale *= new Vector3(1,length , 1);
Comment
Add comment · Show 10 · 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 ultrasilence · Feb 02, 2013 at 10:36 PM 0
Share

$$anonymous$$ono screams at me that *= isn't valid between two Vector3's.

avatar image Wolfram · Feb 02, 2013 at 10:38 PM 0
Share

$$anonymous$$ono is correct ^^

Well, since you just instantiateed that object, the scale should be (1,1,1) anyway, so just override it:

 wireTest.transform.localScale = new Vector3(1,length , 1);
avatar image ultrasilence · Feb 02, 2013 at 10:42 PM 0
Share

Ugh, dunno whats going on. Even with that it's way too long. Just from some simple trial and error, the length is just under 20 units too long.

avatar image Wolfram · Feb 02, 2013 at 10:44 PM 0
Share

Well, this approach only works if your "wire1" prefab has a y-dimension of 1. Is that the case?

EDIT: ...and it has no localScale (shoudl be (1,1,1))

avatar image ultrasilence · Feb 02, 2013 at 10:45 PM 0
Share

It is 1, yes. And if it matters, the scale for x and z of the cylinder I'm using as a wire are .2 and .2.

Show more comments

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

Classic Game Distance masking darkness/fog help 1 Answer

How to make player stop a certain distance away from enemy? 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