• 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
Question by Coolaswa · Sep 18, 2014 at 01:26 PM · c#camerarotation

How can I rotate the camera around an object?

I need to rotate the camera around an object. It needs to be a fixed length from the Object, and must go right or left on user input. Right now I am using Cosines and Sines for it, double checked the math and it turned out right. When I try it out, the camera seems to be moving correctly until a certain point it starts making it own route. Someone have an idea/pointer? Thanks in advance!

 public class CameraView : MonoBehaviour {
     public GameObject center; // Object to rotate around
     public float speed = 1f;
     private float xcoord, ycoord, zcoord;
     private Vector3 aim;
 
     void Start () {
         xcoord = transform.position.x;
         zcoord = transform.position.z;
         ycoord = transform.position.y;
         aim = center.transform.position;
     }
     
     void Update () {
         if(Input.GetKey ("right")){
             xcoord = 20 * Mathf.Sin (Mathf.Asin (xcoord / 20) + speed); // Math to get new x and z position
             zcoord = 20 * Mathf.Cos (Mathf.Acos (zcoord / 20) + speed);
             transform.position = new Vector3(xcoord, ycoord, zcoord);
             transform.LookAt(aim);
         }
         if (Input.GetKey ("left")){
             xcoord = 20 * Mathf.Sin (Mathf.Asin (xcoord / 20) - speed);
             zcoord = 20 * Mathf.Cos(Mathf.Acos (zcoord / 20) - speed);
             transform.position = new Vector3(xcoord, ycoord, zcoord);
             transform.LookAt(aim);
         }
 
     
     }
 }
 
Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Sep 18, 2014 at 01:27 PM

Assuming the object you are rotating around is stationary, you can use Transform.RotateAround():

 #pragma strict
 
 public var speed : float = 135.0;  // Degrees per second
 public var object : Transform;
 
 function Update() {
     transform.RotateAround(object.position, Vector3.up, -Input.GetAxis("Horizontal") * speed * Time.deltaTime);
     
 }

I'm using the 'Horizontal' axis, w$$anonymous$$ch by default maps to the arrow keys and/or the a & d keys.

Comment

People who like this

0 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 Coolaswa · Sep 18, 2014 at 01:49 PM 0
Share

Works like a charm! Thanks a lot! For future debugging, can you tell me what I could have done wrong/better in my previous code?

avatar image robertbu · Sep 18, 2014 at 04:25 PM 0
Share

Without spending some time, I cannot tell you exactly what is wrong with your code, but two things jump out at me. First, the equation for Polar to Rect (which is what you should be doing for circling around an object) is:

 x = r * cos(theta);
 y = r * sine(theta);

As for Asin() and Acos() in the equation, seems to me that you just want to increment and decrement an angle, and in doing so, you want your speed to be multiplied by Time.deltaTime. So the code would be something like this:

     if(Input.GetKey ("right")){
         angle += speed * Time.deltaTime;
         xcoord = 20 * Mathf.Cos (angle); 
         zcoord = 20 * Mathf.Sin (angle);
         transform.position = new Vector3(xcoord, ycoord, zcoord);
         transform.LookAt(aim);
     }

And this code will only work if your object is at the origin. If you want an offset, you'll have to add that.

      transform.position = new Vector3(xcoord, ycoord, zcoord) + offset.

Note this can all be done with Vectors as well, which is the typical way you see problems like this one solved (if RotateAround() did not exist).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Camera to follow the player in the form of radius 1 Answer

Flip over an object (smooth transition) 3 Answers

how to clamp y axis with Quaternion.Euler in unity 1 Answer

Problem with Camera rotation script 3 Answers

RTS Camera movement wrong after rotation 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