• 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 Pzula · Jul 12, 2016 at 05:49 AM · rotationphysicsalignmentangularvelocity

Rotate Rigidbody to Rotation Via AngularVelocity

Hello all,

I'm looking to get a rigidbody object to align its rotation with another object by moving the rigidbody via angular velocity/forces. The rigidbody is not effected by gravity and is motionless in space.

Here's a breakdown of what I'm trying to achieve:

  1. Object A has a rigidbody

  2. Object B is a gameobject with a rotation

  3. Object A (Rigidbody) should add/set its angular velocity so its rotation matches Object B.

The rotation can be gradual.

The rotation is not limited to any axis; the object can rotate freely.

I'm really not great at rotations/quaternions and this is giving me a lot of problems.

Any help is much appreciated.

Thank you!

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 _Yash_ · Jul 12, 2016 at 08:44 AM 0
Share

try this code:

  float dir = Vector3.Cross(A.transform.forward, B.transform.forward).y;
  A.GetComponent<Rigidbody>().angularVelocity = new Vector3(0f, Vector3.Dot(A.transform.forward, B.transform.forward)*dir, 0f) * lerpSpeed;

adjust lerpSpeed to change how quickly it matches the rotaion of B.

avatar image Pzula _Yash_ · Jul 12, 2016 at 12:32 PM 0
Share

Hey, thanks for your response.

I gave your code a try and it works great, but it only rotates around the y axis. I tried implementing rotation for the other axes and while it worked at first, it becomes inaccurate at more extreme rotations.

avatar image _Yash_ Pzula · Jul 13, 2016 at 05:03 AM 0
Share

Okey, you are right,
other solution i can think of is this,

 void FixedUpdate(){
     // if this doesn't work try "B.transform.eulerAngles - A.transform.eulerAngles" below
     Vector3 difference = A.transform.eulerAngles - B.transform.eulerAngles;
     Vector3 velocity = difference / Time.fixedDeltaTime;
     A.rigidbody.angularVelocity = velocity;
 
 }

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

80 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

Related Questions

Problems with rotating an object using angular velocity. 2 Answers

Gravity on a rotating platform 1 Answer

Predicting trajectory using Equations of Motion considering drag & Magnus effect 0 Answers

Calculate Angular Velocity to Match Remote Position 0 Answers

How to get Steering Wheel Rotation angle? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges