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 Edmonds · Jul 11, 2016 at 09:02 PM · camerarotationmath

Rotating a camera around a game object?

I tried to rotate the camera around an object in the game using trigonometry. But whenever the angle of any of the rotation coordinates goes above 90 degrees or below 270 the camera would quickly invert it's position coordinates and instantly put its rotation back between 270 and 90.

I used the code below, I have no idea why it isn't working.

     void Update () {
         camera_rotX = transform.rotation.x;
         camera_rotY = transform.rotation.y;
         camera_rotZ = transform.rotation.z;
         findPos ();
         transform.position = new Vector3 (camera_posX, camera_posY, camera_posZ);
     }

     void findPos(){
         camera_posX = (Mathf.Sin(camera_rotX)) * camera_distance;
         camera_posY = (Mathf.Sin(camera_rotY)) * camera_distance;
         camera_posZ = (Mathf.Sin(camera_rotZ)) * camera_distance;
     }

I have seen the "RotateAround" function being suggested, but wouldn't a more mathematical solution allow better control over things like the distance between the object and camera?

I was hoping to use this code to make a camera setup similar to that of Homeworld, so the player can drag their mouse and pan the camera around the object they have it attached to.

Help! (And thanks!)

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
1

Answer by Arkaid · Jul 12, 2016 at 01:37 AM

There's two things you can do, if you don't want to use RotateAround (which I still recommend)

1) Change the hierarchy of how you camera is set up so it's child of a pivot point. Something like this:

 L Pivot
   L Camera

Set up like that, all you have to do is change the parent of Pivot to that of the object you want to rotate around, like so

 pivot.transform.SetParent(target.transform)

Where target is whatever object you want to rotate around. Then all you have to do is rotate pivot and the camera will rotate around it. To change the distance, simply change the distance between Pivot and Camera, using Camera's localPosition values

2) If you want to get extra mathematical, look into spherical coordinates and Quaternions. Should fix the problem right away.

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

Answer by jimrota · Apr 13, 2017 at 08:00 PM

Using @Arkaid 's solution I created the following C# script and added it as a component to the the pivot. Works great!

  public class PanAroundObject : MonoBehaviour {

 [SerializeField]
 Transform pivot;

 void Start () {
     pivot = transform;
 }
 

 void Update () {
     pivot.Rotate(0,1,0);

 }
  }
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

82 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

Related Questions

How can I use Mathf.Clamp? 1 Answer

Camera Rotation Around Player 1 Answer

How to find a position's angle around an arbitrary axis? 1 Answer

Clamping Between Two Different Ranges 1 Answer

Unity3D accelerometer camera rotation realistic controls 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