• 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
Question by Ludwik1526 · Apr 09, 2020 at 11:35 AM · rotationquaternionrotate objectquaternionsquaternion.lookrotation

Rotating an object in relation to its endpoints

Hey,

In the app I am making the user places objects on the ground and a line is supposed to appear between them, so that they are connected. I managed to instantiate and scale it correctly but when it comes to rotation... after a few hours I still cannot figure out how it should be. The endpoints' coordinates can be different in all axes - they are not in the same plane. Here's my funtion:

 void PlaceLine(Vector3 startPos, Vector3 endPos)
 {
     // calculating the center point between two endpoints
     Vector3 centerPos = new Vector3(startPos.x + endPos.x, startPos.y + endPos.y, startPos.z + endPos.z) / 2f;

     // instantiating the line in the center point with an appropriate rotation
     GameObject line = Instantiate(linePrefab, centerPos, Quaternion.FromToRotation(endPos, startPos));

     // calculating the scale of the line
     float scaleX = Mathf.Abs(startPos.x - endPos.x);
     
     // setting the line's length
     line.transform.GetChild(0).localScale = new Vector3(scaleX, 0.001f, 0.03f);
 }

I've also tried using Quaternion.LookRotation and some other ways but none of them worked for me either. I don't like quaternions and they don't like me, I guess...

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Apr 09, 2020 at 11:54 AM

if you use a Quad or cube as a line there's a really simple trick. First of all parent your unit quad / cube to an empty gameobject and offset the child by half its size along the z axis so that the origin of the parent sits right at the end of the quad / cube and the object extends along the positive z axis. Make this construct a prefab.


To align such a prefab between two points all you have to do is this:

 public Transform linePrefab;
 
 Transform PlaceLine(Vector3 startPos, Vector3 endPos)
 {
     Transform line = Instantiate(linePrefab);
     line position = startPos;
     line.LookAt(endPos);
     line.localScale = Vector3.forward * Vector3.Distance(startPos, endPos);
     return line;
 }

Note that you can adjust the child objects local scale for x and y to adjust the thickness. However the length in z should stay at 1 since the parent scale will directly determine the length of the line. Also note that the LookAt method has an optional up vector as second parameter. It can be used to control the rotation around your line in 3d space. If you use this for 2d you probably want pass -Vector3.forward as up vector so the top side of your object will face the camera.

Comment

People who like this

0 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 haolly · Apr 10, 2020 at 06:28 PM 0
Share

Maybe Transform.RotateAround ? Transform.RotateAround(startPoint, cross(start, end), vector.angle(start, end)) ?

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

166 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 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 can I instantiate a gameobject facing another gameobject 2D? 0 Answers

Quaternion reset rotations didnt smooth 0 Answers

Choose starting point/angle for rotation with cos and sin 0 Answers

quaternion - rotate camera based on original facing at scene start? 0 Answers

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers


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