Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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
Add comment
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

· Add your reply
  • Sort: 
avatar image
0
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
Add comment · 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

$$anonymous$$an 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 : $$anonymous$$onoBehaviour
 {
     public Vector3 EulerRotation = new Vector3(0f, 120f, 0f);
     public float TimeToRotate = .5f;
 
     private bool rotating;
 
     private void Update()
     {
         if (!rotating)
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
             {
                 StartCoroutine(doRotate(EulerRotation));
             }
             else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.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;

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

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

Control Ship and Gun Separately 2 Answers

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