Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by Baalhug · Sep 18, 2018 at 09:46 PM · rotationrigidbodystop

How to stop MoveRotation when rigidbody is facing a specific euler angle

I have a cup which has some objects inside. I want to rotate the cup from a script so it lets the objects fall. I must use Rigidbody.MoveRotate to avoid the objects pass through the cup's mesh while it's moving. So default cup's rotation (euler) is (0,0,0) and it must rotate on both x and y axis to (135,70,0). The problem is how to know when the cup has reached the correct rotation so I can stop MoveRotation to keep rotating forever.

Here is my code:

 void Update () {
        Vector3 rota = new Vector3(135, 70, 0);
        Quaternion rotationDelta = Quaternion.Euler(rota * Time.deltaTime);
        Rigidbody rigid = GetComponent<Rigidbody>();
                 
        rigid.MoveRotation(rigid.rotation * rotationDelta);
             
 }

I have tried to check:

  if (Quaternion.Angle(transform.rotation, Quaternion.Euler(rota)) > angleThreshold)

But it won't work because in real time Euler angles of the object are so messed up by the quaternion rotation (because I'm rotating 2 axis at once and the rotation on 1 axis is actually moving the other axis as it's rotating and viceversa, that's why unity uses quaternions in the first place)

Any help?

Comment
Add comment
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

2 Replies

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

Answer by maxoja · Sep 18, 2018 at 11:09 PM

The solution toward your problem is relatively similar to the answer from this question.

However, due to your rigidbody-related constraints, it needs some little modification and the code below should work.

 public float speed = 1;

 float ratio = 0;
 Quaternion rotationA = Quaternion.Euler(0, 0, 0);
 Quaternion rotationB = Quaternion.Euler(135, 70, 0);

 public void Update()
 {
     ratio += Time.deltaTime; 
     ratio = Mahtf.Clamp(ratio, 0f, 1f);
     rigid.MoveRotation(Quaternion.Lerp(rotationA, rotationB, ratio));

     if ( ratio == 1 ) 
         print("it is stopped now")
 }


I wish this helps.

Comment
Add comment · Show 4 · 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 Baalhug · Sep 18, 2018 at 11:36 PM 0
Share

It works good, nice solution the lerp between 0 and 1

avatar image Baalhug · Sep 27, 2018 at 05:51 PM 0
Share

@maxoja after some testing I have realised the rotation is completed before ratio gets to 1, and I can't find why. I mean, the object finishes rotating in the right angle but at that time ratio is 0,3. As I need to inmediatly move the object after it finishes its rotation the result is a 0.7 seconds delay between rotation and movement :(

avatar image maxoja Baalhug · Sep 27, 2018 at 06:50 PM 0
Share

No offense, I am rather positive that ratio should be 1.0 when your object rotated to the target rotation. Let's figure this out together ;). First of all, I wonder where(which part of code) did you debug ratio and how did you check that your object should start moving.

avatar image mikeohc Baalhug · Feb 28, 2021 at 08:56 AM 0
Share

in case anyone needs this. What @Baalhug said might be the case if you do modify rotationA as you rotate. This could be you using transform.rotation. What you need to do is store transform.rotation at the start of the rotation to rotationA. Then update it after the ratio reaches 1.

avatar image
0

Answer by kevojo · Sep 19, 2018 at 05:08 AM

Just use vector3.angle

 Transform AimTransform;  //this object will hold the target rotation
 
 AimTransform.Lookat(someTarget);
 
 if (Vector3.angle(transform.forward, AimTransform.forward) > .5f)
 {
     rb.moveRotate(someTarget)
 }
 
 

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 Baalhug · Sep 27, 2018 at 05:54 PM 0
Share

@kevojo that would work if rotation was limited to a single axis, but that's not the case

avatar image kevojo · Mar 25, 2019 at 12:59 AM 0
Share

A bit late, but no. it is simply the angle between the direction vectors. They can be on any axis.

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

146 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 avatar image avatar image

Related Questions

Stopping rotation of rigidbody 2 Answers

Stop moving rigidbody but don't stop rotating 1 Answer

Object slope rotation issue 0 Answers

Is there any way to keep increasing an object's angular velocity? 1 Answer

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 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