Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 MrPoggle · Jul 07, 2021 at 01:17 AM · rotationquaternionchildanglepositioning

Rotating an object so that the direction between it's children matches the forward of rotating object

Hello, I have been really struggling to solve this issue.

I have an object - "ParentObject" with a child that rotates into an appropriate position - "Rotator". The "Rotator" object has several children which we will call Grip1, Grip2, Grip3, Grip4. They are all in different locations that cannot be moved or rotated. The only object that can be moved or rotated in this scenario is the "Rotator".

I want to be able to programmatically rotate the "Rotator" object so that the direction between two selected Grips matches the forward direction of the ParentObject.

So I need to find the direction between the two Grips being used and then rotate their parent so that both the grips fall along a perfect line created by the forward direction of the parent.

I have tried so many things and I am lost. I was hoping something like this would work but it does not:

float angleDifferenceY = Mathf.Atan2(directionToObject.x, directionToObject.z) Mathf.Rad2Deg; float angleDifferenceX = Mathf.Atan2(directionToObject.y, directionToObject.z) Mathf.Rad2Deg; rotator.transform.Rotate(angleDifferenceX, angleDifferenceY, 0); execute = false;

Any help would be appreciated

Comment
Add comment · Show 1
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 Astrydax · Jul 07, 2021 at 06:58 AM 0
Share

please use the code block feature. That is really hard to read.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Astrydax · Jul 07, 2021 at 06:57 AM

You should be able to average the differences of the two grips from their target and set that as the direction of the rotator.

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

Answer by BastianUrbach · Jul 07, 2021 at 01:35 PM

You can use Quaternion.FromToRotation for this. It calculates the shortest rotation that rotates one vector to another. All you need to do is figure out the current direction of the grips and then FromToRotation can give you the rotation that you need to apply:

 var currentDirection = grip1.position - grip2.position;
 var targetDirection = parent.forward;
 var rotation = Quaternion.FromToRotation(currentDirection, targetDirection);
 rotator.rotation = rotation * rotator.rotation;

If you're interested in the math behind that function: the cross product of the two directions is the axis of the rotation and the acos of their dot product is its angle.

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

165 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

Related Questions

How to get angular difference? 2 Answers

Align GameObject to Terrain angle 2 Answers

Creating a multiple part turret what locks onto certain axis. 4 Answers

How to properly do Twin Stick shooter controls with an XBOX controller? 1 Answer

Error of angle 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