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 JustJunuh · Jun 22, 2015 at 06:21 AM · cameradistancetranslatethird-personjitter

[SOLVED] Camera jitters while moving towards/away from player

Hello. I've been having this issue for basically the entire time I've been working on this project and I would REALLY like to solve this issue. I'm very new to the proper way to use cameras. So I tried this and it works, ok. (This code is for a third person platformer)

So I have it set so that the camera will always follow the player and translate forward based on the distance between them. capDistance is the set distance (float) that should be between the player and camera.

             if (distanceBetweenPlayerAndCamera > capDistance && frontCollision == false)  //If the camera is farther away from the player than the cap
             {
                 for (int i = 0; i < adjustSpeed; i++)  //Repeat a certain amount times and continually update distanceBetweenPlayerAndCamera
                 {
                     distanceBetweenPlayerAndCamera = Vector3.Distance (transform.position, new Vector3 (target.position.x, transform.position.y, target.position.z)); //Distance between camera and cube
                     float m = distanceBetweenPlayerAndCamera - capDistance; //Get the difference between the player and camera
                     transform.Translate (Vector3.forward * m * Time.deltaTime); //Move towards the player according to the difference
                 }
             }
                 
             if (distanceBetweenPlayerAndCamera < capDistance && backCollision == false)  //If the camera is closer to the player than the cap
             {
                 for (int i = 0; i < adjustSpeed * 2; i++)  //Repeat a certain amount times and continually update distanceBetweenPlayerAndCamera (times 2 to keep move faster than player)
                 {
                     distanceBetweenPlayerAndCamera = Vector3.Distance (transform.position, new Vector3 (target.position.x, transform.position.y, target.position.z)); //Distance between camera and cube
                     float m = capDistance - distanceBetweenPlayerAndCamera; //Get the difference between the player and camera
                     transform.Translate (Vector3.back * m * Time.deltaTime); //Move away from the player according to the difference
                 }
             }

So this works pretty good. Except, when the player is either moving away or towards the camera, the camera will jitter slightly. It's not a super crazy jitter but it is noticable and VERY annoying! The only other contributing factor would be this code that adjusts the y of the camera in the same fashion:

             if ((transform.position.y > yheight && downCollision == false) || (transform.position.y < yheight && topCollision == false))
             { //yheight is updated every 2 frames and is 3f higher than the player
                 for (int i = 0; i < adjustSpeed; i++)  
                 {
                     float ytemp = yheight - transform.position.y; //Find the difference between the proper y height and the current y height for the camera
                     transform.Translate (Vector3.up * ytemp * Time.deltaTime); //Adjust y accordingly
                 }
             }

Increasing the adjustspeed doesn't seem to do anything either :/ And when I decrease it, the camera moves too slow and the player and camera get separated by a great distance.

What is the strangest thing is there are a few, rare occasions when the camera won't jitter for a small bit on startup but will eventually go back to its old jitters before long.

Help?? New C# code would be appreciated! Thanks a TOOOON for anyone who helps me in advance!!

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 tanoshimi · Jun 22, 2015 at 06:22 AM 0
Share

Since this sounds like a ti$$anonymous$$g issue, it would be super-helpful to know whether you were moving the player and camera in Update(), FixedUpdate(), or LateUpdate().

2 Replies

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

Answer by JustJunuh · Jul 05, 2015 at 05:01 AM

Alright so I switched all the players movement over to rigidbodies and changed the camera code to FixedUpdate() and everything is fixed!

Thank you all for your help.

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
avatar image
0

Answer by AlwaysSunny · Jun 22, 2015 at 06:35 AM

Not for nothin, but this looks considerably more complicated than it needs to be. Also, it looks quite non-traditional, unless I'm just witnessing a lack of shortcut methods like Vector3.Lerp.

The best chase cams are really, really simple. At least until you get into stuff like moving it to avoid having geometry between the camera and its focused entity.

The first way to address camera jitter involves moving your camera in LateUpdate(){}. You can safely find the appropriate location for the camera during Update, but you should actually move it during LateUpdate.

If that doesn't improve the situation to your liking, also try visiting the script execution order page of the project settings, and make the camera script execute after other scripts.

If it still jitters noticeably, you might consider using an animation curve or other nonlinear method to dictate the distance the camera can move in a given frame according to its distance from its desired position. A standard linear interpolation is not always satisfactory.

The most important thing to realize is - and I struggled to comprehend this when I encountered camera jitter - there is nothing magical going on. It can be confusing, because the issue is tied to your perspective into the game world. Jitter is (usually, assuming we're using the word to mean the same thing) a manifestation of trying to move the camera without fully appreciating how it's being asked to move in a given frame. Any unsmoothed, significant, or otherwise unusual motion is going to add to the problem, and is often the root cause.

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 JustJunuh · Jun 25, 2015 at 07:08 AM 0
Share

Thanks so much for the help! I moved all the chase related code (the first section) to LateUpdate() and it works great! I also changed the script order to my liking and that helped as well.

And also, I kinda don't know how to use .Lerp in this situation. Whenever I do, I end up worse then I was before.

So the movement seems to be seamless at times. However sometimes, when I play the game, there are extremely tiny camera jitters. I can't tell why. Sometimes it works, sometimes it doesn't. $$anonymous$$ainly I've noticed, it works the first time when you boot up Unity, but every time you run the game after the first play it gets slightly jittery. I can't tell why it does this.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to translate a camera forward and back(basically a zoom) according to the distance of two objects? 1 Answer

Why is 2d camera not smooth at 30 FPS 0 Answers

How to measure the distance between an Augmented Reality marker and camera? 0 Answers

How to get the forward vector normal to the camera's forward vector regardless of camera pitch 1 Answer

3rd person camera reset bug 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