• 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 /
  • Help Room /
avatar image
0
Question by Everstorm1 · Apr 13 at 10:39 AM · quaternionaxisrotate objectlookrotationlocalspace

Rotate an Object around selected local axis towards another object?

Hi, I am currently trying to create something similar to a hinge joint, but without physics-interaction. This will then be used to create a lever for VR.

What I need for this, is to have an object rotate around a SINGLE axis - in the objects local space - that I choose in the direction of another Transform. The axis on the lever that later points in the direction of the other transform should also be selected.

Example: The lever-Object rotates around the LOCAL z-Axis so that the x-Axis points towards the other transform as far as posible on this axis.

Meanwhile the position and all other rotations should be maintained (in the example: position and x and y rotation). I tried combining tutorials, scripts and my last few braincells to create something that works but I just can not wrap my head around these Quaternions. This is what I have created the whole long day:

 public class LeverFramework : MonoBehaviour
 {
     [SerializeField] private Transform axis;
     [SerializeField] private float rotationSpeed;
 
     [SerializeField] private bool restrictX = false;
     [SerializeField] private bool restrictY = false;
     [SerializeField] private bool restrictZ = false;
 
     private InputDevice targetDevice;
 
     public float XDegree;
     public float YDegree;
     public float ZDegree;
     public Vector3 rotationAxis;
 
     public GameObject sphere;
 
     private void FixedUpdate()
     {
         //sphere.transform.position = currentDevicePos.position;
 
         if (interactorPresent == true)
         {
             
         }
 
         //sphere.transform.position = currentDevicePos.position;
 
         Vector3 lookDirection = (sphere.transform.position - axis.position).normalized;
 
         Quaternion rotation = Quaternion.LookRotation(lookDirection, rotationAxis) * Quaternion.Euler(XDegree, YDegree, ZDegree);
         Debug.DrawRay(axis.position, lookDirection);
 
         Quaternion completeRot = Quaternion.Slerp(axis.rotation, rotation, Time.deltaTime * rotationSpeed);
 
         float x = axis.localRotation.x;
         float y = axis.localRotation.y;
         float z = axis.localRotation.z;
 
         if (restrictX == false)
         {
             x = completeRot.eulerAngles.x;
         }
         if (restrictY == false)
         {
             y = completeRot.eulerAngles.y;
         }
         if (restrictZ == false)
         {
             z = completeRot.eulerAngles.z;
         }
 
         axis.rotation = Quaternion.Euler(x, y, z);
     }


it did turn towards my "sphere" object in some occasions but if I rotate the parent of my "Axis" Transform which holds the lever model that should rotate towards the sphere on the selected axis, my axis-object developes consciousness and doesnt do what I had planned for it.

I dont even know where to start with the problem. I worked on a solution a few hours ago that contained trying to work in local space, but I failed miserably.

Can someone tell me from the ground up how to rotate my lever on only one axis in Local Space towards another transform while maintaining all other transforms? It would mean the world to me and I could sleep for the first time in weeks again (˘o˘ ). I hope someone can help me. Have a great day!

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

0 Replies

· Add your reply
  • Sort: 

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

180 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

Related Questions

How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer

I have rotation problem, I look forward to helping. 0 Answers

Axis are not rotated as I would expect. 0 Answers

Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B 0 Answers

Changing a Transform's rotation to match a Vector using Quaternions 0 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