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 Elharter · Feb 08, 2018 at 10:47 AM · scripting problem

Align a very easy cube mesh with collider to a Line!?

Hey Guys,

id like to align a cube to a line. For example: i draw a line with x0/y0 and x1/y1.... Now i wanted to align a cube to this line. It should have the same size, the same rotation and the same location.

How could i do this?

thanks

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 DarkDayZ · Feb 08, 2018 at 12:54 PM 0
Share

What do you mean by "drawing a line" exactly?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Elharter · Feb 08, 2018 at 01:07 PM

i got 1 line. with a start coordinaten and an end-coordinte (x0/y0 and x1/y1). And id like to align a cube (for example) to this line. With size, length, rotation....

I solved it partially....

 //set line ends to vectors
             Vector3 vecA = new Vector3(data.line[i].x, 40, data.line[i].y);
             Vector3 vecB = new Vector3(data.line[i].x1, 40, data.line[i].y1);
             //get our midway point
             Vector3 midPointAB = (vecA + vecB) / 2f;
             //get the direction between the two transforms -->
             Vector3 dirAB = (vecB - vecA).normalized;
             // get the rotation of the line
             //Vector3 relativePos = vecA - transform.position;
             Quaternion rotationAB = Quaternion.LookRotation(dirAB);
             // get the length of the line
             Vector3 abLength = vecB - vecA;
             line.transform.localPosition = midPointAB;
             line.transform.rotation = rotationAB;
             // set the new length (scaling size from cube prefab is 1/80/1/)
             line.transform.localScale = new Vector3(line.transform.localScale.x, line.transform.localScale.y, abLength.z);

But it doesnt work perfectly because the scaling is wrong, in relation to the direction. So i try since 10minutes with magnitude to solve it.

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 Elharter · Feb 08, 2018 at 02:08 PM 0
Share

Solved it !

 //set line ends to vectors
         Vector3 vecA = new Vector3(data.lines[i].x, 40, data.lines[i].y);
         Vector3 vecB = new Vector3(data.lines[i].x1, 40, data.lines[i].y1);
         //get our midway point
         Vector3 midPointAB = (vecA + vecB) / 2f;
         //get the direction between the two transforms, normalized
         Vector3 dirAB = (vecB - vecA).normalized;
         // get the length of the line
         Vector3 abLength = vecB - vecA;
         abLength = new Vector3($$anonymous$$athf.Abs(abLength.x), $$anonymous$$athf.Abs(abLength.y), $$anonymous$$athf.Abs(abLength.z));
         // set middle-point position
         cube.transform.localPosition = midPointAB;
         // get the rotation of the line and set it
         Quaternion rotationAB = Quaternion.LookRotation(dirAB);
         cube.transform.rotation = rotationAB;
         // set the new length (scaling size from cube prefab is 1/80/1/)
         cube.transform.localScale = Vector3.forward * abLength.magnitude;
         // higher the checkpoints to y=60
         cube.transform.localScale = new Vector3(cube.transform.localScale.x, cube.transform.localScale.y + 60f, cube.transform.localScale.z);
         // place it on terrain
         RaycastHit hit;
         if (Physics.Raycast(cube.transform.position, Vector3.down, out hit))
         {
             cube.transform.position = hit.point;
         }

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

133 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

Related Questions

Using GetPixels to break down an image, and only getting the last part. 1 Answer

Applying explosion force to destructible object AFTER it's been destroyed 2 Answers

Instantiate an object on mouse/cursor position. 1 Answer

How to Find gameObject child. 3 Answers

Why is my dropdown not able to control my game countdown time? 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