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 /
  • Help Room /
avatar image
0
Question by exscape · Apr 08, 2021 at 07:38 AM · cameracamera follow

Beginner orbit camera + player rotation issues

I have two goals with this and I can only get parts of them to work at once.

1) Mouse movement causes the camera to orbit around the player character, as is common in third person games.
2) The player rotates following the camera, so that you can walk by holding W and moving the mouse. Ideally I want the rotation to happen only while you move, and gradually, but that's a later goal.

For now the goal is to simply align the player forward axis with the camera's, without jitter.

Camera script:

 public class CameraFollower : MonoBehaviour
 {
     public GameObject cameraFollowTarget;
 
     private float inputSensitivity = 1.2f;
     private float cameraDistance = 8f;
 
     public float cameraYaw = 0f;
     private float cameraPitch = 20f;
 
     public float minCameraPitch = -60f;
     public float maxCameraPitch = 80f;
 
     private Vector3 cameraOffset = new Vector3();
 
     void Start()
     {
         Quaternion lookRotation = Quaternion.Euler(cameraPitch, cameraYaw, 0f);
         transform.localRotation = lookRotation;
     }
     private void updateCameraAngles()
     {
         float mouseX = Input.GetAxis("Mouse X") * inputSensitivity;
         float mouseY = Input.GetAxis("Mouse Y") * inputSensitivity;
         cameraYaw += mouseX;
         cameraPitch -= mouseY;
         cameraPitch = Mathf.Clamp(cameraPitch, minCameraPitch, maxCameraPitch);
     }
 
     private void LateUpdate()
     {
         updateCameraAngles();
 
         if (Input.GetKey(KeyCode.LeftShift)) {
             // This is really jittery/choppy, but player rotation works great
             Vector3 lookPosition = cameraFollowTarget.transform.position - transform.forward * cameraDistance;
             Quaternion lookRotation = Quaternion.Euler(cameraPitch, cameraYaw, 0f);
             transform.SetPositionAndRotation(lookPosition, lookRotation);
         }
         else
         {
             // This is really smooth, but the player only rotates half the angle I expect
             // (and cameraYaw reflects this half angle, vs the camera's actual rotation)
             cameraOffset.Set(0, 0, -cameraDistance);
             cameraOffset = Quaternion.AngleAxis(cameraPitch, Vector3.right) * cameraOffset;
             cameraOffset = Quaternion.AngleAxis(cameraYaw, Vector3.up) * cameraOffset;
 
             transform.position = cameraFollowTarget.transform.TransformPoint(cameraOffset);
             transform.LookAt(cameraFollowTarget.transform.position);
         }
     }
 }


Player script:

 public class Player : MonoBehaviour
 {
     [SerializeField] private CameraFollower cameraFollower;
 
     private void FixedUpdate()
     {
         // Attempt 1) Shift held: works, but jittery. Shift not held: player rotates HALF the camera angle, but it otherwise works
         // Printing cameraYaw also shows half the angle, e.g. rotate the camera 90 degrees and cameraYaw is 45 (and the character rotates 45 degrees as well)
         transform.rotation = Quaternion.AngleAxis(cameraFollower.cameraYaw, Vector3.up);
 
         // Attempt 2) Shift held: works, but jittery. Shift not held: rotates faster and faster based on mouse position
         // transform.rotation = Quaternion.AngleAxis(cameraFollower.transform.rotation.eulerAngles.y, Vector3.up);
 
         // Attempt 3) Same issues as the one above
         /*
         var rot = cameraFollower.transform.rotation;
         rot.x = 0;
         rot.z = 0;
         transform.rotation = rot;
         */
     }
 }

I can't figure this one out. Why is cameraYaw half the camera rotation angle (the camera's transform.rotation.eulerAngles.y) when shift is not held?

As far as I can tell, the camera tracking isn't dependent on the player rotation, only position, and cameraYaw is only modified by mouse input, so how can it depend on the player rotation?

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

Answer by exscape · Apr 08, 2021 at 09:06 AM

OK, I finally fixed the shift-held version; I realized that the position depends on the rotation, but I updated them at the same time. If you set the rotation first, and then calculate the position, it's stable.

             transform.rotation = Quaternion.Euler(cameraPitch, cameraYaw, 0f);
             transform.position = cameraFollowTarget.transform.position - transform.forward * cameraDistance;
 

I'm still confused about the yaw thing with the other version though!

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

221 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

Related Questions

Why camera isn't following active character? 1 Answer

How to get the camera to follow a prefab? 2 Answers

Camera always behind player 0 Answers

Object follows AR Camera 1 Answer

Unity2D Pixel Perfect Camera: Buggy Camera 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