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
0
Question by Sendatsu_Yoshimitsu · May 19, 2014 at 08:56 AM · c#cameramouselookthird-person

Third-person camera snaps to 270 degrees on activation

I'm writing a third-person camera that usually trails behind the player, but pans in response to player input when they hold right click. The camera control script reads as follows:

 public Collider target;
     // The player's gameobject - we want to look at this
 new public Camera camera;
     // The main camera - the script should move and rotate this
 public float rotationUpdateSpeed = 60.0f,
     verticalRotationSpeed = 20.0f,
     followUpdateSpeed = 10.0f;
         // Variables to fine-tune camera performance
 private float optimalDistance;
            //How far the camera should be from the player
 private float mouseX;
 private float mouseY;
            //Store the GetAxis of the mouse in order to aim the camera 
 Quaternion cameraRotation;
 Vector3 cameraPosition;

 void Start (){
     optimalDistance = (camera.transform.position - target.transform.position).magnitude;
 }

 
 void Update (){
     if (Input.GetMouseButtonUp (1)) {
         mouseX = target.transform.rotation.z;
         mouseY = target.transform.rotation.y;
     }
 }
 

 void LateUpdate ()
 {
     if (Input.GetMouseButton (1)) {
         FreeUpdate ();
         } 
     else {
         FollowUpdate();
         }
     //Screen.lockCursor = true;

 }
 
 
 void FollowUpdate (){
 // Camera follows behind the player
     Vector3 cameraForward = target.transform.position - camera.transform.position;
     cameraForward = new Vector3 (cameraForward.x, 0.0f, cameraForward.z);
     float rotationAmount = Vector3.Angle (cameraForward, target.transform.forward);
     rotationAmount *= followUpdateSpeed * Time.deltaTime;
     
     if (Vector3.Angle (cameraForward, target.transform.right) < Vector3.Angle (cameraForward, target.transform.right * -1.0f)){
     // Rotate to the left if the camera is to the right of target forward
         rotationAmount *= -1.0f;
     }
 

     camera.transform.RotateAround (target.transform.position, Vector3.up, rotationAmount);
 }

 void FreeUpdate (){
     //Camera controlled by mouse
     mouseX += Input.GetAxis("Mouse X") * rotationUpdateSpeed * 0.02f;
     mouseY -= Input.GetAxis("Mouse Y") * verticalRotationSpeed * 0.02f;

     cameraRotation= Quaternion.Euler(mouseY, mouseX, 0f);
     cameraPosition= cameraRotation* new Vector3(0.0f, 2.0f, -optimalDistance) + target.transform.position;
     
     camera.transform.rotation = cameraRotation;
     camera.transform.position = cameraPosition;

 }

This works perfectly, except whenever I first click the right mouse button to active mouselook, instead of remaining behind the player until the mouse is moved, the camera.transform.rotation snaps to 270, so it's looking at the player's side. It pans correctly, and snaps back behind the player once I release the right button, but every time I turn mouselook on it begins by snapping back to 270 degrees rotation. How can I fix this so the camera starts behind the player (in its follow position) every time I right click to start mouselook?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by EvilWarren · May 19, 2014 at 11:07 AM

Your issue is the line:

 cameraRotation= Quaternion.Euler(mouseY, mouseX, 0f);

This orientation probably looks at the side of your character. This rotation is global and is not relative to the position of character/player.

Try modifying it to take the last known rotation of the camera like this:

     public Collider target;
     // The player's gameobject - we want to look at this
     new public Camera camera;
     // The main camera - the script should move and rotate this
     public float rotationUpdateSpeed = 60.0f,
     verticalRotationSpeed = 20.0f,
     followUpdateSpeed = 10.0f;
     // Variables to fine-tune camera performance
     private float optimalDistance;
     //How far the camera should be from the player
     private float mouseX;
     private float mouseY;
     //Store the GetAxis of the mouse in order to aim the camera 
 
     private Vector3 lastCameraRotationEuler;
 
     Quaternion cameraRotation;
     Vector3 cameraPosition;
     
     void Start (){
         optimalDistance = (camera.transform.position - target.transform.position).magnitude;
     }
     
     
     void Update (){
         if (Input.GetMouseButtonUp (1)) {
             mouseX = target.transform.rotation.z;
             mouseY = target.transform.rotation.y;
         }
     }
     
     
     void LateUpdate ()
     {
         if (Input.GetMouseButton (1)) {
             FreeUpdate ();
         } 
         else {
             FollowUpdate();
         }
         //Screen.lockCursor = true;
         
     }
     
     
     void FollowUpdate (){
         // Camera follows behind the player
         Vector3 cameraForward = target.transform.position - camera.transform.position;
         cameraForward = new Vector3 (cameraForward.x, 0.0f, cameraForward.z);
         float rotationAmount = Vector3.Angle (cameraForward, target.transform.forward);
         rotationAmount *= followUpdateSpeed * Time.deltaTime;
         
         if (Vector3.Angle (cameraForward, target.transform.right) < Vector3.Angle (cameraForward, target.transform.right * -1.0f)){
             // Rotate to the left if the camera is to the right of target forward
             rotationAmount *= -1.0f;
         }
         
         
         camera.transform.RotateAround (target.transform.position, Vector3.up, rotationAmount);
         lastCameraRotationEuler = camera.transform.rotation.eulerAngles;
     }
     
     void FreeUpdate (){
         //Camera controlled by mouse
         mouseX += Input.GetAxis("Mouse X") * rotationUpdateSpeed * 0.02f;
         mouseY -= Input.GetAxis("Mouse Y") * verticalRotationSpeed * 0.02f;
         
         cameraRotation= Quaternion.Euler(lastCameraRotationEuler.x+ mouseY, lastCameraRotationEuler.y+mouseX, lastCameraRotationEuler.z);
         cameraPosition= cameraRotation* new Vector3(0.0f, 2.0f, -optimalDistance) + target.transform.position;
         
         camera.transform.rotation = cameraRotation;
         camera.transform.position = cameraPosition;
         
     }

Just three extra lines to add using another Vector3 that stores the last camera position from the FollowUpdate() method. Just do a find for lastCameraRotationEuler in the code if you are unsure which lines are new.

Comment
Add comment · Show 1 · 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 Sendatsu_Yoshimitsu · May 19, 2014 at 11:25 AM 1
Share

Holy cow that works perfectly, you sir are a scholar and a gentleman! Thank you very much for the help, having a camera that works is a glorious thing ^_^

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

22 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

Related Questions

How to pan camera based on mouse position 0 Answers

Rotating Camera around player object 0 Answers

Problem with mouse look and second skybox cam 0 Answers

How can I pause the first person character camera rotation in this case?? 1 Answer

Multiple Cars not working 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