Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
1
Question by bobogravyman · May 13, 2019 at 03:54 AM · rotationquaternionangleglitchlerping

Rotation of gameobject to a given angle?

Novice question here. I'm having difficulty coding the rotation of my character's legs back to a standing position after he's stopped moving. Essentially, I made the legs of the character as children of the parent character model, because I wanted them to sort of rotate freely without needing to animate them under the influence of physics. I realized that since my game is 2D and I locked the actual character's rotation, it means that the legs won't be able to rotate either, and I have to script it manually. I've gotten it to go back to its original position from where it ends up after moving (I'm just adding torque right now to cause some rotation, but not randomly varying the torque over time, which I plan on implementing), but it seems inconsistent and will go back sometimes but not others, or do it slowly. Also, if I jump, the leg I applied the script to doesn't seem to follow the parent's y movement, and it becomes disconnected from my character's body. I know I could probably do this more easily through animation, but I've consistently had trouble with various rotation implementations (like quaternions and lerping and whatnot) in the past, and I want to improve that and see if I can understand why I'm getting this behavior. Here's a sample of the script I'm using to move the legs:

 public class LegMovement : MonoBehaviour
 {
     //init    
     Rigidbody2D rb2d; 
     PlayerController2D player;
     ConstantForce2D force2D;    
     private bool rotatingToZero = false; // used to flag when the player has stopped moving and the legs should rotate back to their default positions    
     // Start is called before the first frame update
     void OnEnable()
     {
         rb2d = GetComponent<Rigidbody2D>(); 
         player = FindObjectOfType<PlayerController2D>(); 
     }    
     // Update is called once per frame
     void Update()
     {    
         if(player.isMoving == true)
         {
             rb2d.AddTorque(2f);
             rotatingToZero = false; 
         }
         else
         {
             rotatingToZero = true;    
             Vector3 to = new Vector3(0, 0, 0); // going to be rotating to a local angle of 0 degrees    
             if (Vector3.Distance(transform.eulerAngles, to) > 0.01f) //if the distance in degrees from the current angle to the desired angle is greater than 0.01 (magnitude, so won't be negative)
             {
                 transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime*3.0f);
             }
             else //this is for when it's within 0.01 distance, because the Lerp will go on forever
             {
                 transform.eulerAngles = to;
                 rotatingToZero = false; 
             }
             //if (rb2d.rotation < 0)
             // {
                 //while(rb2d.rotation != 0) 
                 /*for (float i = rb2d.rotation; i < 0; i += 0.001f)
                 {
                        rb2d.AddTorque(0.001f);    
                        if (rb2d.rotation == 0)
                        {
                          return;
                        }
                 }*/   
                /* float originalAngle = transform.rotation.eulerAngles.y;
                 float zeroAngle = 0f;   
                 transform.rotation = Quaternion.AngleAxis(originalAngle + (Time.deltaTime * 10f), Vector3.up);
                 rb2d.rotation += Time.deltaTime * 10f; 
                 */
 
              //}
            //else if (rb2d.rotation > 0)
              //{
                 //while(rb2d.rotation != 0)
                 /*for (float i = rb2d.rotation; i > 0; i -= 0.001f)
                 {
                    rb2d.AddTorque(-0.001f);
 
                    if (rb2d.rotation == 0)
                     {
                       return;
                     }    
                 }*/    
                 /*float originalAngle = transform.rotation.y;
                 float zeroAngle = 0f;    
                 transform.rotation = Quaternion.AngleAxis(originalAngle + (Time.deltaTime * -10f), Vector3.up);
                 rb2d.rotation += Time.deltaTime * -10f;*/                                
             //}                                
                 //rb2d.AddTorque(-rb2d.angularVelocity * 5f);
                 Debug.Log(rb2d.angularVelocity);                   
         }                      
     }
 }

Here's a picture of how I have the character set up: alt text

And here's a picture of what happens if I have the character jump and the leg I attached the script to doesn't follow; the green rectangle that the red arrow is pointing to is the leg (other leg is only fine because I didn't test the script on it yet): alt text Thanks for any help you guys can give

PS: Post re-edited. Please use Code sampler (or Crtl+K) to post code... Bye!

character-leg-glitch.png (10.0 kB)
character-setup.png (14.4 kB)
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 Captain_Pineapple · May 13, 2019 at 08:21 AM 0
Share

can you please use the proper code formatting feature to display your code? it's unreadable in the current version.

avatar image DCordoba · May 13, 2019 at 01:50 PM 2
Share

hi, the problem is there:

         Vector3 to = new Vector3(0, 0, 0); //going to be rotating to a local angle of 0 degrees    
          if (Vector3.Distance(transform.eulerAngles, to) > 0.01f) //if the distance in degrees from the current angle to the desired angle is greater than 0.01 (magnitude, so won't be negative)
          {
              transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime*3.0f);
          }

listen, Vector3 lerp was designed to be used on distances, if you try to use it on euler (3 axis movement) angles, this will be a considerable mess

if you see the graph to a angle rotation, by one of the sides, (the sin or cos are this functions) you will have a sinusoid, so, all the angle translations are curved on a flat space, you cant interpolate it using a straight interpolation like the Vector3.Lerp function

in short, this is not the proper function to do a angle interpolation, please use Quatenion.Lerp

   Quaternion to = Quaternion.identity; //going to be rotating to a local angle of 0 degrees    
   if (Quaternion.Angle(to, transform.rotation) > 0.01f) //if the distance in degrees from the current angle to the desired angle is greater than 0.01 (magnitude, so won't be negative)
    {
         transform.eulerAngles = Quaternion.Lerp(transform.rotation, to, Time.deltaTime*3.0f);
    }

also check this old and great example credits @Oana

avatar image bobogravyman DCordoba · May 21, 2019 at 02:57 AM 0
Share

Thanks for the help @DCordoba ! That seems to have worked in terms of rotating the leg where I want it; the only problem is that it doesn't follow the player and still becomes detached if I jump or anything. I'm thinking it's because I handled the movement in FixedUpdate while the leg movement was done in Update? If you have any advice as to where to move the code I would greatly appreciate it! And @Captain_Pineapple sorry about that I noticed after I had posted it that my code was mangled; this was my first question and in the future I'll be sure to check on that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyPalmSprings · May 21, 2019 at 03:05 PM

Can't you take the rotation the legs had before the player moved, and set it back to that after the character moves?

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

144 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

Related Questions

How can I multiply the rotation of a mirrored object? 1 Answer

How can I angle an image along an line renderer and maintain a look-at-camera rotation? 0 Answers

how to convert the angle to roatation? 1 Answer

How do I clamp the Z-Axis Rotation for this code? 1 Answer

Quaternion and rotation angles 2 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