Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 moEJoe82 · Nov 16, 2019 at 05:11 AM · rotate objectrigidbody physics

Rotate 3D Object using rigidbody.rotation rather than transform.rotate

Hello everyone,

I am trying to rotate a 3d object based on touch using its rigidbody rather than transform, so i won't violate physics, and to be able to collide properly with other objects while rotating.

This code works perfectly using Transform.Rotate, but I am not able to change it to use RigidBody.Rotation.

I would appreciate any help how can i do this! Thanks.

 public class RotateObject : MonoBehaviour
 {
     public float Velocity = 0.5f;
     public float VelocityFactor = 1;
     Vector3 mPrevPos = Vector3.zero;
     Vector3 mPosDelta = Vector3.zero;
 
     public Vector3 lastDirectionAsResisting;
 
     bool isTouchActive;
 
     private void FixedUpdate()
     {
         if (Input.GetMouseButton(0))
         {
             if (isTouchActive == false)
             {
                 mPrevPos = Input.mousePosition;
                 isTouchActive = true;
             }
             mPosDelta = Input.mousePosition - mPrevPos;
             ApplyRotation(mPosDelta);
             mPrevPos = Input.mousePosition;
         }
         else
         {
             isTouchActive = false;
         }
     }
 
     void ApplyRotation(Vector3 delta)
     {
         delta *= Velocity * VelocityFactor;
         if (Vector3.Dot(transform.up, Vector3.up) >= 0)
         {
             transform.Rotate(transform.up, -Vector3.Dot(delta, Camera.main.transform.right), Space.World);
         }
         else
         {
             transform.Rotate(transform.up, Vector3.Dot(delta, Camera.main.transform.right), Space.World);
         }
 
         transform.Rotate(Camera.main.transform.right, Vector3.Dot(delta, Camera.main.transform.up), Space.World);
     }
 
 
     public void CapturePreviousDelta()
     {
         lastDirectionAsResisting = Vector3.down;
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ermiq · Nov 16, 2019 at 08:57 AM

To rotate a rigidbody you'd better use Rigidbody.MoveRotation() to get advantage of the rigidbody interpolation setting to get smooth rendering with no jittering.
By rotating a rigidbody with rigidBody.rotation you just apply the rotation immediately therefore you might violate physics. Use MoveRotation() when you need to rotate the rigidbody in update loops.

To use this you'll need a rotation quaternion. Something like this:

 Quaternion rotationToMove;
 
 void ApplyRotation(Vector3 delta)
  {
      delta *= Velocity * VelocityFactor;
      if (Vector3.Dot(transform.up, Vector3.up) >= 0)
      {
          // get the rotation which rotates 'x' degrees around the 'axis'.
          // 'x' will be your dot product, 'axis' - transform's up vector.
          rotationToMove = Quaternion.AngleAxis(-Vector3.Dot(delta, Camera.main.transform.right), transform.up);
      }
      else
      {
          rotationToMove = Quaternion.AngleAxis(Vector3.Dot(delta, Camera.main.transform.right), transform.up);
      }
      rigidBody.MoveRotation(rotationToMove);
  }

Not sure why you rotate the transform again then relative to the camera though.

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

116 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

Related Questions

How to make a cube turn in the direction it is moving? 1 Answer

How do I stop a rigidbody from rotating after colliding with another object? 1 Answer

Object is passing from colliders when swipe with rigidbody.position 1 Answer

Plane CollisionWheels keep jumping around 0 Answers

Rotation of enemies 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