Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tenate · Jan 24, 2016 at 10:01 PM · c#camerathird-person

Weird Camera snap

I made two scripts, a movement script for the player and a rotation script for the camera that is following him. I have it so that the movements script can use the W, A, S, and D keys to move around and the mouse to rotate the player. My Camera script uses the mouse to rotate around the player. I made it so that whenever the player is standing still the camera can rotate around the player, however, when the camera moves the camera can no longer rotate and it switches to the player rotation while the camera stays locked on the player. My problem starts when I stop moving for the second time the camera snaps to the front of the player and it's really annoying. I have no idea what's causing the problem, I thought it may have been the camera still rotating and then when i stop it shows where the camera rotated to however that's not the case because in the scene the camera is following the player the whole time.

Player Movement:

         //Simple movement script
         transform.Translate (speed * Input.GetAxis ( "Horizontal" ) * Time.deltaTime, 0f, speed * Input.GetAxis ( "Vertical" ) * Time.deltaTime);
 
         //Rotation Script
         if (canRotate == true)
         {
             transform.localEulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * speed, 0);
         }
 
         if (lastPosition != transform.position)
         {
             canRotate = true;
         }
         else
         {
             canRotate = false;
         }
         lastPosition = transform.position;

Camera Movement:

         if (canRotate == true)
         {
             if (target)
             {
                 velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
 
                 rotationYAxis += velocityX;
                 rotationXAxis -= velocityY;
 
                 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
 
                 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
                 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
                 Quaternion rotation = toRotation;
 
 
                 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                 Vector3 position = rotation * negDistance + target.position;
 
                 transform.rotation = rotation;
                 transform.position = position;
 
                 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
                 velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
             }
         }
     }
   
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
1

Answer by lgarczyn · Jan 24, 2016 at 10:14 PM

Your problem might be this

 transform.localEulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * speed, 0);

Simply put, you should never read any rotation from your transform, only write. Quaternions are weird, and EulerAngles are worse.

So modify a Vector2 storing your x and y rotation, and then create a new Quaternion from that.

Also, you didn't put any camera reset code, so I don't know if you actually set the camera Quaternion back to identity or not, if canRotate happens to be false.

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 Tenate · Jan 25, 2016 at 05:42 AM 0
Share

Alright, I changed the euler to this:

         if (canRotate == true)
         {
             float rotY = Input.GetAxis("$$anonymous$$ouse X") * mouseSensitivity;
             transform.Rotate(0, rotY, 0);
         }

Which works fine, however, it still does the snap thing. Once my character stops moving and switches to the camera rotation the camera snaps in the opposite direction. It probably sounds confusing and I wish I could add a gif of what's happening. Also, how would I go about setting the camera reset code?

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

Third Person Camera Help? 0 Answers

Distribute terrain in zones 3 Answers

How to keep the player on the left side o the screen? 2 Answers

making a camera stay at the right distance (c#) 3 Answers

Third-person camera snaps to 270 degrees on activation 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