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
1
Question by zico_mahilary · Apr 22, 2013 at 03:46 PM · rotationtransformvector3euler angles

Smooth 90 degrees rotation on input . (infinite number of times)

Hi guys,

I have this game object that I am trying to rotate 90 degrees clockwise or anticlockwise according to user input.

Every time the user presses a key the object should slowly rotate 90 degrees and wait there . if the user presses the same key again it will rotate 90 degrees farther in the same direction, otherwise if the player presses the negative key for , the object rotates 90 degrees in the opposite direction... and so on.

So far i have this script. but the rotation is faulty, since every-time the player does an input, the rotation values get reset to (0,0,0)

buttonz.x is -1 on keypress 'q' and 1 on keypress 'e'.

 function Start () {
 
 }
 
 function Update () {
 
     if (buttonz.x==-1) {
     
         MyRotate();
          buttonz.x=0;
     }
      if (buttonz.x==1) {
      
          MyRotato();
           buttonz.x=0;
     }
 }
 
 function MyRotate() {
     for(var i = 0; i<90; i+=2) {
             //print ("Entered for. Value is " + transform.eulerAngles.z);
             yield;
             transform.eulerAngles = Vector3(0, 0, i);
     }
 }
     
     
 function MyRotato() {
     for(var i = 0; i>-90; i-=2) {
             //print ("Entered for. Value is " + transform.eulerAngles.z);
             yield;
             transform.eulerAngles = Vector3(0, 0, i);
     }
 }

Please help...

Thank You

Comment
Add comment · Show 1
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 Jabes22 · May 23, 2013 at 11:14 PM 0
Share

I'm trying to get rotations as well. I can't really give you an answer but all my research is pointing me towards quaternion.Slerp Looking up quaternion and slerp functions may point you in the right direction :) Wish I could help more.

1 Reply

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

Answer by robertbu · May 23, 2013 at 11:32 PM

You are directly assigning to 'eulerAngles' so that is giving your object an absolute rotation. So you code rotates the angle between 0 and 90 or 0 and -90. As a quick fix, you can change the code to use Transform.Rotate():

 #pragma strict
 
 function Update () {
  
     if (Input.GetKeyDown(KeyCode.A)) {
         MyRotate();
     }
      if (Input.GetKeyDown(KeyCode.S)) {
         MyRotato();
     }
 }
  
 function MyRotate() {
     for(var i = 0; i<90; i+=2) {
          //print ("Entered for. Value is " + transform.eulerAngles.z);
          yield;
          transform.Rotate(0,0,2);
     }
 }
  
 function MyRotato() {
     for(var i = 0; i>-90; i-=2) {
          //print ("Entered for. Value is " + transform.eulerAngles.z);
          yield;
          transform.Rotate(0, 0, -2);
     }

}

There is one problem with this code...it is not frame rate independent. That is, it rotates two degrees for each frame, so it will be twice as fast when Unity is run at 60 fps on the desktop compared to 30 fps it might run on a smart phone.

So here is a bit different take on it that is frame rate independent:

 #pragma strict
 
 public var speed = 55.0;
 private var rotation = 0.0;
 private var qTo = Quaternion.identity;
 
 
 function Update () {
  
     if (Input.GetKeyDown(KeyCode.A)) {
         rotation += 90.0;
         qTo = Quaternion.Euler(0.0, 0.0, rotation);
         
     }
      if (Input.GetKeyDown(KeyCode.S)) {
          rotation -= 90;
        qTo = Quaternion.Euler(0.0, 0.0, rotation);
     }
     
     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
     
 }

 
Comment
Add comment · Show 4 · 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 kecoman · Mar 24, 2015 at 11:56 AM 0
Share

how do you change it to a y axis

avatar image yoeyyutch · Mar 24, 2015 at 11:32 PM 1
Share

how do you change it to a y axis

The y axis value is the 2nd float value of a Vector3. i.e. Vector3(x,y,z) or in this case Quaternion.Euler(x,y,z), so in this case to change it to y axis change lines 12 and 17 to... qto = Quaternion.Euler(0f,rotation, 0f);
avatar image rooster2 · Sep 20, 2016 at 05:56 PM 0
Share

put this into c# as i work in c# also the quateron.identity doesnt work on c#

avatar image Owen-Reynolds rooster2 · Sep 20, 2016 at 08:22 PM 0
Share

Well, yes, that is unityScript. But Quaternion.identity works in C# just fine. FYI, unityScript stopped being the recommended language, in favor of C#, a few years ago.

But beyond that, this is just pointing you in the right direction. Transform.Rotate is an easy way to spin yourself. Quaternion.Euler is how you make an (x,y,z) angle. Quaternion.RotateTowards is a way to gradually spin one angle into another one.

If you look those up, you'll find the official inputs, C# examples, and lots more examples of people using them. Rotations take a while to learn, and you'll probably have to it learn a little, since you won't find exactly what you need prewritten.

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

Transform a Vector by a Quaternion 1 Answer

How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers

Instantiate GameObject towards player 0 Answers

Multiple Fire on different launcher GameObjects 1 Answer

Rotating a Vector direction by another Vector direction in one axis 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