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 /
  • Help Room /
avatar image
0
Question by SteenPetersen · May 09, 2017 at 02:52 PM · camera2drotationjitter

Jittery movement of the camera when rotating AND moving - 2D top down

Hello all,

I am making a 2d top down game where the player can rotate the camera around himself as he moves through the world.

Everything works fine when the player is moving around the world the camera follows him fine. if he standing still then the camera rotation also works very well. However as soon as I start to do both then there is a jittery in the camera that makes all other objects jitter besides the player.

Now in working with this problem I have found out that this could have to do with the fact that I use rigidbody2d.AddRelativeForce (so physics) to move the player and that his movements are checked in FixedUpdate.

https://forum.unity3d.com/threads/camera-jitter-problem.115224/ http://answers.unity3d.com/questions/381317/camera-rotation-jitterness-lookat.html

I have tried moving my camera rotation and the following scripts to LateUpdate, FixedUpdate, Update you name it. Nothing seems to work. I am sure that there is some sort of delay between movement of the camera and the rotation that is causing this. I am wondering if anyone has any feedback?

Scripts:

To Follow character, script applied to a gameobject that has the camera as a child

  public class FollowPlayer : MonoBehaviour {
  
      public Transform lookAt;
      public Spawner spawner;
      private Transform trans;
      public float cameraRot = 3;
  
      private bool smooth = false;
      private float smoothSpeed = 30f;
      private Vector3 offset = new Vector3(0, 0, -6.5f);
  
      //test
      public bool changeUpdate;
  
      private void Start()
      {
          trans = GetComponent<Transform>();
      }
  
      private void FixedUpdate()
      {
          CameraRotation();
      }
  
      private void LateUpdate()
      {
          following();
      }
  
      public void following()
      {
  
          Vector3 desiredPosition = lookAt.transform.position + offset;
  
          if (smooth)
          {
              transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
          }
          else
          {
              transform.position = desiredPosition;
          }
      }
  
      void CameraRotation()
      {
          if (Input.GetKey("q"))
          {
              transform.Rotate(0, 0, cameraRot);
          }
  
          if (Input.GetKey("e"))
          {
              transform.Rotate(0, 0, -cameraRot);
          }
      }
  
      public void SetTarget(string e)
      {
          lookAt = GameObject.Find(e).GetComponent<Transform>();
      }
  }

The character Controller, script applied to the Player, is called in FixedUpdate

      private void HandleMovement()
      {
  
          if (Input.GetKey("w"))
          {
              rigid.AddRelativeForce(Vector2.up * speed);
          }
  
          if (Input.GetKey("s"))
          {
              rigid.AddRelativeForce(Vector2.down * speed);
          }
  
          if (Input.GetKey("a"))
          {
              if (facingRight)
              {
                  Flip();
              }
              rigid.AddRelativeForce(Vector2.left * speed);
          }
  
          if (Input.GetKey("d"))
          {
              if (!facingRight)
              {
                  Flip();
              }
              rigid.AddRelativeForce(new Vector2(1,0) * speed);
          }
      }

Any Input is appreciated.

Thank you.

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

3 Replies

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

Answer by SteenPetersen · May 10, 2017 at 02:15 PM

I fixed this problem finally:

First I made my cameraRotation() and follow() into 1 function and gave it a better clearer name:

 public void UpdateCameraPosition()
     {
         if (Input.GetKey("q"))
         {
             transform.Rotate(0, 0, cameraRot);
         }
         else if (Input.GetKey("e"))
         {
             transform.Rotate(0, 0, -cameraRot);
         }
     
         Vector3 desiredPosition = lookAt.transform.position + offset;
     
         transform.position = desiredPosition;
     }

I then called that function from the character controller straight after the handling of movement. Both calls (handleMovement & cameraPosition) in fixedUpdate.

 void FixedUpdate()
 {
     HandleMovement();                         
     cameraController.UpdateCameraPosition(); 
 }

and the jittering was gone.

So it seems it was imply because there was a slight delay between the two calls as the previous posts I read had said. But I had failed to be able to properly Set them close enough together.

Hope this helps someone.

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 IgorAherne · Jun 29, 2018 at 04:31 PM

Searched the web and everybody is talking about FixedUpdate or LateUpdate which doesn't help me either. And I don't want any smooth lerping! :D


If I reposition my camera, and then make it look at my character, it will jitter. This is related to unity's order of building MVP matrix for the camera.


Literally, nobody mentioned this SO I AM GOING TO PUT MY SOLUTION IN BOLD. You have to sacrifice 1 frame of your camera, and set it to the previous lookAt position:

 Transform myLookTarget; //follow your gaze after this object
 Vector3 prevPos;
 
 void MyUpdate(){
       camera.transform.position = someSampledPosition;
       camera.transform.rotation = Quanternion.Lookat(previousPos);
       previousPos = myLookTarget.transform.position; //update AT THE END of the function
 }



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 True_Beef · Aug 11, 2018 at 03:50 PM

@IgorAgerne Nicely put.

Sorry to rev an old thread, but I also want to interject that using RotateTowards DOES NOT WORK for cam control. This method uses degrees of control meaning the faster you want it to go, the bigger the steps it will take, causing jitter. If you want to modify rotation directly (without using a vector to look at) Use a form of Slerping after positioning everything to make it look like butter. (All in fixed update, mind you.)

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

182 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

Related Questions

How do I get the current active camera? 2 Answers

Rotate towards mouse pointer 1 Answer

Extremely Specific 2D camera rotation problem 0 Answers

URP: use multiple render data together 0 Answers

Pixel jitter/flicker while camera movement (2D) 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