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
1
Question by creed24 · Oct 04, 2012 at 02:46 AM · transformrotatebone

Rotating a bone via scripts

 Goal: To be able to rotate bone in script. shoulder is the bone I'm trying to rotate.
 Problem: No errors, but bone either
 1.) doesn't rotate in game
 or 2.) mesh doesn't move with bone
 
 Here's my script:

shoulder is a Transform var

 function PointToMouse(){
     if(shoulder){
         print("shoulder");
     }
     var playerPlane = new Plane(Vector3.forward,transform.position);
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hitdist = 0.0;
     if (playerPlane.Raycast (ray, hitdist)) {
     var targetPoint = ray.GetPoint(hitdist);}
     mouseVec = Vector3(targetPoint.x - transform.position.x,targetPoint.y - transform.position.y,0);
     zAngle = Vector3.Angle(mouseVec,Vector3(0,-1,0));
     if(zAngle>minZ && zAngle<maxZ){
     print(zAngle);
     shoulder.transform.localEulerAngles = Vector3(0,0,zAngle+270);
     }
 }


It prints shoulder and prints zAngle and zAngle is correct. Also, when shoulder is an Empty GameObject with a mesh as a child, the mesh will rotate. So I know the logic is right...

Any help?

Comment
Add comment · Show 3
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 nixtwiz · Oct 04, 2012 at 06:38 PM 0
Share

Are the bones of your mesh animated?

avatar image creed24 · Oct 04, 2012 at 11:02 PM 0
Share

Yes, they are.

avatar image creed24 · Oct 04, 2012 at 11:34 PM 0
Share

Aha! If I don't play the animations, the bone will rotate. How can I override the animation??

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by nixtwiz · Oct 05, 2012 at 12:37 AM

Put the rotation code in LateUpdate. LateUpdate is called after the animation is applied, so you can override the animation movement. HOWEVER, if you do this the rotation cannot rely on the previous rotation of the bone, so no lerps or anything of that sort. You'll get a jittering effect if you try to use a lerp with the previous rotation of the bone since the animation is still being applied before the next time the lerp is called.

Comment
Add comment · Show 5 · 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 creed24 · Oct 06, 2012 at 04:30 AM 0
Share

Thank you so much! Using LateUpdate solved my problem.

avatar image Ronin6 · Jan 19, 2013 at 04:44 PM 0
Share

Thank you for this good answer. Never knew LateUpdate existed.

avatar image ProbePLayer · Oct 05, 2013 at 11:47 PM 0
Share

sweet Geezus. Thanks nixtwiz, you are like mother tereza to me right now. really helped me. I was having the same problem, although translation was actually possible while the mesh had animation on. weird?

avatar image unity_user_12345 · May 21, 2015 at 04:33 PM 0
Share

could you please advise me how to save the rotation for the follwing frames when I apply the rotation? as it keeps jettering and flickering

avatar image rakeshjone · Mar 28, 2016 at 03:02 PM 0
Share

O$$anonymous$$G i spent 12 hours looking for the solution and you answered this in 2012 :)

Thanks a ton man :)

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

14 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

Related Questions

Added rotation AFTER the rotation from animation clip ( rewarding 400 points to whoever solves this) 1 Answer

How to make a bone rotate facing towards a given Transform? 0 Answers

How to prevent Z axis rotation ? 1 Answer

How can I rotate a bone from script? Applications: Move crane arm, point gun... 2 Answers

Updating rotation of client not working - strange error 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