Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Pakislav · Jul 21, 2017 at 10:25 AM · rotationvector3quaterniondirectionrts

Get rotation from a vector direction.

EDIT: The solution to title question is this:

Quaternion.LookRotation(Vector3 desiredDirectionOfRotation, Vector3 desiredDirectionOfUpUsuallyVector3.up);

Original (more off-topic) post: I'm making an RTS. When I have multiple units selected and click and drag I want them to form into a row between the locations of the mouse on buttondown and buttonup and then rotate 90 degrees left from that line.

What I have a problem with is that last part.

What I tried: Get a cross of the two points and pass it to the unit function;

 Vector3 A = rightClickStartHit.point;
 Vector3 B = rightClickEndHit.point;
 //Vector3 AB = rightClickEndHit.point - rightClickStartHit.point;
 Vector3 turn = Vector3.Cross(Vector3.up, A - B);
 
 foreach (GameObject go in listOfSelected)
                 {
                     Vector3 C = Vector3.Lerp(A, B, j);
                     go.GetComponent<Unit>().Move(C, turn);
                     j += i;
                 }

Then use Quaternion.Slerp to rotate towards the euler of turn:

 public void Move(Vector3 targetPos, Vector3 targetRot)
 {
     isTurning = true;
     //targetRot = targetRot.normalized;
     lookRot = Quaternion.Euler(targetRot); //this gives 0?
     lookRotV3 = targetRot;
     Debug.Log("lookRot = " + lookRot);
     agent.destination = targetPos;
 }

     // in Update
     if (isTurning && agent.remainingDistance <= agent.stoppingDistance)
     {
         Debug.Log("current = " + transform.rotation + " desired = " + lookRot);

         //lookRot = Quaternion.FromToRotation(transform.forward, lookRotV3);

         transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, 1 * Time.deltaTime);
         //transform.rotation.eulerAngles 
         //transform.Rotate(lookRot * 1 * Time.deltaTime);
         if (Quaternion.Angle(lookRot, transform.rotation) < 16)  
         //this only stops if set to < 20 but that's ugly
         {
             Debug.Log("Stopped turning");
             isTurning = false;
         }
     }

As you can see I tried a bunch of things but I don't understand quaternions and vectors at all. The resulting target rotation always ends up being a 0.0.0

I'll add the object is in a plane and I only want them to rotate around the y axis.

Also additional question: How to rotate an object over time, but consistently, not like Slerp?

PS: All help highly appreciated, sorry for formatting.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jul 21, 2017 at 11:23 AM

The success of this line:

 Vector3 turn = Vector3.Cross(Vector3.up, A - B);

highly depends on which plane you use for your RTS game. This assumes that A and B are both inside the X - Z - plane. If you use the X - Y - plane (the usual 2d GUI plane) the cross product will result either in "Vector3.forward" or "-Vector3.forward"

In addition you should make sure that "AB" is actually a non zero vector. Or in other words: make sure your two hit points are not the same.

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 Pakislav · Jul 21, 2017 at 11:48 AM 0
Share

Thanks for your answer!

A and B are raycast on terrain in the X and Z plane. What I want to do is get an A -> B vector, rotate it 90 degrees left and use that to rotate the units - like in Total War games. But I'm failing to understand this.

Will Vector3.Cross(Vector3.up, A - B); get that A -> B vector turned by 90 degrees left?

This code:

                 Debug.Log("A = " + A + " B = " + B);
                 Vector3 AB = B - A;
                 Vector3 T = Quaternion.Euler(0, 90, 0) * AB;
                 Debug.Log("T = " + T + " AB = " + AB);
                 Vector3 turn = Vector3.Cross(Vector3.up, A - B);
                 Debug.Log("Turn = " + turn);

Prints these values: A = (-18.3, 0.0, 16.2) B = (3.2, 0.0, 18.7) T = (2.6, 0.0, -21,5) AB = (21.5, 0.0, 2.6) Turn = (-2.6, 0.0, 21.5)

So which one will be my +90 degrees? T or Turn? Thanks again for your help!

avatar image Bunny83 Pakislav · Jul 21, 2017 at 12:11 PM 1
Share

Well your two examples are very confusing since you use the vector AB for the quaternion rotation but the vector BA for the cross product. First have a look at your quaternion rotation:

You take the direction AB and rotate it 90° to the right since the quaternion rotation around Y is clockwise when you look at it from the top. It's counter clockwise when you look along the Y axis (from the bottom up). So T will be a vector that is 90° to the right.

Your cross product on the other hand will also rotate the vector 90° to the right but since you use the opposite vector (BA) you actually rotate the vector 90° to the left. That's why "T" is the inverse of "Turn":

 T = -Turn

$$anonymous$$eep in $$anonymous$$d that Unity uses a left-hand system so the cross product uses the left-.hand rule ins$$anonymous$$d of the right hand rule.

edit

To be more precise:

 Vector3 AB = B - A;
 Vector3 T = Quaternion.Euler(0, 90, 0) * AB;
 Vector3 turn = Vector3.Cross(Vector3.up, AB);

In those two cases both "T" and "turn" should contain the same direction (roughly, keep in $$anonymous$$d floating point numbers!).

ps: In a 2d environment the cross product can be extremely simplified since you use (0,1,0) as first vector. That basically leaves you with:

 Turn90Right = new Vector3( AB.z, 0, -AB.x);
 Turn90Left  = new Vector3(-AB.z, 0,  AB.x);
avatar image Pakislav Bunny83 · Jul 21, 2017 at 12:35 PM 0
Share

Yes, I inverted AB because I realized it was originally the opposite of what I intended to use it for, which was the correct result of the Cross after all.

Thanks a lot for taking your time to explain this to me. Can you push me in the right direction to figure out how to turn the direction of the calculated vector into a rotation I can set as target for Quaternion.SLerp or equivalent method of rotating over-time? And maybe suggest what would be the best method of rotating that avoids the "never quite reaches 1" problem of SLerp?

Again thank you so much for your time!

EDIT: I believe Quaternion.LookRotation is what I'm looking for. Now to find something better than SLerp!

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

97 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

Related Questions

Rotate vector3 relative to another vector3 1 Answer

Rotate object based on hit normal + matching camera direction 0 Answers

3D nested Turret Prefab Rotation 1 Answer

Rotating a direction Vector3 by Quaternion 2 Answers

Quaternion amount/value of rotation? 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