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 myjean17 · Dec 12, 2011 at 03:03 PM · animationrotationjavascripttransform

Rotating An Object On Its Axis Once

I can't seem to find out how to do this just right. My basic idea is that there is a lever that, when the key "e" is pressed, will rotate to give it the effect that it is activated. This is a 2D game, so the lever is a plane. I have a custom pivot point at the middle-bottom of the plane.

How can I rotate the object JUST to the left one time when the "e" key is pressed? I have tried multiple different options, but they all cause the plane to go in strange directions. The only one that has worked so far is:

  function Update () {
 
       if (Input.GetKey ("e") {
 
            transform.Rotate (0, 45, 0 * Time.deltaTime, Space.World);
 
       }
 
  }

This did rotate the object gradually in the right direction as the "e" key was held, but this isn't exactly the effect I'm going for. I want an automatic rotation, which will stop at a certain angle.

Any ideas? Help is greatly appreciated.

-myjean17

EDIT**

Well, I haven't exactly solved my problem. So if anyone still wants to answer this question please do. But I instead created an animation for the lever turning on and off, and tied them together and its actually functioning quite well.

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 bouke1 · Dec 12, 2011 at 03:09 PM 0
Share

try using a separate function that does the rotating for 90 deg. and call that function with your e key

avatar image myjean17 · Dec 12, 2011 at 04:52 PM 0
Share

I Think Ive figured it out... mostly. I used a boolean to check if the function had been run, so it would only perform the rotation once. Now, however, i would like to rotate it gradually and not just have it appear in the new location.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robert_mathew · Dec 12, 2011 at 04:11 PM

float rotationleft = 360; float rotationspeed = 10;

void Update() { float rotation = rotationspeed * Time.deltaTime;

 {
     rotationleft-=rotation;
 }
 else
 {
     rotation = rotationleft;
     rotationleft = 0;
 }
 transform.Rotate(0,0,rotation);

}

Comment
Add comment · Show 2 · 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 Bunny83 · Dec 12, 2011 at 04:38 PM 0
Share

How about writing an actual answer? Also the OP uses UnityScript (JS) and not C#. Furthermore Your code seems missing an if statement because it doesn't make any sense this way.

ps. for the future: format your code in a way it's readable.

avatar image myjean17 · Dec 12, 2011 at 04:46 PM 0
Share

Is there an if missing in there? It would help if you could please explain what you just posted.

avatar image
0

Answer by suruz@kento · Dec 13, 2011 at 02:58 AM

Use Quaternion.Euler() to control rotation.

private var startRotY: float = 0; private var endRotY: float = 0; var currentRotY: float;

function Update () {

   if (Input.GetKey ("e") )
   {
         currentRotY = 0;
         startRotY = 0;
         endRotY = 45;
         transform.rotation = Quaternion.Euler(0,0 ,0);
   }
   
   currentRotY++;
   currentRotY = Mathf.Clamp(currentRotY,startRotY,endRotY);
   transform.rotation = Quaternion.Euler(0,currentRotY ,0);

}

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 SZ_95 · Sep 08, 2015 at 09:19 AM

This should work correctly. It turns an object one time (and only one time) 90 degrees on the Z axis.

 using UnityEngine;
 usingSystem.Collections;
 
 public class RotateHalt : MonoBehaviour
 {
     bool turn;
     
     if (Input.GetKeyDown(KeyCode.Q)) {
                 turn = true;
                 if(turn == true)
                 {
                 Vector3 rotation = new Vector3(0, 0, 90);
                 transform.Rotate (rotation);
                 }
                 turn = false;
             }
 }
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

8 People are following this question.

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

Related Questions

Move an object toward an angle in 2d space 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Rotating an object in position 2 Answers

Disabling a lineRender or Ray when there is no target 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