• 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
Question by manikiran22 · Sep 08, 2017 at 07:33 AM · rotationgameobjecttransformkeykeypress

How to Rotate Game Object in specific Angle repeatedly on Key Press?

Ok, so I am trying to rotate an Equilateral Triangle in Y-Axis. So basically I want this Triangle to rotate +120 degrees everytime I press the key "W" and -120 degrees every time I press the Key "S".

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Casy10 · Sep 08, 2017 at 08:35 AM

Hello @manikiran22 ! Hope this helps you :

 using UnityEngine;
 
 public class FixedAngleRotator : MonoBehaviour
 {
     public float XAngle;
     public float YAngle;
     public float ZAngle;
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.W))
             transform.Rotate(XAngle, YAngle, ZAngle);
         if (Input.GetKeyDown(KeyCode.S))
             transform.Rotate(-XAngle, -YAngle, -ZAngle);
     }
 }

Comment

People who like this

0 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 manikiran22 · Sep 09, 2017 at 04:54 PM 0
Share

Man thanks @Casy10 a ton it is rotating as I wanted it to :) , one thing i want to implement is the triangle is rotating fine but is there a way to see its rotation smoothly like i want the user to see the triangle rotate +120 or -120 degrees. thanks again :)

avatar image Litleck manikiran22 · Sep 09, 2017 at 05:50 PM 0
Share

I may be able to help, what do you mean by wanting the user to see it rotate +120 or -120 degrees? Do you want it to instantly turn that much or slowly rotate until it has turned 120 degrees?

avatar image CraigGraff · Sep 10, 2017 at 03:20 AM 0
Share

@manikiran22 This will do what you want nicely, I think.

 using UnityEngine;
 using System.Collections;
 
 public class FixedAngleRotator : MonoBehaviour
 {
     public Vector3 EulerRotation = new Vector3(0f, 120f, 0f);
     public float TimeToRotate = .5f;
 
     private bool rotating;
 
     private void Update()
     {
         if (!rotating)
         {
             if (Input.GetKeyDown(KeyCode.W))
             {
                 StartCoroutine(doRotate(EulerRotation));
             }
             else if (Input.GetKeyDown(KeyCode.S))
             {
                 StartCoroutine(doRotate(-EulerRotation));
             }
         }
     }
 
     private IEnumerator doRotate(Vector3 rotation)
     {
         Quaternion start = transform.rotation;
         Quaternion destination = start * Quaternion.Euler(rotation);
         float startTime = Time.time;
         float percentComplete = 0f;
         rotating = true;
         while(percentComplete <= 1.0f)
         {
             percentComplete = (Time.time - startTime) / TimeToRotate;            
             transform.rotation = Quaternion.Slerp(start, destination, percentComplete);
             yield return null;
         }
         rotating = false;
     }
 
 }

avatar image mahmoud_mousa CraigGraff · Apr 25, 2019 at 12:48 PM 0
Share

I don`t understand why you multiply the two Quaternion Quaternion destination = start * Quaternion.Euler(rotation); It`s working prefect without this multiplication.

avatar image ATLGAN CraigGraff · Jul 23, 2020 at 11:08 AM 0
Share

But what is mean that? percentComplete = (Time.time - startTime) / TimeToRotate;

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

130 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 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

Auto leveling help 0 Answers

How can I defer the recalculation of child transforms whilst modifying the parent transform? 0 Answers

Transform a gameobject y rotation to another gameobject y rotation 1 Answer

postion scale and rotation greyed out and not working 1 Answer

Transform - GameObject? 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