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 /
  • Help Room /
avatar image
0
Question by RealCool_Guy · Aug 11, 2017 at 07:20 AM · rotationtilttransform.rotate

How can I properly tilt an object about two axes, while preventing the other axis from rotating?

I've been trying to make a very simple game. You have a gameboard, you tilt it and a marble rolls around on it. I've been able to tilt the gameboard well enough, but I'm encountering a small problem. As I keep tilting the game board, it seems to become offset in the wrong direction.

For example I want to be able to tilt each side of the game board. Basically I am rotating the 'x' and 'z' axes towards the 'y' axis. The gameboard is in the 'xz' plane if that helps visualize it.

However, the game board starts to "spin" or rotate in the xz plane, either counterclockwise or clockwise, which I want to avoid because it just feels awkward.

  1. Is this avoidable?

  2. If it is, how can I change my code to fix this?

  3. As a side note, I use Quaternion.slerp to reset the gameboard back to original position when left idle. I heard that you don't really want to mess with the Quaternion. Is there a more efficient way to do this?

Here is my code, thank you in advance.

 public class BoardTiltControl : MonoBehaviour
 {
 
     public float rotationspeed = 1;
     public float gameboardTilt = 0.4F;
     private float x_pos, y_pos, x_pos_mouse, y_pos_mouse, x_pos_diff, y_pos_diff, time_off_press = 0;
     private Quaternion starting_pos;
     private Quaternion reset_y;
     private int back_to_origin = 0,  pressed = 0;
 
     Vector3 rotate_vect = new Vector3(0,0, 0);
  
     // Use this for initialization
     void Start ()
     {
 
         starting_pos = transform.rotation;
 
 
        // print(starting_pos.x);
 
     }
     
     // Update is called once per frame
     void FixedUpdate ()
     {
 
         if (Input.GetMouseButtonDown(1))
         {
             x_pos_mouse = Input.mousePosition.x; //get position of the mouse only on the frame it is pressed
             y_pos_mouse = Input.mousePosition.y;
         }
 
         else { }
 
         Rotation();
 
     }
 
     private void Rotation()
     {
         //float rothorizontal;
         //float rotvertical;
 
         if (Input.GetMouseButton(1)) //if the right mouse button is being held
         {
 
             back_to_origin = 0;
             pressed = 1; //means it is true that button is pressed
             x_pos = Input.mousePosition.x; //track the position of the mouse every frame
             y_pos = Input.mousePosition.y;
 
             x_pos_diff = x_pos - x_pos_mouse; //take differnce of the initial position when mouse was clicked and where the mouse is now
             y_pos_diff = y_pos - y_pos_mouse;
 
             rotate_vect = new Vector3(y_pos_diff, 0, -x_pos_diff); //create a Vector3 based on displacement
                                                                   //rotate_right = new Vector3(0, x_pos_final, 0);
 
             
 
             transform.Rotate(rotate_vect * gameboardTilt * Time.deltaTime, Space.Self);
            
 
 
         }
 
         else if ((Input.GetMouseButton(1) != true) && (pressed == 1)) //if mouse isn't being pressed. The "pressed" variable is to ensure that this statement is only entered once after not pressing, to ensure time_off_press isnt reset
         {
 
             time_off_press = Time.time; //take time snapshot after button is not being pressed
             //Debug.Log("Mouse is not pressed");
             pressed = 0; //false, button isn't being pressed
             
         }
 
         else if (((Time.time - time_off_press) > 2.0F) && pressed == 0) //if the interval of time the button hasn't been pressed for is greater than 2 seconds
         {
            // Debug.Log("Should reset gameboard");
             time_off_press = Time.time;
             pressed = 1; //will manually reset gameboard at this point, so it's as if the mouse has been pressed
             back_to_origin = 1; //condition to go back to origin
 
         }
         else if(back_to_origin ==1)
         {
              //Now find the current positions of the transform and compare it to the initial in order to rotate gameboard back to original state
 
             transform.rotation = Quaternion.Slerp(transform.rotation, starting_pos, Time.deltaTime*rotationspeed);
             //Debug.Log("BOARD RESET");
 
 
         }
     }
 }

Thanks again

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 kalen_08 · Aug 06, 2017 at 09:48 PM 0
Share

so basically you don't want any rotation from the y axis?

avatar image RealCool_Guy kalen_08 · Aug 07, 2017 at 02:00 AM 0
Share

It's hard to explain but basically, I don't want the board to rotate about the y-axis.

I've attached photos to explain what I mean. In the original, the board is straight, but after tilting it a few times, it becomes "misaligned" alt text

alt text

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unidad2pete · Aug 11, 2017 at 06:50 AM

I dont know if I understand fine your problem.

If you rotates an object about X, when you rotates Z, is based on X. To fix, you can make a parent Object to rotate X, and your gameboard object child of that parent to rotate Z. this way, X rotation not affects to Z, but you have to rotate both objects individualy.

Try this:

 public GameObject parentX;
     public GameObject gameboardZ;  // child of parentX
     public float rotationSpeed;
 
     void Update()
     {
 
         float X = Input.GetAxis("Mouse X");
         float Z = Input.GetAxis("Mouse Y");
 
         if(Input.GetMouseButtonDown(1))
         {
             // IF YOU ARE PRESSING MOUSE BUTTON, BOTH OBJECTS ROTATES MOVING YOUR MOUSE
             parentX.transform.Rotate(X, 0, 0);
             gameboardZ.transform.Rotate(0, 0, Z);
 
         } else
         {
             // IF YOU ARE NOT PRESSING MOUSE BUTTON, BOTH OBJECTS ROTATES TO ORIGIN AT rotationSpeed value.
 
             // X TO 0
             if (parentX.transform.localEulerAngles.x != 0)
             {
                 if (parentX.transform.localEulerAngles.x > 0)
                 {
                     parentX.transform.Rotate(-Time.deltaTime * rotationSpeed, 0, 0);
 
                     if (parentX.transform.localEulerAngles.x < 0)
                     {
                         parentX.transform.localEulerAngles = Vector3.zero;
                     }
 
                 }
                 else
                 {
                     parentX.transform.Rotate(Time.deltaTime * rotationSpeed, 0, 0);
 
                     if (parentX.transform.localEulerAngles.x > 0)
                     {
                         parentX.transform.localEulerAngles = Vector3.zero;
                     }
                 }
             }
 
             // Z TO 0
             if (gameboardZ.transform.localEulerAngles.x != 0)
             {
                 if (gameboardZ.transform.localEulerAngles.x > 0)
                 {
                     gameboardZ.transform.Rotate(-Time.deltaTime * rotationSpeed, 0, 0);
 
                     if (gameboardZ.transform.localEulerAngles.x < 0)
                     {
                         gameboardZ.transform.localEulerAngles = Vector3.zero;
                     }
 
                 }
                 else
                 {
                     gameboardZ.transform.Rotate(Time.deltaTime * rotationSpeed, 0, 0);
 
                     if (gameboardZ.transform.localEulerAngles.x > 0)
                     {
                         gameboardZ.transform.localEulerAngles = Vector3.zero;
                     }
                 }
             }
         }
     }

Is that your objective?,

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

90 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

Related Questions

Rotate Object by Tilting Android Device Any Angle 1 Answer

Tilt camera on movement 2 Answers

Tilt object based on movement and direction 1 Answer

How can i make accelerometer for my rolling ball android game? 0 Answers

How do invert the Y rotation of my player on collision with an object? 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