• 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 ticknesh · Sep 16, 2022 at 12:54 PM · camera3dcamera-movementcamera rotatecamera follow

How to rotate the camera around the player both vertically and horizontally?

I am currently working on a 2.5D game which will be point and click style. I'm trying to create a camera that allows for you to zoom in, zoom out, rotate 360 degrees fully around the player horizontally, but also move up and down vertically with a max angle height and min angle height while staying focused on the center of the player. As of now, the zooming works perfectly and so does the horizontal rotation.

I have since added in Vertical Rotation which works decently on its own. However, if I add that in with the Horizontal Rotation, it just doesn't work at all. I'm thinking this is not working because I have created 3 new floats (cameraXAlt, cameraYAlt, and cameraZAlt) as well as a new Vector3 and it is having a conflict.

Below is my code for CameraFollows.cs. I've spent hours trying to figure out how to get both of these to work together but can't seem to get it right! Any help on how I can acheive the vertical rotation with the horizontal rotation would be greatly appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
 {
     // This is the main player camera
     public Camera playerCamera;
     // The camera will follow this target
     public Transform target;
 
     // The camera will be these many units away from player
     public float height = 15;
     public float angle = 0;
     public float rotationalSpeed = 3;
     public float radius = 10;
     public float maxFOV = 80f;
     public float minFOV = 15f;
     public float scrollSpeed = 0.5f;
     // Up and Down Camera
     public float radiusUpDown = 20;
     public float upDownSpeed = 0.005f;
     public float maxAngle = 3;
     public float minAngle = 2;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         // Horizontal Rotation of Camera
         float cameraX = target.position.x + (radius * Mathf.Cos(angle));
         float cameraY = target.position.y + height;
         float cameraZ = target.position.z + (radius * Mathf.Sin(angle));
 
         transform.position = new Vector3(cameraX, cameraY, cameraZ);
 
         // Pressing A or D on keyboard will rotate camera around player horizontally
         if (Input.GetKey(KeyCode.A)) {
             angle = angle - rotationalSpeed * Time.deltaTime;
         } else if (Input.GetKey(KeyCode.D)) {
             angle = angle + rotationalSpeed * Time.deltaTime;
         }
 
         // Vertical Rotation of Camera
         float cameraXAlt = target.position.x + height;
         float cameraYAlt = target.position.y + (radiusUpDown * Mathf.Sin(angle));
         float cameraZAlt = target.position.z;
 
         transform.position = new Vector3(cameraXAlt, cameraYAlt, cameraZAlt);
 
         // Pressing W or S on keyboard will rotate camera around player vertically
         if (Input.GetKey(KeyCode.W)) {
             angle = Mathf.Lerp(angle, minAngle, upDownSpeed);
         } else if (Input.GetKey(KeyCode.S)) {
             angle = Mathf.Lerp(angle, maxAngle, upDownSpeed);
         }
 
         // Zooming In with Scrollwheel
         if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
             playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, minFOV, scrollSpeed);
         }
 
         // Zooming Out with Scrollwheel
         if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
             playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, maxFOV, scrollSpeed);
         }
 
         transform.LookAt(target.position);
     }
 }
 
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

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

Related Questions

CineMachine Camera Movement is inverse 1 Answer

C# Smooth Follow Space Ship 1 Answer

Spyro Like Camera Follow 0 Answers

When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer

Making the camera rotation in line with the ball 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