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 Skyknighted · Oct 05, 2017 at 04:10 PM · cameracamera-movementcamera rotatecamera followcamera rotation

How to rotate the camera with the player smoothly without the camera snapping back and forth?

So basically I got my player gameobject to move and rotate simple enough. then I was able to get the camera to follow the player with a slight delay. So I basically want to create the same delay effect for the rotation of the camera. I partially completed the coding and set the smoothTimeZ float in the inspector to 0.5 but there is a problem....... when the player's Z rotation goes between -1 to 1 the camera snaps back and forth. I believe it is due to the nature of the eulerAngle code I used but I'm just wondering how do I fixed this

Here is the codes so far.

 public class FollowCamera2D : MonoBehaviour {
 
     private Vector3 velocity;
 
 
     public float smoothTimeY;
     public float smoothTimeX;
 
     public float smoothTimeZ;
 
 
     public GameObject player;
 
 
     // Use this for initialization
     void Start () {
 
         player = GameObject.FindGameObjectWithTag("Player");
         
     }
 
     void FixedUpdate()
     {
 
         //Camera moving according to player position factoring in smoothtime
         float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX);
         float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y, ref velocity.y, smoothTimeY);
 
 
         //Camera rotating according to player eulerangle
         float rotZ = Mathf.SmoothDamp(transform.eulerAngles.z, player.transform.eulerAngles.z, ref velocity.z, smoothTimeZ);
 
         transform.position = new Vector3(posX, posY, transform.position.z);
 
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, rotZ);
 
     }
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 Skyknighted · Oct 06, 2017 at 10:27 AM 0
Share

Ugh I forgot to mention this camera is meant for Unity 2D

2 Replies

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

Answer by ImpOfThePerverse · Oct 05, 2017 at 04:45 PM

I've been using the following function to condition euler angles:

     float PlusMinus180(float angle, float target)
     {
         while (angle > target + 180f)
         {
             angle -= 360f;
         }
         while (angle <= target - 180f)
         {
             angle += 360f;
         }
         return angle;
     }

So your code would looks something like:

 float zTemp = transform.eulerAngles.z;
 float rotZ = Mathf.SmoothDamp(zTemp, PlusMinus180(player.transform.eulerAngles.z, zTemp), ref velocity.z, smoothTimeZ);

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 Skyknighted · Oct 06, 2017 at 10:26 AM 0
Share

This works just fine thank you very much.

avatar image
0

Answer by BlakeSchreurs · Oct 05, 2017 at 04:39 PM

Here's how I do it... (Note the misspelling of RotationSpeed, because I'm apparently classy like that). I create a temp. game object to mirror my camera's position and orientation, and then use LookAt to figure out what the ideal rotation should be. I then rotate at a given speed toward that ideal rotation. RotationSpeed is a public parameter so that I can tinker with it in the editor.

         var go = new GameObject(); // a placeholder for the camera
         go.transform.position = transform.position; // Place where camera is
         go.transform.rotation = transform.rotation; // Rotate to how camera is rotated
         go.transform.LookAt(GazeTarget); // Ideal look-at-target rotation
         transform.rotation = Quaternion.Slerp(transform.rotation, go.transform.rotation, RotaitonSpeed * Time.deltaTime); // Spherical interploation of camera rotation
         Destroy(go); // Get rid of the placeholder
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

114 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

Related Questions

Camera gets stuck when cursor is locked 0 Answers

URGENT Cinemachine virtual camera while falling 1 Answer

How to orbit camera around player from top-down to sidescroller view? 1 Answer

camera zoom 1 Answer

Returning the camera back to original position, 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