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 helloiam · Jan 28, 2013 at 07:48 PM · smoothvaluessnappingblockdegrees

Rotations problem..

I want to rotate a cube smoothly with 45 degress each time, so if I click on the cube it snaps to 45 degrees and then to 90 degrees and so on till 360. What the problem is it goes not exactly to 45 degrees but 43.4 on the rotation and I want that it goes exactly to 45.

What I tried:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class useObject : MonoBehaviour {
 
     float thisStep = 0f;
     GameObject lastHit;
     
     void Start () {
         Screen.lockCursor = true;
     }
 
     void Update () 
     {
         if(thisStep == 0 && lastHit != null)
         {
             float rot = Mathf.RoundToInt(lastHit.transform.rotation.eulerAngles.x);
             
             print("r=" + rot);
             
             if(rot > 10 && rot < 45) // 0
             {
                 lastHit.transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
                 lastHit.transform.Rotate(new Vector3(Mathf.LerpAngle(0f,90f,Time.deltaTime),0f,0f));                
             }
             else if(rot > 45 && rot < 135) // 90
             {
                 lastHit.transform.rotation = Quaternion.Euler(new Vector3(0,90,0));
                 lastHit.transform.Rotate(new Vector3(Mathf.LerpAngle(0f,90f,Time.deltaTime),0f,0f));
             }
             else if(rot > 135 && rot < 225) // 180
             {
                 lastHit.transform.rotation = Quaternion.Euler(new Vector3(0,180,0));
                 lastHit.transform.Rotate(new Vector3(Mathf.LerpAngle(0f,90f,Time.deltaTime),0f,0f));
             }
             else if(rot > 225 && rot < 315) // 270
             {
                 lastHit.transform.rotation = Quaternion.Euler(new Vector3(0,270,0));
                 lastHit.transform.Rotate(new Vector3(Mathf.LerpAngle(0f,90f,Time.deltaTime),0f,0f));
             }
             else // 0
             {
                 lastHit.transform.rotation = Quaternion.Euler(new Vector3(0,0,0));
                 lastHit.transform.Rotate(new Vector3(Mathf.LerpAngle(0f,0f,Time.deltaTime),0f,0f));
             }
             
             print ("hallo2=" + lastHit.transform.rotation.x);
         }
         
         
         Ray raycast = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit raycasthit = new RaycastHit();
         if (Physics.Raycast(raycast, out raycasthit, 1000.0f) && Input.GetMouseButton(0) && thisStep < 360)
         {
             switch(raycasthit.collider.name)
             {
             case "Deel1":
             case "Deel2":
             case "Deel3":    
                 Vector3 rot = new Vector3(Mathf.LerpAngle(0f,90f,Time.deltaTime),0f,0f);
                 if(thisStep + rot.x <= 360)
                 {
                     lastHit = raycasthit.collider.gameObject;
                     raycasthit.collider.transform.Rotate(rot);
                     thisStep += rot.x;
                 }
                 else if(thisStep + rot.x == 360)
                 {
                     lastHit = raycasthit.collider.gameObject;
                     raycasthit.collider.transform.Rotate(rot);
                     thisStep -= rot.x;
                 }
                     
                 break;
             default: print("Unhandled raycastHit " + raycasthit.collider.name); break;
             }
         }
         else if(Input.GetMouseButtonUp(0))
         {
             thisStep += 0;
         }
     }
 }
 

 
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 robertbu · Jan 28, 2013 at 08:46 PM 0
Share

Comparing your words to your code, I'm confused by what you want to accomplish. Do you want an immediate rotation to 45 degrees or, or do you want a quick snap (i.e. rotation takes a few frames)? Which axis do you want to rotate around? What do you want to happen when the object reaches 360 degrees...stop and do nothing or reverse direction?

Some thoughts from looking at your code. Transform.Rotate() is relative. It rotates by the specified amount from a starting angle. Assigning Quaternion's to transform.rotation is absolute. You are assigning a specific rotation to the object. Any of the Lerp methods (including LerpAngle() need a parameter that increment/decrements over time in the range of 0 to 1. Time.deltaTime is the time since the last frame and remains fairly constant.

avatar image helloiam · Jan 28, 2013 at 09:16 PM 0
Share

What I want is when you click on the target it rotates slow and smooth to 90 degrees on the x- as of the target and if I click again it goes form 90 degrees to 180 also slow and smooth and so on till 360. And then from 360 the same rythm back. from 0/90/180/270/360 to 360/270/180/90/0

avatar image helloiam · Jan 28, 2013 at 11:11 PM 0
Share

Can u tell me how or where I can find/learn them, because I am really interested.

Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 28, 2013 at 09:48 PM

I recommend making the code that rotates the object a component of that object. You are using global code attached to some other objects you want to rotate. With that configuration, if you click on one object and start it rotating and then click on a second object object and start it rotating, the first object will stop rotating before the rotation is complete because you are reassigning lastHit. Attach the code below to any object you want to rotate by 90 degrees.

 public class RotateBy90: MonoBehaviour {
     public float maxDegreesPerSecond = 30.0f;
     private int iRotation = 0;  // Rotation = 90 * iRotation
     private int iInc = 1;
     private Quaternion qTo;
     
     void Start() {
         qTo = transform.rotation;
     }
     
     void OnMouseDown() {    
         iRotation += iInc;
         if (iRotation == 0) 
             iInc = 1;
         if (iRotation == 4)
             iInc = -1;
         qTo = Quaternion.Euler (iRotation * 90.0f, 0.0f, 0.0f);
     }
         
     void Update() {
         transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
     }
 }
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 helloiam · Jan 28, 2013 at 10:20 PM 0
Share

it works but again the values of x are from 0 to 90 and then to -5.008956e-06 then to 270 and as last to -5.008956e-06.

But what I want is that it goes from 0/90/180/270/360 in that way.

Thanks btw!

avatar image helloiam · Jan 28, 2013 at 10:21 PM 0
Share

So hope that you can help me further with it :)

avatar image robertbu · Jan 28, 2013 at 11:01 PM 0
Share

-5.008956e-06 is .000005008956...essentially 0.0. So the rotation vector is (0,180,180) which orients the cube the same as (180,0,0). This is the kind of thing you will get when working with Quaternions. The code does rotate by 90 degrees. This is not the problem in your code above. A setting of 43.4 is a fair ways away from 45 degrees. If you really need the values to read (180,0,0) there are other ways to rotate the cube, but the code will be more complex including dealing with the 360/0 boundary.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to smootlhy change values in amout of time? 3 Answers

Turning the camera down smoothly while running 2 Answers

how do i get the to snap to the closest 1 3 Answers

Smooth rotating by 90 degrees in x and y axis. 1 Answer

Animation speed control - no smooth animation on slow down 3 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