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 manaslegodesigns · Nov 30, 2017 at 02:00 PM · rotate objectcubesmovinggameobject

How to Rotate and move a cube

i have a cube on a plane surface and when i press a left button i want the cube to move left with a +z and also rotate while moving and then come to rest till the next button click.

I am a beginner and have been trying to do this for over a week Any help would be appreciated For a visual representation of what i am saying you can also download the app 'adventure cube' and see how the cube moves and rotates at the same time .

Thanks

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
3
Best Answer

Answer by Hellium · Nov 30, 2017 at 08:12 PM

Create a script called CubeControls.cs and put the following code in it :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CubeControls : MonoBehaviour
 {
     public float speed;

     // The point the cube will rotate around
     // They represent the middle point of each 4 bottom edges of the cube
     Vector3 forwardRotationPoint;
     Vector3 backRotationPoint;
     Vector3 leftRotationPoint;
     Vector3 rightRotationPoint;
     Bounds bounds;
     bool rolling;
 
     void Start()
     {
         bounds = GetComponent<MeshRenderer>().bounds;

         // Compute the rotation points
         forwardRotationPoint = new Vector3( 0, -bounds.extents.y, bounds.extents.z );
         backRotationPoint = new Vector3( 0, -bounds.extents.y, -bounds.extents.z );
         leftRotationPoint = new Vector3( -bounds.extents.x, -bounds.extents.y, 0 );
         rightRotationPoint = new Vector3( bounds.extents.x, -bounds.extents.y, 0 );
     }
 
     void Update()
     {
         // Make sure you are not already rolling / moving
         if ( rolling )
             return;
 
         // Rotate around forward point when pressing the up button
         if ( Input.GetKey( "up" ) )
             StartCoroutine( Roll( forwardRotationPoint ) );
         // Rotate around back point when pressing the down button
         else if ( Input.GetKey( "down" ) )
             StartCoroutine( Roll( backRotationPoint ) );
          // Rotate around left point when pressing the left button
         else if ( Input.GetKey( "left" ) )
             StartCoroutine( Roll( leftRotationPoint ) );
         // Rotate around right point when pressing the right button
         else if ( Input.GetKey( "right" ) )
             StartCoroutine( Roll( rightRotationPoint ) );
     }
 
     // Make the cube roll around given rotation point
     private IEnumerator Roll( Vector3 rotationPoint )
     {
         // Compute the real rotation point according to current position
         Vector3 point = transform.position + rotationPoint;

         // Compute an axis to rotate in the correct direction
         Vector3 axis = Vector3.Cross( Vector3.up, rotationPoint ).normalized;
         float angle = 90;
         float a = 0;

         // Prevent the user from rolling since we already are
         rolling = true;
         
         while( angle > 0 )
         {
             // Compute the angle and rotate the cube around the point
             a = Time.deltaTime * speed;
             transform.RotateAround( point, axis, a );

             // Keep track of the remaining angle
             angle -= a;
             yield return null;
         }

         // Adjust the rotation to make sure the cube rotates **exactly** 90°
         transform.RotateAround( point, axis, angle );
 
         // Allow the user to roll in a new direction
         rolling = false;
     }
 }



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 Hellium · Nov 30, 2017 at 08:24 PM 0
Share

However, it will work only with a cube, not with a cuboid.

avatar image manaslegodesigns · Dec 01, 2017 at 04:20 AM 0
Share

This worked for me Thanks a lot Would be appreciated if you could explain the thing. Thanks!!

avatar image Hellium manaslegodesigns · Dec 01, 2017 at 08:32 AM 0
Share

I've added some comments to my code to make it easier to understand

avatar image ptsironiw · Dec 12, 2017 at 03:11 PM 0
Share

hello i'm try to make a cuboid to move can i change some how this code so can i move it or i have to write a new script ??

avatar image Hellium ptsironiw · Dec 12, 2017 at 08:11 PM 0
Share

Please, take a look at this forum thread for the cuboid script :

https://forum.unity.com/threads/rotating-cuboid-script.163603/

avatar image
0

Answer by text23d · Nov 30, 2017 at 06:23 PM

   bool StartCubeMovement = false;
   Quaternion q;
   Vector3 cubepos;
    if (Input.GetKeyDown(0))
   {
  StartCubeMovement =true;
   }
  if ( StartCubeMovement == true)
  {
  cubepos = cube.transform.position;
  cubepos.z = cubepos.z +1 ;
  cube.transform.position=cubepos;
  q =  cube.transform.rotation;
  q.z=q.z+1;
  cube.transform.rotation=q;
  }
 
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

74 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

Related Questions

Character Movement won't work properly because of the rotation 1 Answer

how to jump to the left\right? 1 Answer

How to rotate a game object in a given path? 2 Answers

Hexagon Match3 selecting adjacent objects and rotating clockwise, 0 Answers

problem with rotation 2D 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