• 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
Question by basukumar2222 · May 24, 2020 at 12:32 PM · cameraunity5camera-movementplayer movementuntiy3d

Making camera follow and rotate player

I'm making my camera follow the player. However, I can't figure out the best way to rotate the player. Here's my camera controller:

 using UnityEngine;
 
 namespace UltimateCameraController.Cameras.Controllers
 {
     [AddComponentMenu("Ultimate Camera Controller/Camera Controller")]
     public class CameraController : MonoBehaviour
     {
         [Header("Follow Settings")]
         [Space(10)]
         
         [Tooltip("Should the camera follow the target?")]
         public bool followTargetPosition = true; //Do we want our camera to follow the target object
         
         [Tooltip("The target object our camera should follow or orbit around")]
         public Transform targetObject; //The object that our camera should follow
 
         [Tooltip("The smooth factor when the camera follows a target object")]
         [Range(0.2f, 1f)]
         public float cameraFollowSmoothness; //The smooth factor when the camera follows a target object
 
         [Header("Orbit Settings")] 
         [Space(10)] 
         
         [Tooltip("Should the player be able to orbit around the target object?")]
         public bool orbitAroundTarget = true; //Do we want to add orbit functionality to the camera
         
         [Tooltip("The speed by which the camera rotates when orbiting")]
         [Range(2f, 15f)]
         public float rotationSpeed; //The speed by which the camera rotates when orbiting
 
         [Tooltip("The mouse button that the player must hold in order to orbit the camera")]
         public MouseButtons mouseButton; //The mouse button that the player must hold in order to orbit the camera
         
         private Vector3 _cameraOffset; //How far away is the camera from the target
         
         private void Start()
         {
             //We calculate the distance between the camera and the target
             _cameraOffset = transform.position - targetObject.position;
         }
 
         //We use late update so that player movement is completed before we move the camera
         //This way we can avoid glitches
         private void FixedUpdate()
         {
             //We do an error check
             if (targetObject == null)
             {
                 Debug.LogError("Target Object is not assigned. Please assign a target object in the inspector.");
                 return;
             }
             
             //If we want the camera to follow the target
             if (followTargetPosition)
             {
                 //We set the position the camera should move to, to the sum of the offset and the target's position
                 var newPosition = targetObject.position + _cameraOffset;
                 //We are moving slowly to the new position. The smooth factors determines how fast 
                 //the camera will move to its new postion
                 transform.position = Vector3.Slerp(transform.position, newPosition, cameraFollowSmoothness);
             }
 
             //If we want to make the player able to orbit around the target
             if (orbitAroundTarget)
             {
                 //We call the function to orbit the camera
                 OrbitCamera();
             }
         }
 
         //Method to handle Orbit of the Camera
         private void OrbitCamera()
         {
             //If the player holds the selected mouse button
             if (Input.GetMouseButton((int)mouseButton))
             {
                 //We cache the mouse rotation values multiplied by the rotation speed
                 float y_rotate = Input.GetAxis("Mouse X") * rotationSpeed;
                 float x_rotate = Input.GetAxis("Mouse Y") * rotationSpeed;
                 
                 //We calculate the rotation angles based on the cached values and a specific axes
                 Quaternion xAngle = Quaternion.AngleAxis(y_rotate, Vector3.up);
                 Quaternion yAngle = Quaternion.AngleAxis(x_rotate, Vector3.left);
 
                 //We multiply the rotation angle by the camera offset 
                 _cameraOffset = xAngle * _cameraOffset;
                 _cameraOffset = yAngle * _cameraOffset;
 
                 //We make our transform to "look" at the target        
                 transform.LookAt(targetObject);
             }
         }
     }
 
     //Custom enumerator that represents the mouse buttons
     public enum MouseButtons
     {
         LeftButton = 0,
         RightButton = 1,
         ScrollButton = 2
     };
 }

This is my camera zoom:

 using UnityEngine;
 
 namespace UltimateCameraController.Cameras.Controllers
 {
      //We use Require Component attribute to ensure that there is always a camera attached to the game object
      //so that we can effectively avoid null reference exceptions
      [RequireComponent(typeof(Camera))]
      [AddComponentMenu("Ultimate Camera Controller/Camera Zoom")]
      public class CameraZoom : MonoBehaviour
      {
          //We cache the target camera so that we won't need to use 
          //GetComponent every frame and thus we can boost performance
          private Camera targetCamera;
      
          [Header("Zoom Configuration")] 
          [Space(10)] 
          
          [SerializeField] 
          [Tooltip("Maximum Zoom Value")]
          private float zoomMax; //Maximum Zoom Value
          
          [SerializeField] 
          [Tooltip("Minimum Zoom Value")]
          private float zoomMin; //Minimum Zoom Value
      
          [Space(10)]
          
          [SerializeField] [Tooltip("Scroll Bar Sensitivity")] [Range(1, 100)]
          private float sensitivity; // Sensitivity Variable
      
          private void Start()
          {
              //Here we cache the target Camera
              targetCamera = GetComponent<Camera>();
          }
      
          private void Update()
          {
              //We cache camera's field of view in a temporary variable
              float fieldOfView = targetCamera.fieldOfView;
      
              //Simple calculation to perform the zoom functionality
              fieldOfView += Input.GetAxis("Mouse ScrollWheel") * sensitivity * -1;
      
              //We ensure that the camera's field of view is always within
              //the specified limits
              fieldOfView = Mathf.Clamp(fieldOfView, zoomMin, zoomMax);
      
              //We assign the temp field of view to the actual FOV in the camera
              targetCamera.fieldOfView = fieldOfView;
          }
      }
 }
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

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

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

288 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 to make camera follow player from point to point 2 Answers

Camera flickers while chasing the player 0 Answers

Mouvement issue with mouse input. 0 Answers

If player hits certain X value in opposition to Camera, shift camera question. 0 Answers

Locking Camera's rotation on a rolling character 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