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 /
  • Help Room /
avatar image
0
Question by potu1304 · Aug 29, 2016 at 09:28 PM · camerarotationmovementaxis

How to rotate fluently/smoothly?

Hello!

At the moment I am making a game like Legend of Grimrock or the old type of games. I am wondering how I can do the fluent roating? At the moment I rotate like this:

  public void RightTurn()
         {
             this.transform.Rotate(Vector3.up, 90);
             this.transform.forward = this.transform.forward.Round(0);
             this.Turned();
         }
 
 private void Turned()
         {
             this.canWaitIntersection = false;
             this.waitingIntersection = false;
             this.OnPlayerTurned();
         }
 
   private void OnPlayerTurned()
         {
             Action<Vector3> handler = this.PlayerTurned;
             if (handler != null)
                 handler(this.player.transform.forward);
         }

Any ideas and/or suggestions? :) Thanks in advance!

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
17
Best Answer

Answer by mj321 · Aug 29, 2016 at 09:58 PM

Here's a script that rotates an object by 90 degrees if you press space. The rotation itself happens inside a CoRoutine. I'm still inexperienced in Unity and i suspect there's an easier way to do it. But that's how i'd do it today :)

 using UnityEngine;
 using System.Collections;
 
 public class Rotator : MonoBehaviour
 {
   void Update ()
   {
     if( Input.GetKeyDown(KeyCode.Space) )
     {
       StartCoroutine( Rotate(Vector3.up, 90, 1.0f) );
     }
   }
 
   IEnumerator Rotate( Vector3 axis, float angle, float duration = 1.0f)
   {
     Quaternion from = transform.rotation;
     Quaternion to = transform.rotation;
     to *= Quaternion.Euler( axis * angle );
    
     float elapsed = 0.0f;
     while( elapsed < duration )
     {
       transform.rotation = Quaternion.Slerp(from, to, elapsed / duration );
       elapsed += Time.deltaTime;
       yield return null;
     }
     transform.rotation = to;
   }
 }
Comment
Add comment · Show 13 · 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 InsertNamehere · Aug 30, 2016 at 05:59 PM 0
Share

Hey man i used your code and it works awesome just like what i was looking for. Can i use it in my commercial game? Your code is awesome it's working really good and smooth!!! Thank you so much!

avatar image mj321 InsertNamehere · Aug 30, 2016 at 06:19 PM 1
Share

Sure, you can use it however you like.

avatar image InsertNamehere mj321 · Aug 30, 2016 at 06:26 PM 0
Share

Thanks so much! I have 1 point to give and it's yours now!

Show more comments
avatar image potu1304 · Aug 30, 2016 at 06:04 PM 0
Share

Thank you it works fine! :)

The only problem I have now is, that it recognizese treasures some how in the wrong angle. So if I am walking to wards the treasure and turn right to face it, nothing happens, if I turn right again than it recognizes the treasure. I am not sure why because the camera is rotating right and is facing in the right direction? I forget to mention my CheckFacingDirection(); method:

 private void CheckFacingDirection()
 {
         RaycastHit hitInfo;
         if (Physics.Raycast(this.transform.position, this.transform.forward, out hitInfo, 1))
         {
             GameObject hitObject = hitInfo.collider.gameObject;
             TreasureChest tChest = hitObject.GetComponent<TreasureChest>();
             if (tChest != null && !tChest.IsOpened)
             {
                 this.interactionPanel.gameObject.SetActive(true);
                 this.interactionPanel.UpdateContent(tChest);
                 return;
             }
         }
     this.interactionPanel.gameObject.SetActive(false);
 }

avatar image mj321 potu1304 · Aug 30, 2016 at 06:23 PM 1
Share

The Coroutine lets the rotation happen in the background and Turned() gets called immediately. Try moving the Turned() call to the end of the Coroutine (After transform.rotation = to; )

avatar image potu1304 mj321 · Aug 30, 2016 at 07:03 PM 0
Share

Perfect, it works! :D

avatar image SuperMoeClub · Mar 16, 2018 at 06:51 AM 0
Share

New to Unity as well and ended up using this script as well but I've noticed a small issue. It would seem that if you were to hit the button twice it undoes the last animation. For instance, if you hit it once, wait until it's at 10 degrees and then hit it once more it ends up resting at 100 degrees rather than 90 degrees or 180 degrees. I've decided to try using this script to make a simple camera dolly of sorts by attaching it to an empty that the camera is tied to. It rotates at 90 degree angles around the character when you press an input. I've also got a separate input to rotate the object 90 degrees in the opposite direction. It works if you give it time to complete the rotation but if you double-tap the buttons or hit both inputs then the camera gets a little confused. If the player were to mash the two then it just bugs out completely. Do you happen to know if there is a way to force it to fully execute the rotation before accepting another input? Or if not, is it possible to force the input onto a stack of sorts (Hitting it twice will execute the first then the second, etc.). Thanks for the help, sorry for the bother.

avatar image SuperMoeClub SuperMoeClub · Mar 16, 2018 at 07:20 AM 1
Share

I had just realized, just by adding StopAllCoroutines(); to the end of the script it would do exactly that. Pardon the question earlier.

avatar image Nithinsvs · Jan 22, 2019 at 12:42 PM 0
Share

The code works fine...thanks for that...But how to make the rotation whilst moving forward and also after rotation forward movement should be based on continuous holding of mouse button (Input.mouseButton(0))...Help me, thanks in advance :-)

avatar image stroibot · May 13, 2019 at 08:33 PM 0
Share

So, axis = Vector3.up and angle is 180. Somehow the X of my object also rotates, but I only need it to rotate around Y axis. Any help? Thanks!

Show more comments

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

89 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

Related Questions

Move an object by rotation 0 Answers

Hard coded rotation of child results in weird arc rotation when parent is rotated? 0 Answers

How to access y rotation of an object as a variable or value 1 Answer

Problems with rotating the player on the Y axis C# 0 Answers

MouseOrbitImproved slanted orbit 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