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 DennisW83 · Jan 04, 2015 at 10:02 AM · cameravector3quaternion

Camera rotation over Player

Hi, Im very new to Gamedevelopment and currently Im doing my first Steps with Unity, but Im already facing a problem I can't solve with my small knowledge and hope someone could help me a bit.

I have a GameObject which should be my Player and the normal MainCamera in a scene.

I want to let the camera follow my player in a way that I can move the player with the keyboard and look around the player with my mouse inputs. I already have a script thats working, but there is one issue I don't understand. If I try to rotate above my player it works if I look straight forward, but if I rotate the player and try it again, the camera rotates beneath the player instead of rotating over it...

Im using this code:

Everything is working fine, only increaseCameraAngle is making problems, if I have rotated the player with updateCameraRotation before... If I didnt rotate first, the increase works fine... but after a rotation it increases to the left :-(

PlayerController

 void Update () {
         listenUserInputs ();
     }
 
     private void listenUserInputs () {
         float keyboardVertical = Input.GetAxis ("Vertical") * movementSpeed * Time.deltaTime;
         float keyboardHorizontal = Input.GetAxis ("Horizontal") * movementSpeed * Time.deltaTime;
         transform.Translate (keyboardHorizontal, 0, keyboardVertical);
         mainCamController.updateHumanPosition (transform);
 
         if (Input.GetMouseButton (0)) {
             float mouseVertical = Input.GetAxis("Mouse X") * turningSpeed * Time.deltaTime;
             transform.Rotate(0, mouseVertical, 0);
             mainCamController.updateCameraRotation(mouseVertical);
         }
 
         if (Input.GetAxis("Mouse ScrollWheel") < 0) {
             float mouseHorizontal = Input.GetAxis("Mouse ScrollWheel") * turningSpeed * 3 * Time.deltaTime;
             mainCamController.decreaseCameraAngle(mouseHorizontal);
         }
 
         if (Input.GetAxis("Mouse ScrollWheel") > 0) {
             float mouseHorizontal = Input.GetAxis("Mouse ScrollWheel") * turningSpeed * 3 * Time.deltaTime;
             mainCamController.increaseCameraAngle(mouseHorizontal);
         }
     }

CameraController

 void Update () {
         if (humanPosition != null) {
             transform.position = humanPosition.position + humanOffset; 
             transform.LookAt (humanPosition.position);
         }
     }
 
     public void updateHumanPosition (Transform newPosition) {
         this.humanPosition = newPosition;
     }
 
     public void updateCameraRotation (float newValue) {
         humanOffset = Quaternion.AngleAxis (newValue, Vector3.up) * humanOffset;
     }
 
     public void increaseCameraAngle (float newValue) {
         humanOffset = Quaternion.AngleAxis (newValue, Vector3.right) * humanOffset;
     }
 
     public void decreaseCameraAngle (float newValue) {
         humanOffset = Quaternion.AngleAxis (newValue, Vector3.right) * humanOffset;
     }


Comment
Add comment · Show 1
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 Eluate · Jan 04, 2015 at 12:38 PM 0
Share

Does decreaseCameraAngle cause problems as well? Why are using a variable that you are assigning in the quaternion? What is wrong with Transform.Rotate?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aidan · Jan 04, 2015 at 02:40 PM

Hi there, this forum post contains what you are looking for here.

Here is the script in c#

  public GameObject target;
     float radius = 3f, angleX = 0f, angleY = -45f;
    
     void Update()
     {
         radius -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 100f;
         angleX += Input.GetAxis("Mouse X") * Time.deltaTime;
         angleY += Input.GetAxis("Mouse Y") * Time.deltaTime;
  
         float x = radius * Mathf.Cos(angleX) * Mathf.Sin(angleY);
         float z = radius * Mathf.Sin(angleX) * Mathf.Sin(angleY);
         float y = radius * Mathf.Cos(angleY);
         transform.position = new Vector3(x + target.transform.position.x,
                                          y + target.transform.position.y,
                                          z + target.transform.position.z);
         transform.LookAt(target.transform.position);
     }

For longer time to transition from mouse pointer and target area, change Time.deltaTime to Time.deltaTime/2 and Time.deltaTime/3 and so on. Hope this helped!

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 DennisW83 · Jan 04, 2015 at 05:49 PM 0
Share

Hi, thank you very much... :-)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer

Object not moving in forward direction 1 Answer

Camera Rotation using Gyroscope: Yaw and Pitch reversed 2 Answers

Need explanation for this code 1 Answer

How to make Y-Axis face away from a position? 2 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