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 /
  • Help Room /
avatar image
0
Question by Banditmayonnaise · Mar 15, 2021 at 11:50 PM · camera2d-platformerrigidbody2dcamera follow

When player move the camera flickers and it looks janky

I made a Camera Script that follows the player using a lerp for making it look more smooth, but for some reason, it looks laggy when walking in some sudden areas.

The ground the player is walking on is tilemap, and at first, I thought there were tiny cell gaps that caused the problem, but it still happens even when I changed the ground to one solid block. So I concluded it must be something with Camera Script that cause this problem.

Here is a video clip of what I'm talking about: https://youtu.be/mmBMHWuHpxo

I think the problem stems from this part of my camera script:

   void SetTargetPos()
     {
         // By default the target x and y coordinates of the camera are it's current x and y coordinates.
         targetX = transform.position.x;
         targetY = transform.position.y;
         // If the player has moved beyond the x margin...
         if (CheckXMargin())
         {

     targetX = Mathf.Lerp(transform.position.x, transformPlayer.position.x, xSmooth * Time.deltaTime); 
         }
 
         // If the player has moved beyond the y margin...
         if (CheckYMargin())
         {

     targetY = Mathf.Lerp(transform.position.y, transformPlayer.position.y, ySmooth * Time.deltaTime);
         }
 
             targetX = Mathf.Clamp(targetX, currentMinBounds.x, currentMaxBounds.x);
             targetY = Mathf.Clamp(targetY, currentMinBounds.y, currentMaxBounds.y);
        
         
         // Set the camera's position to the target position with the same z component.
         transform.position = new Vector3(targetX, targetY, transform.position.z);
 
         TestOutOfCamBounds();
     }

So my question is how can I make the camera look less janky and more smooth. Just in case of the issue is not from this part of the camera script, I will post the entire script. The rest of the camera script:

 public class Camera_Controller : MonoBehaviour
 {
     private static Camera_Controller _instance;
     public static Camera_Controller instance;
     [Header("Put player here")]
     public GameObject player;
     public Transform transformPlayer;
     [Space]
     Vector2 currentMinBounds;
     Vector2 currentMaxBounds;
     public Vector3 targetPos;
 
     [Space]
     [Header("Camera Properties")]
     public float xMargin = 1f;
     public float yMargin = 1f;
     public float xSmooth = 8f;
     public float ySmooth = 8f;
 
     private float targetY;
     private float targetX;
 
     [Header("Smooth Transition")]
     public Vector3 oldPosition;
     public Vector3 targetPosition;
     public float transitionsTime;
     public bool switchingCamera;
 
     private void Awake()
     {
 
 
         if (_instance != null && _instance != this)
         {
             Destroy(gameObject);
             return;
         }
         else
         {
             _instance = this;
         }
 
         instance = _instance;
     }
 
     void Start()
     {
         targetPos = transform.position;
     }
 
     private bool CheckXMargin()
     {

         return Mathf.Abs(transform.position.x - transformPlayer.position.x) > xMargin;
     }
 
 
     private bool CheckYMargin()
     {

         return Mathf.Abs(transform.position.y - transformPlayer.position.y) > yMargin;
     }
     void Update()
     {
         if (!switchingCamera)
         {
             SetTargetPos();
         }
 
     }
 
     public void SetCamBounds(Vector2 minBounds, Vector2 maxBounds) //Called from Game Events Trough WarpController as trigger
     {
         currentMinBounds = minBounds;
         currentMaxBounds = maxBounds;
     }
     //SetTargetPos() should be causing the problem
     void SetTargetPos()
     {
         // By default the target x and y coordinates of the camera are it's current x and y coordinates.
         targetX = transform.position.x;
         targetY = transform.position.y;
         // If the player has moved beyond the x margin...
         if (CheckXMargin())
         {

             targetX = Mathf.Lerp(transform.position.x, transformPlayer.position.x, xSmooth * Time.deltaTime); 
         }
 
         // If the player has moved beyond the y margin...
         if (CheckYMargin())
         {

             targetY = Mathf.Lerp(transform.position.y, transformPlayer.position.y, ySmooth * Time.deltaTime);
         }
 
             targetX = Mathf.Clamp(targetX, currentMinBounds.x, currentMaxBounds.x);
             targetY = Mathf.Clamp(targetY, currentMinBounds.y, currentMaxBounds.y);
        
         
         // Set the camera's position to the target position with the same z component.
         transform.position = new Vector3(targetX, targetY, transform.position.z);
 
         TestOutOfCamBounds();
     }
 
     void TestOutOfCamBounds() //Set Camera some boundaries.
     {
         if (targetPos.x <= currentMinBounds.x)
         {
             targetPos.x = currentMinBounds.x;
         }
         if (targetPos.x >= currentMaxBounds.x)
         {
             targetPos.x = currentMaxBounds.x;
         }
         if (targetPos.y <= currentMinBounds.y)
         {
             targetPos.y = currentMinBounds.y;
         }
         if (targetPos.y >= currentMaxBounds.y)
         {
             targetPos.y = currentMaxBounds.y;
         }
     }
 
 }

Btw I have the exact same script on different Unity Projects with the same variable inputs. There is a small difference, and that just the overall size of everything in the second project is a lot smaller than the first one. Here is a video clip of that one: https://youtu.be/baJmKehYfG0

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 Banditmayonnaise · Mar 15, 2021 at 08:26 PM 0
Share

Btw I have the exact same script on different Unity Projects with the same variable inputs. There is a small difference, and that just the overall size of everything in the second project is a lot smaller than the first one. Here is a video clip of that one: https://youtu.be/baJmKehYfG0

0 Replies

· Add your reply
  • Sort: 

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

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

Unity2D Pixel Perfect Camera: Buggy Camera 0 Answers

How to stop the camera when the player has reached the edge of the level? 2 Answers

Why camera isn't following active character? 1 Answer

How to get the camera to follow a prefab? 2 Answers

Camera always behind player 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