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 Achando · Jul 21, 2020 at 11:21 PM · rotationvector3unityeditorquaternionrotatearound

How do I rotate a Vector3 based on two other Vector3?

alt text See the picture for a better understanding.
(BTW the example is in 2D but i need this in 3D)

Picture Subtitle:

Red and yellow dot: origins of the vector3
Blue Vector3: Imagine those as a forward vector3 of an object
Green Vector3: A vector3 i obtained from rotating the red dot blue transform
Orange Vector3: the vector3 that i want to obtain, it has the same rotation as the green one but relative to its origin
Purple rotation: Same rotation on both sides


I've already tried messing with quaternions but I cant figure how to make this work, when using the quaternions it looked like they were rotating based on the wrong origin...

chrome-0r4qtarwni.png (31.5 kB)
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

2 Replies

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

Answer by jamesvhyde · Jul 22, 2020 at 04:39 PM

I think you are trying to obtain the orange vector, and you don't care about the purple rotation itself? In that case, there is no need to mess with quaternions. You can use the local coordinate systems.

I also think that maybe it's not working how you want because the ScreenPointToRay method gives you a direction and an origin, and the two have to be used in conjunction. It will be a little off if you just rotate the direction vector, because the origin of the ray is on the near clipping plane, not at the camera position as might be expected.

Here is some code in two behaviors that I think is doing what you want:

 public class CameraMouseRay : MonoBehaviour
 {
     public Vector3 localDirection { get; private set; }
     public Vector3 localOrigin { get; private set; }
 
     void Update()
     {
         Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
         localDirection = transform.InverseTransformVector(ray.direction);
         localOrigin = transform.InverseTransformPoint(ray.origin);
     }
 }

 public class CameraLocalRay : MonoBehaviour
 {
     public CameraMouseRay mouseCamera;
 
     void Update()
     {
         Vector3 localDir = mouseCamera.localDirection;
         Vector3 worldDir = transform.TransformVector(localDir);
         Vector3 localOrg = mouseCamera.localOrigin;
         Vector3 worldOrg = transform.TransformPoint(localOrg);
         Debug.DrawRay(worldOrg, worldDir * 100, Color.yellow);
     }
 }

The CameraMouseRay component goes on the camera that the player is looking through, and the CameraLocalRay component goes on the second camera.

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 Achando · Jul 22, 2020 at 09:48 PM 0
Share

I'm so thankful, this worked 100%!

avatar image
1

Answer by Namey5 · Jul 22, 2020 at 09:54 AM

Assuming;

  • Va = Red-Blue

  • Vb = Red-Green

  • Vc = Yellow-Blue

  • Vd = Yellow-Orange

You can think of a quaternion as a rotation - as such, you can just find and store the rotation from Va to Vb and apply the same rotation to Vc;

 Quaternion rot = Quaternion.FromToRotation (Va, Vb);
 Vector3 Vd = rot * Vc;
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 Achando · Jul 22, 2020 at 01:52 PM 0
Share

Thanks for the answer, but it isn't working correctly (at least as I intended to xD)
The code im using (simplified):

 var _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
 var _rot = Quaternion.FromToRotation(_forward, _ray.direction);
 _final = _rot * _forward2;

I think the problem is that its rotation the _foward2 based on the _foward vector origin.
Basically think this way:
I have two cameras, one's the main. (_forward is from the main one and _forward2 is from the other)
I want to make a raycast based on mouse position and then make another raycast from the second camera like it has a player that had the mouse in the same position (sorry if this sounds confusing)

avatar image Namey5 Achando · Jul 22, 2020 at 09:47 PM 0
Share

@jamesvhyde 's answer works just as well and brings up a good point - ScreenPointToRay is cast from the camera's near plane. However, all you need to do is combine the ray's components and compare them with the camera's to get a more useable vector;

 Vector3 rayDir = ((ray.origin + ray.direction) - cam.transform.position).normalized;

From there you should be able to use that in the quaternion example as well. Both work - the space transformations may be a bit easier to follow, but the quaternions are simpler to write and faster performance-wise. It's really up to you, I would just be careful using Camera.main in a core Update loop (cache it on first use, etc).

avatar image Achando Namey5 · Jul 22, 2020 at 10:32 PM 0
Share

Thanks for the advice!

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

200 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 an object so its up vector matches a sphere 1 Answer

Get rotation from a vector direction. 1 Answer

3D nested Turret Prefab Rotation 1 Answer

Sync Rotations of objects with differing position and rotation 1 Answer

c# modify only one axis of a quaternion 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