Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 steelserpent2001 · Jul 12, 2019 at 02:49 AM · rotationtroubleshooting

Rotating object while button is held

Sorry for saturating the forum, but I couldn't find any results after an hour of searching for a solution. I assume that's why my question was taken down last time without being posted at all, but I really do need some help from anyone willing. I can make it work if I don't hold down the buttons, but I believe that implementing this feature will help make the game more intuitive as opposed to adjusting the angle by 5 degrees every time you press up or down.

Edit: Furthermore, I forgot to mention that when I press the up and down keys, the engine crashes.

Any help is greatly appreciated.

 void UpThing()
     {
         Vector3 direction = new Vector3(0, 0, 1);
         targetRotation *= Quaternion.AngleAxis(-5, direction);
     }
     void DownThing()
     {
         Vector3 direction = new Vector3(0, 0, 1);
         targetRotation *= Quaternion.AngleAxis(5, direction);
     }
     void Update()
     {
         if (Input.GetKeyDown("up"))
         {
             up = true;
         }
         if (Input.GetKeyUp("up"))
         {
             up = false;
         }
         while(up)
         {
             InvokeRepeating("UpThing", 1f, 1f);
         }
             
         
         if (Input.GetKeyDown("down"))
         {
             down = true;   
         }
         if (Input.GetKeyUp("up"))
         {
             down = false;
         }
         while (down)
         {
             InvokeRepeating("DownThing", 1f, 1f);
         }
         
        
         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10 * smooth * Time.deltaTime);
         
 
     }

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

2 Replies

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

Answer by ADiSiN · Jul 12, 2019 at 03:58 AM

Hello!

So, I believe that the reason why your engine crashes it's because when you set up or down boolean to true the engine each update invoke repeating and it stucking on each other forever so it crashes.

Try this code:

 public float speed = 2f;
 
 bool b_IsRotating = false;
 Vector2 v2_Movement;
 Quaternion q_StartRotation;
 
 private void Start()
 {
     q_StartRotation = transform.rotation;
 }
 
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
         b_IsRotating = true;
 
     if (Input.GetMouseButtonUp(0))
         b_IsRotating = false;
 
     if (b_IsRotating)
     {
         v2_Movement.Set(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"));
 
 
         transform.Rotate(v2_Movement * speed, Space.Self);
     }
 
     if (Input.GetMouseButtonDown(1))
         transform.rotation = q_StartRotation;
 }


So, we have speed to adjust speed rotation, b_IsRotating to see if we should track mouse input change and store it in v2_Movement. Also we save start rotation to q_StartRotation just so palyer wil be able to quick reset rotation. When you press left mouse button you set boolean to true and when we release it then we change it to false. When we press right mouse button we reset the rotation. If you have qny question regarding to this script - ask. You can improve it by storing rotating object in variable, because currently you should put this script on each object that you want to rotate what's not good :)

Comment
Add comment · Show 1 · 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 steelserpent2001 · Jul 12, 2019 at 07:14 PM 0
Share

Thank you so much for your reply! It gave me the hint I needed to solve this thing.

avatar image
0

Answer by bgoldbeck · Jul 12, 2019 at 03:15 AM

Try using GetKey instead of GetKeyUp/Down

Comment
Add comment · Show 1 · 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 steelserpent2001 · Jul 12, 2019 at 03:39 AM 0
Share

Thank you for cleaning up my code; however, this doesn't stop the engine from crashing.

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

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

Flip over an object (smooth transition) 3 Answers

Realtime cubemap that respects rotation? 0 Answers

Velocity bug with hinge joints forces in C# 0 Answers

transform.rotation cancels movement. 1 Answer

Network.Instatiate Problem 0 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