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 rjr · Jan 06, 2014 at 11:14 AM · cuberoll

How to roll a cube?

I want to roll a cube by 90 degrees whenever i press arrow keys in respective direction without attaching rigid-body to it. I want to roll the cube slowly(animation) by 90 degrees. Help me with an example code. Thanks in advance.

Comment
Add comment · Show 2
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 GameVortex · Jan 06, 2014 at 11:22 AM 0
Share

This depends on if you want to roll the cube from the center or from the corner. The first one is easy, the second one more difficult.

avatar image UnbreakableOne · Jan 06, 2014 at 12:19 PM 0
Share

I would either go to moving the mass center and applying force to it's top surface/edge or just use plain old animations.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jan 06, 2014 at 04:58 PM

Your question is open to intrepretation. Here is a bit of code that rotates a cube on its edge to walk a cube through rotation.

 #pragma strict
 
 public var speed = 45.0;
 private var offset = 0.5;  // Assues a cube of 1 unit on a side.
 private var tr : Transform;
 private var rotating = false;
 
 function Start() {
     tr = transform;
 }
 
 function Update () {
     var pos : Vector3;
     
     if (Input.GetKeyDown(KeyCode.UpArrow) && !rotating) {
         pos = Vector3(tr.position.x, tr.position.y-offset, tr.position.z+offset);
         DoRotation(pos, Vector3.right, 90.0);    
     }
     else if (Input.GetKeyDown(KeyCode.DownArrow) && !rotating) {
         pos = Vector3(tr.position.x, tr.position.y-offset, tr.position.z-offset);
         DoRotation(pos, -Vector3.right, 90.0);    
     }
     else if (Input.GetKeyDown(KeyCode.RightArrow) && !rotating) {
         pos = Vector3(tr.position.x+offset, tr.position.y-offset, tr.position.z);
         DoRotation(pos, -Vector3.forward, 90.0);    
     }
     else if (Input.GetKeyDown(KeyCode.LeftArrow) && !rotating) {
         pos = Vector3(tr.position.x-offset, tr.position.y-offset, tr.position.z);
         DoRotation(pos, Vector3.forward, 90.0);    
     }
 }
 
 function DoRotation(pos : Vector3, axis : Vector3, degrees : float) {
     var curr = 0.0;
     rotating = true;
     while (curr != degrees) {
         curr = Mathf.MoveTowards(curr, degrees, Time.deltaTime * speed);
         tr.RotateAround(pos, axis, Time.deltaTime * speed);
         yield;
     }
     rotating = false;
 }

Note this code does the rotation incrementally and it does not take into account the last fractional rotation. If you are going to be rotating a bunch of times, you will see errors in the rotation creep in, and therefore will have to add some additional code to assure the code has only it's specified 90 degree rotation.

Comment
Add comment · Show 1 · 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 Torian · Apr 20, 2015 at 06:53 PM 0
Share

Hi, used your code but adapted it to C#. Why is this not working with a Rigidbody component that uses gravity in Unity 5?

Also, after rolling the cube a couple of times, the cube does not remain on the same plane and it will go under any element that was below it, like in the attached pic.alt text

What could cause this?

Thanks!

cube-roll-err.png (44.6 kB)
avatar image
0

Answer by Kolodej · Jan 06, 2014 at 01:56 PM

You can simply use a animation (with defined speed and rotation around center) and start the animation when key is pressed. This is sometimes the simplest solution. Or you can attach e.g. RotationCubeScript and in Update routine increase transform.EulerAngle.x/y/z until target angle or target increment is reached. Consider using more sophisticated functions such as Mathf.Lerp, Mathf.Dampf(angle). It should be simple code for a few lines.

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

Answer by thomasfriday · Jul 10, 2021 at 03:33 PM

Here's a short Youtube video that covers exactly how to roll a cube on its edges: https://youtu.be/06rs3U2bpy8


rolling cube

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

23 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

Related Questions

How to Roll a Rigidbody Cube one step? 3 Answers

Cube roll over hills/objects 2 Answers

Rolling a cube 2 Answers

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

Move a cube on it's edges 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