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 Danirey · Jan 10, 2014 at 12:00 PM · animationrotate objectriggingcusm rotation

Get angle difference between two objects to correct the position of a child object

Hi, guys!

I'm a little confused with Quaternions, eulers, angles etc... I've this problem: My character is facing forward. The animation rotates the spine a little to the right and the weapon is in the right hand and rotated to the left, so i want to rotate the spine until the forward of the gun points a certain point in the world. So i think i need to calculate the difference angle between the gun rotation and the target and apply it to the spine to rotate only the needed to make the gun point to the target. Of course when i make this, the animator component is disabled. I do that because i've unity free and can't use ik. The game is turn based so i don't need to play animation when aiming!

Thanks!

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 poncho · Jan 10, 2014 at 04:43 PM 0
Share

a little trigonometry could help, some ATan of each object would give you their respective angle, then the difference between those angles is your desired value, not sure what ik is but at least rotating those objects can be solved this way

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Danirey · Jan 10, 2014 at 05:10 PM

Thanks poncho!

IK is inverse Kinematics! For animations. With that i know that yo can still playing the animation but moving a hand to touch an object for example, but it is a unity pro feature.

I know that the trigonometry is the answer, but i've no idea of that. It would be nice that somebody could point me a little and then i'll go alone from there. But i need to know what Mathf.Atan does and so on! I already take a look to the mathf functions relative to angles but i cant see more than that. Not everybody has been made to know about maths! ;) ;) (and i'm the first)

Thanks man!

Comment
Add comment · Show 2 · 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 Danirey · Jan 10, 2014 at 05:25 PM 0
Share

Sorry, the above is supposed to be a comment!

avatar image itzikpel Danirey · Apr 22, 2017 at 09:23 PM 0
Share

Hi , Did you manage to solve it?

avatar image
0

Answer by robertbu · Jan 10, 2014 at 05:09 PM

I don't fully understand your geometry, so I'll give you some things to try. If your spine is created so that the 'forward' is in the direction of the object you want to aim at, you can do:

 spine.transform.LookAt(aimPosition);

Typically you would only want the spine to rotate on one axis. If the spine is parallel to Vector3.up, then you can do:

 var v = aimPosition - spine.transform.position;
 v.y = 0.0;
 spine.transform.rotation = Quaternion.LookRotation(v);

If you want a literal interpretation of what you are asking for would be this:

 var q = Quaternion.FromToRotation(gun.transform.forward, target.transform.position - gun.transform.position);
 spine.transform.rotation = q * spine.transform.rotation; 
Comment
Add comment · Show 10 · 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 Danirey · Jan 10, 2014 at 05:23 PM 0
Share

Hi robertbu,

thanks for your answer. The spine isn't aligned with the parent gameobject, so if the character look at the target, the spine is almost perpendicular to that direction and the gun is child of the arm bone and is facing in other direction as well. So i think that the third option is more close to what i need. If i understand right, here we get a quaternion that goes from the actual gun direction, to the direction that will point the gun to the target, and then we add the rotation difference to the spine. So the spine will rotate until the gun face the target point in the world! Fantastic! I'll try as soon as i can and see what happens. Thanks a lot!

avatar image Danirey · Jan 11, 2014 at 12:58 PM 0
Share

Ok! i tried and it rotates in the opposite direction.... It seems that this is not the answer after all! The theory in my head says that it should work, but it doesn't! $$anonymous$$aybe if i could get the angle between the objects in separated axis and then rotate each euler individually.... i don't know!

If i get the angle difference in the Y axis(vertical) between the weapon and the spine, and then rotate the spine these angle, i could test axis by axis it it works, but i cant get the data correctly. Vector3.Angle seems useless in this situation, and as i already said the $$anonymous$$ath is Black magic for me!

Please guys be patient with me! and try again ! ;)

avatar image robertbu · Jan 11, 2014 at 03:36 PM 0
Share

I find the effort in answering rotation questions is about 80% figuring out how the geometry is setup and the desired result and on 20% co$$anonymous$$g up with a solution. I don't understand the geometry here, so I gave you some generic tools to approach the problem. Using euler angles is problematic. I don't understand why it would rotate in the 'opposite direction', but did you try reversing the parameters in the FromToRotation() call? Beyond that, I really need more information...drawing, screen shot, short video, simple project to play with...something to help me understand the relationship between the object and the problem you are trying to solve.

avatar image Danirey · Jan 11, 2014 at 05:04 PM 0
Share

You're 100% right. So here we go! alt text

The 3 images on the top are from left to right the forward of the character, the spine forward and the gun forward. the other two images are the result of point to the enemy's head. As you can see the spine is looking at the enemy, and the gun is ai$$anonymous$$g to the left. This difference is tha value i'm looking for. so i can compensate the rotation to the riaght and make the gun point to the enemy's head or whatever part of the body i choose!

The code is:

                 var q = Quaternion.FromToRotation(shotOrigin.forward, actualTarget - shotOrigin.position);
                 spine.rotation = q * spine.rotation; 

shootOrigin is a transform and actualTarget is a Vector3. I've tried to modify the parameters but the results are always weird rotations.

This is the code i already have to make it appear that is ai$$anonymous$$g in the right direction, but at close range it looks at the moon!

                 var lookPos2 = actualTarget - shotOrigin.position;
                 var tempShotOriginRot = Quaternion.LookRotation(lookPos2);
                 tempShotOriginRot.eulerAngles+=Vector3(10,10,0);
                 spine.rotation.eulerAngles.x=tempShotOriginRot.eulerAngles.x;

Hope this makes more sense to may question!

And Thanks one more time! ;)

spine_config.jpg (197.4 kB)
avatar image robertbu · Jan 11, 2014 at 05:06 PM 0
Share

Helpful. I won't get a chance to study it until tonight.

Show more comments

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

20 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

Related Questions

Rotating an already rotated object. 1 Answer

Mecanim distorts 3D model 0 Answers

creating a disolvable ik rig in blender for unity3D mecanim 0 Answers

Everything works in Maximo but exporting it to Unity will give me problems.. 0 Answers

How can i animate my Object? (bones available) 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