Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by spunktrumpet · Jan 12, 2014 at 05:16 PM · cameraquaternionrotatefollow

Rotate camera around player

I'm trying to get my camera to rotate around my player on a button press however I've only been able to get it to rotate on its own axis and not around the players position. Any ideas how I'd go about it in my script.

 using UnityEngine;
 using System.Collections;

 public class MouseOrbit : MonoBehaviour 
 {
 public Transform target;

 public float distance = 10.0f;

 private float x = 0.0f;
 private float y = 0.0f;
 
 void Start () 
 {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 }
 
 void Update () 
 {
     if (target)
     {
         transform.position = (Quaternion.Euler(y, x, 0)) * new Vector3(0.0f, 0.0f, -distance) + target.position;
     }
     if (Input.GetButton("CameraRotate"))
     {
         transform.rotation = Quaternion.Euler(y, x, 0);
     }
     if (Input.GetAxis("MouseScrollWheel") > 0)
     {
         if (distance >= 2)
         {
         distance = distance - 0.25f;
         }
     }
     if (Input.GetAxis("MouseScrollWheel") < 0)
     {
         if (distance <= 10)
         {
             distance = distance + 0.25f;
         }
     }
 }    
 }


The target = player.

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
3

Answer by robertbu · Jan 12, 2014 at 05:44 PM

I'm confused some about what you are trying to do with parts of your code. In particular the 'Input.GetButton("CameraRotate"). Anyway, here is a basic MouseOrbit script that you can build on. Note the default axis name for the scroll wheel is 'Mouse ScrollWheel'. Not sure if you changed the name or if there is a bug here.

sing UnityEngine; using System.Collections;

 public class MyOrbitScript : MonoBehaviour {
 
     public Transform target;
     public float distance = 10.0f;
     public float sensitivity = 3.0f;
 
     private Vector3 offset; 
 
     void Start ()  {
         offset = (transform.position - target.position).normalized * distance;
         transform.position = target.position + offset;
     }
 
     void Update () {
         Quaternion q = Quaternion.AngleAxis(Input.GetAxis ("Mouse ScrollWheel") * sensitivity, Vector3.up);
         offset = q * offset;
         transform.rotation = q * transform.rotation;
         transform.position = target.position + offset;
     }
 }
Comment
Add comment · Show 3 · 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 spunktrumpet · Jan 12, 2014 at 05:53 PM 0
Share

The mouse scroll is/was fine it just increases/decreases the distance between the camera and player. The 'Input.GetButton("CameraRotate")' is where I am trying to set the rotation of the camera around the player whenever CameraRotate is pressed which is set in my Input settings to Q.

avatar image · Mar 20, 2016 at 05:44 AM 0
Share

Thanks for the answer it really works well. Is there a way to make it smoother?

avatar image GamingTopTen · May 06, 2018 at 05:08 AM 0
Share

I'm using this as the groundwork for my rotation script. Thanks a thousand!

avatar image
1

Answer by Lagger625 · May 18, 2018 at 12:06 AM

I want to share my script to make the camera follow and rotate around the player. It even supports zooming in and out with the mouse wheel, all the settings exposed in the editor, and a mechanism to avoid seeing through the objects!

 using UnityEngine;
 
 public class PlayerCamera : MonoBehaviour {
 
     [SerializeField]
     private float turnSpeed = 4.0f, distanceFromPlayer = 3f, minDistance = 1f, maxDistance = 10f, zoomStep = .5f;
     [SerializeField]
     private Transform player;
     [SerializeField]
     private Vector3 startingRotation = new Vector3(20f, 0f, 0f);
 
     private Vector3 rotation;
 
     // couldn't find a better name...
     // distance to offset camera position to help avoid seeing through the floor or walls
     // still not a perfect mechanism
     private float onRaycastHitOffset = .7f;
 
     private void Start() {
         rotation = startingRotation;
     }
 
     private void LateUpdate () {
         Vector3 newPosition = player.position;
         distanceFromPlayer = Mathf.Clamp(distanceFromPlayer - Input.mouseScrollDelta.y * zoomStep, minDistance, maxDistance);
         transform.rotation = Quaternion.Euler(rotation += new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X")) * turnSpeed);
 
         // check if there is a collider between camera and player to move camera closer to the player
         RaycastHit hit;
         Ray ray = new Ray(newPosition, -transform.forward);
         if (Physics.Raycast(ray, out hit, distanceFromPlayer * (1f / onRaycastHitOffset))) {
             newPosition -= transform.forward * hit.distance * onRaycastHitOffset;
         } else {
             newPosition -= transform.forward * distanceFromPlayer;
         }
 
         transform.position = newPosition;
     }
 }
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

21 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

Related Questions

Camera rotation around player while following. 6 Answers

How to don't face player to camera direction? 1 Answer

Camera follow mouse around the map 2 Answers

How do I rotate a Rigidbody to a surface normal AND use camera rotation? 1 Answer

Top-Down Camera Trouble 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