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 chillypacman · Dec 08, 2012 at 05:47 AM · rotationquaterniondirection

Rotation to go from one direction to another?

I have a target direction say (0,1,0) and a current direction a transform is facing, say (0,-1,0), how can I calculate the rotation required to make the transforms direction (0,1,0) so that I can apply it with something like transform.Rotate((x,y,z))

The reason I'd like to do this is that I have a skinned character and I want to make him point in a direction of my choosing, unfortunately setting transform.forward on a bone does not necessarily mean he points in the right direction.

Ok I think I came up with a solution but it's not perfect, is it the correct way to do it or am I doing something wrong?

 void LateUpdate()

(....)

 Vector3 origin = transform.GetChild(0).position;
 Vector3 currentDir = (transform.GetChild(0).GetChild(0).GetChild(0).position - origin).normalized; //direction based on outstretched arm, it's from hand to arm

 Vector3 targetDir = (targetPos - origin).normalized; //target direction to point

 transform.GetChild(0).rotation *= Quaternion.FromToRotation(targetDir, currentDir);



My current problem is the arm is still being affected by the parents rotations rather than just pointing, so it wobbles as its parent transform has rotations applied to it..

Comment
Add comment · Show 5
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 MountDoomTeam · Dec 08, 2012 at 07:23 AM 0
Share

I can't find the exact formulas, you can do this with sine and cosine , you can also convert the unit vectors to eulerAngles, anyway normally I know this but right now I don't! http://www.jmargolin.com/uvmath/uvmath.htm

avatar image fafase · Dec 08, 2012 at 07:55 AM 0
Share

Use simply quaternion.Slerp: http://unitygems.com/quaternions-rotations-part-1-c/

avatar image whydoidoit · Dec 08, 2012 at 08:31 AM 0
Share

It sounds like you are trying to write an I$$anonymous$$ solution - these are pretty difficult (I know I wrote 95% of one and then heard about mechanim which includes it!). Your problem may be a twist around the axis of the bone which you aren't fixing with the FromToRotation.

avatar image MountDoomTeam · Dec 08, 2012 at 09:50 AM 0
Share

this question has got me confused, normally it's a lot easier to work with eulerAngles and transform.rotation, find the target.transform.rotation and the origin.transform.rotation and rotate the eulerAngles in between, like that you can control the child's transform by using local rotation and rotation.

avatar image whydoidoit · Dec 08, 2012 at 10:49 AM 0
Share

Well I'd say a FromToRotation was the right solution in this case, given that the OP has a desired vector and an existing vector being updated every frame, it makes sense to do it like that.

@chillypacman you really need to cache those transforms rather than calling GetChild multiple times per frame.

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

12 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

Related Questions

Unity thinks my Quaternion is a Float 2 Answers

Rotate vector3 relative to another vector3 1 Answer

Copy Rotation Direction only on Y Axis. 1 Answer

Rotate object to target position 1 Answer

How can I move an object to click point in 2D? 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