• 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 MattSawrey · Sep 12, 2015 at 06:35 PM · c#rotationcamera-movementcamera rotatesphere

How to make camera rotate spherically around an object

I'm trying to create a third-person camera controller that rotates the camera around the player at a fixed distance - think of a camera system like The Witcher 3's, or MGS V's for basic camera movement.

I have rotation around the Y axis working just fine, but rotation around the X axis is more of a problem. The camera doesn't rotate around the player at the widest circumference point, as it would if it move directly upwards when rotating, rather it loops in a smaller circle, depending upon it current Y axis rotation. It's hard to describe without a visual, but try out my code to see for yourself. I think it might be because I don't want to just rotate around the X axis, but rather the X and Z axis', depending upon my Y rotation. I haven't solved it yet though.

This is my camera code:

 public class CameraController1 : MonoBehaviour {
 
     public GameObject pointOfFocus;
 
     public float camRange;
 
     public float rotationSpeed;
 
     private float xRotationOffset, zRotationOffset;
 
     public float minRSSensitivity;
     
     void Awake()
     {
         transform.position = pointOfFocus.transform.position + new Vector3(0f, 0f, -camRange);
 
         //When the pointOfFocus inputs are correct to move the camera
         InputManager.cameraMove += MoveCamera;
     }
 
     void OnDestroy()
     {
         InputManager.cameraMove -= MoveCamera;
     }
 
     //Only gets called when pointOfFocus messes with left stick
     private void MoveCamera()
     {
         //check that the pointOfFocus input is strong enough to warrant cam movement on xRotationOffset
         if(!InputManager.rsXAxisInput.IsBetween(-minRSSensitivity, minRSSensitivity))
         {
             xRotationOffset = InputManager.rsXAxisInput * rotationSpeed;
         }
         else
         {
             xRotationOffset = 0f;
         }
 
         //check that the pointOfFocus input is strong enough to warrant cam movement on xRotationOffset
         if(!InputManager.rsZAxisInput.IsBetween(-minRSSensitivity, minRSSensitivity))
         {
             zRotationOffset = InputManager.rsZAxisInput * rotationSpeed;
         }
         else
         {
             zRotationOffset = 0f;
         }
     }
 
     void Update()
     {                
         transform.RotateAround(pointOfFocus.transform.position, Vector3.up, xRotationOffset);
         transform.RotateAround(pointOfFocus.transform.position, Vector3.right, zRotationOffset);
         transform.LookAt(pointOfFocus.transform.position);
     }
 }

To use it requires my InputManager as well. The Right Stick input has to be configured to my input manager for it to work:

 public class InputManager : MonoBehaviour {
 
     public static float rsXAxisInput{get; private set;}
     public static float rsZAxisInput{get; private set;}
 
     public static float lsXAxisInput{get; private set;}
     public static float lsZAxisInput{get; private set;}
 
     public delegate void InputControl();
     public static event InputControl characterMove;
     public static event InputControl cameraMove;
     public static event InputControl jump;
     public static event InputControl interact;
 
     void Update()
     {
         lsXAxisInput = Input.GetAxis("Horizontal");
         lsZAxisInput = Input.GetAxis("Vertical");
 
         rsXAxisInput = Input.GetAxis("360_rsHoriz");
         rsZAxisInput = Input.GetAxis("360_rsVert");
 
         //Joystick inputs
         if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
         {
             if(characterMove != null){characterMove();}
         }
 
         if(Input.GetAxis("360_rsHoriz") != 0 || Input.GetAxis("360_rsVert") != 0)
         {
             if(cameraMove != null){cameraMove();}
         }
     
         if(Input.GetButtonDown("360_AButton"))
         {
             if(jump != null){jump();}
         }
         if(Input.GetButtonDown("360_XButton"))
         {
             if(interact != null){interact();}
         }
     }
 }

Any suggestions would be appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

Problem getting Vector3.Slerp to rotate in the correct direction in specific situation 0 Answers

How to prevent camera going into terrain 0 Answers

Make Camera follow rotation of 2 Players 1 Answer

Unity's FPC apply camera deadzone for mouse 0 Answers

Rotating Camera on click and drag 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