• 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
1
Question by Bund187 · Mar 12, 2018 at 10:40 AM · camera2dvector3smoothvector3.lerp

Unity smooth following 2D camera not working properly on different resolutions

I´m working in a third person 2D game with oblique camera view in Unity and I want the camera to follow the character smoothly. I´m using Vector3.lerp and works like a charm in mostly all the resolutions but when I change to 1080x720 or 600x480 (for example) it doesn´t work well.

I´m using an othographic camera which orthographic size changes depending on the resolution for pixel perfect. When I deactivate the pixelPerfect script doesn´t work in any of the resolutions. I post the pixelPerfect script:

 [System.Serializable]
 public class PerfectOverride
 {
     public int referenceOrthographicSize;
     public float referencePixelsPerUnit;
 }
 
 public class PerfectPixel : MonoBehaviour
 {
     public int referenceOrthographicSize;
     public float referencePixelsPerUnit;
     public List<PerfectOverride> overrides;
 
     private int lastSize = 0;
 
     // Use this for initialization
     void Start()
     {
         UpdateOrthoSize();
     }
 
     PerfectOverride FindOverride(int size)
     {
         return overrides.FirstOrDefault(x => x.referenceOrthographicSize == size);
     }
 
     void UpdateOrthoSize()
     {
         lastSize = Screen.height;
 
         // first find the reference orthoSize
         float refOrthoSize = (referenceOrthographicSize / referencePixelsPerUnit) * 0.5f;
 
         // then find the current orthoSize
         var overRide = FindOverride(lastSize);
         float ppu = overRide != null ? overRide.referencePixelsPerUnit : referencePixelsPerUnit;
         float orthoSize = (lastSize / ppu) * 0.5f;
 
         // the multiplier is to make sure the orthoSize is as close to the reference as possible
         float multiplier = Mathf.Max(1, Mathf.Round(orthoSize / refOrthoSize));
 
         // then we rescale the orthoSize by the multipler
         orthoSize /= multiplier;
 
         // set it
         this.GetComponent<Camera>().orthographicSize = orthoSize;
 
         Debug.Log("Last Screen height "+lastSize + " Ortho Size" + orthoSize + " Multiplier " + multiplier + " PPU " + ppu);
     }
 
     // Update is called once per frame
     void Update()
     {
         #if UNITY_EDITOR
             if (lastSize != Screen.height)
             {
                 UpdateOrthoSize();
                 GetComponent<CameraCenterController>().CameraChanged = true;
             }
         #endif
     }
 }


I must say that, when I use a fixed value as the interpolate in Vector3.lerp, instead Time.Deltatime, it works fine (but less smooth) in all resolutions but still have problems with 1080x720 and 600x480 working only in the Y axis and not in the X.

I paste my code for understanding:

  public float speed;
  public Vector3 distance;

 private void FixedUpdate()
 {
         PlayerCentered();
 }
 
 void PlayerCentered()
 {
         Vector3 positionDesired = player.transform.position + distance;
         Vector3 smoothPosition= Vector3.Lerp(transform.position, positionDesired, speed /*Time.Deltatime*/);
         transform.position = smoothPosition;
 }


I would like to know if you guys know why this is happening and if there is a solution. I research about this problem and everybody talks about lerp and smoothDamp but nobody seems to have problems when changing the resolution.

Thanx!

Comment
Add comment · Show 5
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 FlaSh-G · Mar 12, 2018 at 12:10 PM 0
Share

Your code is not dependent on your resolution at all. So if the code sometimes works but sometimes not, it's either not really because of the resolution, or there's additional code involved somehow. Your other script that sets your camera's orthographic size for example. Does that one involve transform.position or something?

Also, using or not using Time.deltaTime here should not have an effect other than changing the lerp speed. Since you're in a FixedUpdate context, Time.deltaTime has the constant value of the fixed time steo, which is 0.02 by default.

avatar image Harinezumi FlaSh-G · Mar 13, 2018 at 09:09 AM 0
Share

Oh, wow, I didn't know Time.deltaTime returns Time.fixedDeltaTime from within FixedUpdate()! $$anonymous$$akes sense, but it is a bit obscure...
Anyway, as FlaSh-G said, the above code does not depend on resolution, it must be a different part of the code that affects the movement.

avatar image Bund187 FlaSh-G · Mar 13, 2018 at 11:00 AM 1
Share

That totally makes sense so I deactivate my pixelPerfect script for testing. After that, none of the resolutions are working. The strange thing is that works perfect on the Y axis, follows the player and centers on it, but the X axis follows the player too slow so the character goes our of the screen at some point and the camera never centers on it.

In the next example you can see, in the first part, how I would like to behave and when I change the resolution, how bad behaves: https://www.youtube.com/watch?v=6rYTWFu-Jj8

avatar image Harinezumi Bund187 · Mar 13, 2018 at 11:11 AM 0
Share

How do you calculate distance? In the video it is clearly visible that the camera moves to the side when you change the resolution, so I think that's what influences the camera position (could you share some code, please, preferably as an edit to the original question?). Do you have a CanvasScaler on the camera? That could explain why it works in y axis, but not the x.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

205 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

Related Questions

make my gameobject lerp between points more smoothly 0 Answers

CAMERA SMOOTH 2D player is not on the left side 0 Answers

How to smoothly move a 2D camera without ghosting 3 Answers

Why does the camera smoothly follow in one direction but not the other? 0 Answers

smooth follow 2d camera runner ios 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges