Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Under_Sun · Mar 05, 2014 at 08:55 AM · c#camerarotate

Rotating a camera 90 degrees around an object

I've attached this script to the camera and am using it to rotate the camera around an object. It's currently rotating freely but I'd like it rotate 90 degrees smoothly, over 60 frames, when the button is pressed. So that four presses of the left arrow key would have the have the camera rotate the full 360 degrees over 240 frames and back to it's starting position. I can't seem to figure out how to do this. Any help would be appreciated.

 public GameObject targetObject;
 public float rDistance = 1.0f;
 public float rSpeed = 1.0f;;
 
 // Update is called once per frame
 void Update()
 {
 
     // Trigger functions if Rotate is requested
     if (Input.GetKey(KeyCode.LeftArrow)) {
         RotateLeft(true);
     } else if (Input.GetKey(KeyCode.RightArrow)) {
         RotateLeft(false);
     }
 
 }
 
 protected void RotateLeft(bool goLeft)
 {
 
     float step = rSpeed * Time.deltaTime;
     float orbitCircumfrance = 2F * rDistance * Mathf.PI;
     float distanceDegrees = (rSpeed / orbitCircumfrance) * 360;
     float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI;
 
     if (goLeft) {
         transform.RotateAround(targetObject.transform.position, Vector3.up, -distanceRadians);
     } else {
         transform.RotateAround(targetObject.transform.position, Vector3.up, distanceRadians);
     }
 
 }
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
3
Best Answer

Answer by EvilWarren · Mar 05, 2014 at 12:55 PM

This is a basic solution from what I understood of what you wanted, building on code you have supplied but hopefully it is enough for you to refine to what fits best.

     public GameObject targetObject;
     private float targetAngle = 0;
     const float rotationAmount = 1.5f;
     public float rDistance = 1.0f;
     public float rSpeed = 1.0f;
      
     // Update is called once per frame
     void Update()
     {
      
         // Trigger functions if Rotate is requested
         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             targetAngle -= 90.0f;
         } else if (Input.GetKeyDown(KeyCode.RightArrow)) {
             targetAngle += 90.0f;
         }
      
         if(targetAngle !=0)
         {
             Rotate();
         }
     }
      
     protected void Rotate()
     {
 
         float step = rSpeed * Time.deltaTime;
         float orbitCircumfrance = 2F * rDistance * Mathf.PI;
         float distanceDegrees = (rSpeed / orbitCircumfrance) * 360;
         float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI;
         
         if (targetAngle>0)
         {
             transform.RotateAround(targetObject.transform.position, Vector3.up, -rotationAmount);
             targetAngle -= rotationAmount;
         }
         else if(targetAngle <0)
         {
             transform.RotateAround(targetObject.transform.position, Vector3.up, rotationAmount);
             targetAngle += rotationAmount;
         }
      
     }
Comment
Add comment · Show 2 · 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 TeraTech · Jan 20, 2016 at 02:51 PM 0
Share

How come you never use the variable distanceDegrees, or distanceRadians in your example?

avatar image Dremand · Jan 31, 2018 at 09:52 PM 0
Share

Wondering how to Rotate around the player, like a boom stick. I used this and it works well but stays in one position... so you can't see the player any more. Probably something easy to fix, just thought I'd ask in case someone else was having a similar problem.

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

23 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

Related Questions

Rotate Camera on axis and X axis not working 1 Answer

Logical Camera errors... 1 Answer

Multiple Cars not working 1 Answer

Movement with camera and rotation of objects 1 Answer

Get object orientation 3 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