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 BLKNinja40 · Feb 12, 2019 at 12:24 AM · rotationgameobjectquaternionlerpeuler angles

Smoothly rotating a gameObject 90 degrees on a button press and being able to rotate it in the opposite direction off of the same button.

I'm having trouble rotating my magnification scope +90 degrees on a button press to move it to the seeing position through to my holographic sight. Everything I have tried through Update will just snap the scope to its position without smoothly moving. The Code For That:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MagnifierRotation : MonoBehaviour
 {
     public GameObject Magnifier;
 
     Transform from;
     Transform to;
     float speed = 0.1f;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.N))
         {
             transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
         }
     }
 }

I've also tried using a Coroutine, but when I attempted to rotate the object off of its z-axis, it started rotating over all three axes instead of the z. The transform's rotation began at (-90, 0, 0), and after the coroutine rotation, of Vector3.forward, it ended up at a weird (-173. blahblahblah, 256.blahblahblah, 39.blahblahblah) instead of (0,0,0). The Code For That:

 public class MagnifierRotation : MonoBehaviour
 {
     private bool magnifierSwitch;
 
     IEnumerator RotateMe(Vector3 byAngles, float inTime)
     {
         var fromAngle = transform.rotation;
         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
         for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
         {
             transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
             yield return null;
         }
     }
     void Update()
     {
         if (magnifierSwitch && Input.GetKeyDown(KeyCode.M))
         {
             StartCoroutine(RotateMe(Vector3.right * -90, 0.8f));
             magnifierSwitch = !magnifierSwitch;
         }
 
         else if (!magnifierSwitch && Input.GetKeyDown(KeyCode.M))
         {
             StartCoroutine(RotateMe(Vector3.right * 90, 0.8f));
             magnifierSwitch = !magnifierSwitch;
         }
     }
 }

I have also tried Quaternion.Euler's and the good old transform.Rotate, but both have the same effect as the Quaternion.Lerp that just snapped the object when in Update and under an if statement checking for an input. If there is any other way to make the object smoothly rotate on a button press in both directions after one rotation is over, please let me know before I just make a broken looking animation.

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

0 Replies

· Add your reply
  • Sort: 

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

172 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Lerp Rotation C# 3 Answers

Copy rotation of targetObject to current object with an influence in the range from 0-1. 0 Answers

My Quaternion.Lerp() doesn't do anything 0 Answers

How to rotate towards the mouse on one axis while snapping 0 Answers

Pinball Flipper using Quaternion.Lerp? 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