Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 shadow11235 · May 17, 2013 at 06:50 PM · rotatetranslaterotatearoundrolldice

How to rotate an object around its 3 axis and translate it on 2 world axis so it looks like its a dice rolling?

Hi everyone, I am new to unity and I am trying to rotate a dice around its 3 axis and translate it at the same time on 2 axis. The problem I am facing is if I rotate it without translating it works fine and same for translating without rotating, but once I do both together it starts rotating in a weird way and not around itself and it also doesn't translate the way it is supposed to. Can someone help me figure out what I am missing here ? Thanks!

This what I am doing:

Vector3 center = gameObject.collider.bounds.center; gameObject.transform.Translate(velocity); gameObject.transform.RotateAround(center,Vector3.right,angularVelocity.x); gameObject.transform.RotateAround(center,Vector3.up,angularVelocity.y); gameObject.transform.RotateAround(center,Vector3.forward,angularVelocity.z);

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

3 Replies

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

Answer by whydoidoit · May 17, 2013 at 07:00 PM

Well you are moving it in local space so that sorts your weird movement out. If the dice is modelled centrally then all of those rotate arounds are probably a bit much too, and you aren't using Time.deltaTime so the rotation and movement isn't frame rate independent.

 Vector3 center = gameObject.collider.bounds.center; 
 gameObject.transform.Translate(velocity * Time.deltaTime, Space.World);
 gameObject.transform.RotateAround(center,Vector3.right,angularVelocity.x * Time.deltaTime); 
 gameObject.transform.RotateAround(center,Vector3.up,angularVelocity.y * Time.deltaTime); 
 gameObject.transform.RotateAround(center,Vector3.forward,angularVelocity.z * Time.deltaTime);

I would probably have done this, which might work for you:

 var t = gameObject.transform;
 t.position += velocity * Time.deltaTime;
 var angle = t.eulerAngles;
 angle += angularVelocity * Time.deltaTime;
 t.eulerAngles = angle;
Comment
Add comment · Show 5 · 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 shadow11235 · May 17, 2013 at 07:28 PM 0
Share

Thanks for the answers, but yeah I am using the collider bounds to calculate the center of rotation because the pivot point is not at the center of my dice, I will be fixing that soon.

But Whydoidoit is right, the problem I am seeing is mostly with the translation, if I translate without rotating the dice goes in an upward/right direction, but once I try to rotate the dice while translating it will change direction according to the dice's local axis, it will start by going upward and as the dice rotate it will start translating in a circular position almost. Is there a way to translate the dice in a certain direction regardless of its rotation ?

Thanks!

avatar image whydoidoit · May 17, 2013 at 07:30 PM 0
Share

So firstly - don't post comments as answers, on UA Answer means Solution and not Reply. There's an add new comment button hidden on the right. - I converted it for you, your Answers currently require moderation while your comments do not.

The first part of my answer does that translation for you while maintaining your RotateAround logic.

avatar image whydoidoit · May 17, 2013 at 07:30 PM 0
Share

Basically use Space.World and then also multiply by Time.deltaTime (increasing your velocity if it gets too slow after doing that).

avatar image shadow11235 · May 17, 2013 at 08:26 PM 0
Share

I couldn't find the Add new comment button, had to google it and someone suggested clearing my browser's cache...it seems that it fixed it..and yes the first part of your answer solved it for me..Thanks!

avatar image whydoidoit · May 17, 2013 at 09:03 PM 0
Share

Ah, yep - doubly hidden then!!!

avatar image
0

Answer by robertbu · May 17, 2013 at 07:05 PM

My guess is the issue is your use of the collider bounds to calculate the center of rotation. Is there a particular reason why you are using the center of the collider? Is it because the pivot point is not at the center of your dice? If so you can either 1) re-author in your 3D editor program, 2) Use the SetPivot editor script, or 3) make the visible object a child of an empty game object positioned at the correct pivot point. Once you do that, you can do something this to make them roll:

 Transform.Rotate(angularVelocity * Time.deltaTime, Space.Self); 


Comment
Add comment · Show 3 · 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 whydoidoit · May 17, 2013 at 07:07 PM 0
Share

The movement's wrong because the translation is in local space though don't you think? (tumbling velocity is not related to the rotation of the body).

avatar image robertbu · May 17, 2013 at 08:35 PM 0
Share

@whydoidoit - you are right, and I missed it. I was trying to figure out how the RotateAround() center was moving:

 transform.Rotate(angularVelocity*Time.deltaTime, Space.Self);
 transform.Translate(velocity*Time.deltaTime,  Space.World);
avatar image Vipul-Dudharejiya · Dec 15, 2014 at 07:42 AM 0
Share

Thanx so much... i got my solution of translating and rotating by these two line...

transform.Rotate(angularVelocity*Time.deltaTime, Space.Self);

transform.Translate(velocity*Time.deltaTime, Space.World);

avatar image
0

Answer by bigbat · May 17, 2013 at 07:27 PM

I think changing your movement code to something like this solve the problem : transform.translate( velocity*Time. deltatime,space.world );

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

15 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

Related Questions

How to rotate a dice to a final angle ? 1 Answer

Apply torque/force to rotate an object around a fixed point 1 Answer

Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not? 1 Answer

RotateAround help 1 Answer

Tube Shooter Movement Design Help 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