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
1
Question by Bogdan003 · Mar 15, 2020 at 12:54 PM · cameratransformcamera-movementcamera rotate

How to rotate camera around an object to a certain amount of degrees?

I tried to rotate the camera around its own axis when i press a key by using RotateAround but the problem is that it instantly teleports the camera at the given angle instead of smoothly rotating to it.I tried multiplying the angle by time.deltatime but it just moved a little when i pressed the key.

         if (Input.GetMouseButtonDown(1))
         {   
             transform.RotateAround(parent.position, Vector3.up, angle);
         }
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
2
Best Answer

Answer by Hellium · Mar 15, 2020 at 01:31 PM

While the solution of metalted will effectively rotate smoothly the object, it requires you to hold the mouse button down.

Here is a solution to automatically rotate object only when the mouse button is pressed down.

 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     public float rotationSpeed = 10; // = 10° / sec, adjust value in inspector
     public float angle = 30; // adjust value in inspector
     public Transform parent;
     private float remaininAngle;
 
     void Update()
     {
         if(Input.GetMouseButtonDown(1))
             remaininAngle += angle;
         
         float newRemainingAngle = Mathf.MoveTowards(remaininAngle, 0, rotationSpeed * Time.deltaTime);
         float delta = remaininAngle - newRemainingAngle;
         remaininAngle = newRemainingAngle;
         transform.RotateAround(parent.position, Vector3.up, delta);
     }
 }

Note that if you click again while the rotation is running, it will rotate further. If you want to prevent this behaviour. Do as follow:

         if(Mathf.Approximately(remaininAngle, 0) && Input.GetMouseButtonDown(1))
             remaininAngle += angle;
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 Bogdan003 · Mar 15, 2020 at 04:24 PM 0
Share

Thank you so much this is exactly what i was expecting ^^;

avatar image
1

Answer by metalted · Mar 15, 2020 at 01:14 PM

The example script on this page : https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html, has a nice example script that we can change to suit your needs:

  using UnityEngine;
     
     //Attach this script to a GameObject to rotate around the target position.
     public class Example : MonoBehaviour
     {
         private Vector3 target = new Vector3(5.0f, 0.0f, 0.0f);
     
         void Update()
         {
             // Spin the object around the world origin at 20 degrees/second.
             transform.RotateAround(target, Vector3.up, 30 * Time.deltaTime);
         }
     }

So as you see in this script, using Time.deltaTime is the right way to go. The problem in your code is that you are using the wrong input type.


Input.GetMouseButtonDown will be true only on the moment the mouse button is pressed, so only one frame. The object will only rotate for that frame. If you want to get continuous input from the mouse button you should use Input.GetMouseButton(). So the changed script will look like this:

 using UnityEngine;
         
      //Attach this script to a GameObject to rotate around the target position.
          public class Example : MonoBehaviour
          {
              private Vector3 target = new Vector3(5.0f, 0.0f, 0.0f);
          
              void Update()
              {
                  if(Input.GetMouseButton(0)){       
                      // Spin the object around the world origin at 20 degrees/second.
                      transform.RotateAround(target, Vector3.up, 30 * Time.deltaTime);
                  }
              }
          }
Comment
Add comment · 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

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

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

Third person Camera stop it from going lower than the terrian 0 Answers

Would anyone teach me how to create camera movement and look controls? 2 Answers

Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer

I only rotated X, but Y and Z are shifting as well when I play the game. 1 Answer

How to allow camera to complete an upside down rotation while using LookAt() 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