Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by nathaxx · Jul 28, 2021 at 11:57 AM · movementvrquaterniontransformation

How to fly in Controller's forward direction?

Hello all,

with the Unity XR Interaction Toolkit, I am trying to realise a movement type that enables the player to fly around space in whatever direction the left controller points. For this, I have modified the ContinuousMoveProvider's function ComputeDesiredMove like this:

     protected override Vector3 ComputeDesiredMove(Vector2 input)
     {
         if (input == Vector2.zero)
             return Vector3.zero;
 
         XRRig xrRig = system.xrRig;
         if (xrRig == null)
             return Vector3.zero;
 
         // Assumes that the input axes are in the range [-1, 1].
         // Clamps the magnitude of the input direction to prevent faster speed when moving diagonally,
         // while still allowing for analog input to move slower (which would be lost if simply normalizing).
         Vector3 inputMove = Vector3.ClampMagnitude(new Vector3(enableStrafe ? input.x : 0f, 0f, input.y), 1f);
 
         Transform rigTransform = xrRig.rig.transform;
         Vector3 rigUp = rigTransform.up;
 
         // Determine frame of reference for what the input direction is relative to
         Transform forwardSourceTransform = forwardSource == null ? xrRig.cameraGameObject.transform : forwardSource;
         Vector3 inputForwardInWorldSpace = forwardSourceTransform.forward;
         Quaternion forwardRotation = Quaternion.LookRotation(inputForwardInWorldSpace, forwardSource.up);
 
         Vector3 translationInRigSpace = forwardRotation * inputMove * (moveSpeed * Time.deltaTime);
         Vector3 translationInWorldSpace = rigTransform.TransformDirection(translationInRigSpace);

This works fine, but only until the XRRig changes its forward direction (via SnapTurnProvider). I am pretty sure this affects the calculations, but I am not sure in what way. Can anyone help me with this? I think this is something quite stupid about quaternions or transformation, but I don't see the concrete problem right now.

Thanks in advance!

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
0

Answer by tlskillman · Jan 20 at 12:58 AM

Hey @Nathaxx, I don't have an answer to your question, but here is your code updated to work with the new XROrigin game object, which replaced XRRig.

< I had a problem with overriding the method, so modified the source. Yikes.>

I'm not using SnapTurn so your change works fine for me. Thanks,

         protected virtual Vector3 ComputeDesiredMove(Vector2 input)
         {
             if (input == Vector2.zero)
                 return Vector3.zero;
 
             var xrOrigin = system.xrOrigin;
             if (xrOrigin == null)
                 return Vector3.zero;
 
             // Assumes that the input axes are in the range [-1, 1].
             // Clamps the magnitude of the input direction to prevent faster speed when moving diagonally,
             // while still allowing for analog input to move slower (which would be lost if simply normalizing).
             var inputMove = Vector3.ClampMagnitude(new Vector3(m_EnableStrafe ? input.x : 0f, 0f, input.y), 1f);
 
             var originTransform = xrOrigin.Origin.transform;
             var originUp = originTransform.up;
 
             // Determine frame of reference for what the input direction is relative to
             var forwardSourceTransform = m_ForwardSource == null ? xrOrigin.Camera.transform : m_ForwardSource;
             var inputForwardInWorldSpace = forwardSourceTransform.forward;
             if (Mathf.Approximately(Mathf.Abs(Vector3.Dot(inputForwardInWorldSpace, originUp)), 1f))
             {
                 // When the input forward direction is parallel with the rig normal,
                 // it will probably feel better for the player to move along the same direction
                 // as if they tilted forward or up some rather than moving in the rig forward direction.
                 // It also will probably be a better experience to at least move in a direction
                 // rather than stopping if the head/controller is oriented such that it is perpendicular with the rig.
                 inputForwardInWorldSpace = -forwardSourceTransform.up;
             }
 
             //TLS Edits based on https://answers.unity.com/questions/1851515/how-to-fly-in-controllers-forward-direction.html
 
             var inputForwardProjectedInWorldSpace = forwardSourceTransform.forward; // Vector3.ProjectOnPlane(inputForwardInWorldSpace, originUp);
             //var forwardRotation = Quaternion.FromToRotation(originTransform.forward, inputForwardProjectedInWorldSpace);
             Quaternion forwardRotation = Quaternion.LookRotation(inputForwardInWorldSpace, forwardSource.up);
 
             var translationInRigSpace = forwardRotation * inputMove * (m_MoveSpeed * Time.deltaTime);
             var translationInWorldSpace = originTransform.TransformDirection(translationInRigSpace);
 
             return translationInWorldSpace;
         }
Comment
Add comment · 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

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

222 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

8-Axis 3D Top Down Movement 1 Answer

how do you sway the camera based on sharp turns 0 Answers

Steam Vr Hands lagg behind when i move 2 Answers

Gradual Rotation of Character from 2 Axis Input Sources 0 Answers

Player Control. Roller Ball with Cube Acceleration. 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