• 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 beanworks42 · Jun 05, 2012 at 03:04 AM · animationproceduralarmature

Odd bone rotation problem

Everyone, anyone:

I'm having a fairly strange problem doing procedural animation on the bones of a model. The bone I'm animating controls a lance in a knight figure. Ive attached the camera to the lance for a close view of what the user is doing with it.

When I animate the lance using bone, I find that up-down controls work properly, but side-side controls introduce a strange tilt in the perspective. It looks like it turns side-to-side, but it also rotates around the forward axis. The picture contained in lance-at-rest.zip show the neutral horizon pic, and the file contained in lance-problem.zip shows the problem.

I'm fairly confident that my script isnt at fault, as I've done a simple abstract model with two bones, and when I animate the upper bone (as a stand-in for my lance bone in the real model, side-to-side rotation works perfectly - that is there is no tilt around the forward axis which causes the horizon to be slanted. The script (it is quick and dirty) is included here for grins:

using UnityEngine; using System.Collections;

public class TestController : MonoBehaviour {

 public float sensitivityX = 5F;
 public float sensitivityY = 5F;

 readonly public float minimumX = -30F;
 readonly public float maximumX = 30F;

 readonly public float minimumY = -30F;
 readonly public float maximumY = 30F;

 private float rotationX = 0F;
 private float rotationY = 0F;
 
 private Quaternion originalRotation;
 
 private bool initialized = false;
 
 private float LowPassKernelWidthInSeconds = 1.0f;
 private float AccelerometerUpdateInterval = 1.0f/60.0f;
 
 private float LowPassFilterFactor = (1.0f/15.0f) / 1.0f;
 
 private Vector3 lowPassValue = Vector3.zero;
 
 private Vector3 centerRPY = Vector3.zero;
 
 public Transform referenceTransform = null;
 
 public Transform lanceTransform = null;
 
 private bool useLocalTransform = false;
 
 // Use this for initialization
 void Start () {
     
     Screen.orientation = ScreenOrientation.LandscapeLeft;
 
     // get the original rotation, so that we can reset the per-frame rotation wrt the original

// originalRotation = transform.localRotation; originalRotation = lanceTransform.localRotation;

     print ("lance local rotation: " + lanceTransform.rotation);
     print ("lance reference rotation: " + referenceTransform.rotation);        
     
 
 }
 
 void Awake() {
     
     
     
 }
 
 Vector3 LowPassFilter(Vector3 newSample) {

     lowPassValue = Vector3.Lerp(lowPassValue, newSample, LowPassFilterFactor);

     return lowPassValue;

 }    
 
 
 private Vector3 getRollPitchYaw(){
     
     Vector3 rpy = Vector3.zero;
     
     Vector3 dampedAcc = LowPassFilter(Input.acceleration);
     
     if(dampedAcc.sqrMagnitude > 1) {
     
         dampedAcc.Normalize();
         
     }
     
     rpy.x = adjustedAtan2(dampedAcc.z, dampedAcc.x);
     
     rpy.y = adjustedAtan2(dampedAcc.z, dampedAcc.y);    
     
     return rpy;
     
 }
 
 private float adjustedAtan2(float y, float x) {
 
     float angleRad = Mathf.Atan2(y, x);
     
     float angleDeg = Mathf.Rad2Deg * angleRad;
     
     if( (x >= 0 && y < 0) || (x < 0 && y < 0) ) {
     
         angleDeg += 360;
         
     }    
 
     return angleDeg;
     
 }

 
 public float ClampAngle (float angle, float min, float max) {
     
     if(angle < -360F) {
         angle += 360F;
     }
     if(angle > 360F) {        
         angle -= 360F;
     }
     
     return Mathf.Clamp (angle, min, max);
 
 }        
 
 float pitchRate = 1.0f;
 float rollRate = 1.0f;
 
 
 private void doRunAnimation() {
 
     if(Input.GetKeyDown(KeyCode.W)) {

// running = !running; // animation.CrossFade("idle"); animation.CrossFade("lower_lance"); useLocalTransform = true; }

// if(running == true) { // animation.CrossFade("run"); // } else { // animation.CrossFade("idle"); // }

 }    
 
 void Update() {
     
     doRunAnimation();
     
 }
 
 // Update is called once per frame
 void LateUpdate () {
     

// print(Screen.orientation);

     if(Input.touchCount > 0) {
     
         centerRPY = getRollPitchYaw();
         
         initialized = true;
     }
     
     if(initialized == false) {        
         return;            
     }
     
     Vector3 dampedRPY = getRollPitchYaw();
     
     float pitchIncrement = centerRPY.y - dampedRPY.y;
     float rollIncrement = centerRPY.x - dampedRPY.x;
     
     rotationX = rotationX + (pitchRate * pitchIncrement * Time.deltaTime);
     rotationY = rotationY + (rollRate * rollIncrement * Time.deltaTime);
     
     rotationX = ClampAngle (rotationX, minimumX, maximumX);
     rotationY = ClampAngle (rotationY, minimumY, maximumY);
     

// Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up); // Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.forward);

     Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, lanceTransform.up);
     Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, lanceTransform.forward);        
     

// transform.localRotation = originalRotation xQuaternion yQuaternion;

     if(useLocalTransform == false) {
         lanceTransform.localRotation = referenceTransform.localRotation * xQuaternion * yQuaternion;

// lanceTransform.localRotation = referenceTransform.localRotation xQuaternion; } else { lanceTransform.localRotation = lanceTransform.localRotation xQuaternion yQuaternion; // lanceTransform.localRotation = lanceTransform.localRotation xQuaternion; }

// lanceTransform.rotation = lanceTransform.rotation xQuaternion yQuaternion;

     print ("lance rotation: " + lanceTransform.rotation);
     print ("lance local rotation: " + lanceTransform.rotation);

// print ("lance reference rotation: " + referenceTransform.rotation); // print ("root rotation: " + transform.localRotation);

 }

}

If anyone has any ideas of what rock I should look under next to solve this problem, I'd be very grateful.

Thanks in advance,

jonbitzen42

[1]: http://answers.unity3d.com/storage/temp/1140-lance-at-rest.zip

lance-at-rest.zip (330.0 kB)
lance-problem.zip (390.6 kB)
Comment
Add comment · Show 2
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 You! · Jun 05, 2012 at 06:11 AM 0
Share

Have you used a regular animation and then simply used "Animation.Play()"? It would be a lot easier than a ton of code.

You can create and edit animations using the Animation window (Ctrl+6).

avatar image hysp · Jan 15, 2013 at 06:28 PM 0
Share

did yo solve it? we are having similar issues. I ve expend around 30 hours and I can clearly understand what is going on... any help would be much appreciated

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I make animations snap to a frame? 1 Answer

Jerky Procedural Material Animation 2 Answers

Inverse Kinematics On Animation 0 Answers

My animation will not play after I apply armature in Blender. 1 Answer

How do I overlay (blend) my own procedural animation on top of a canned animation? 3 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