Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera movement slows down

Discussion in '2D' started by therocketman2018, Aug 1, 2019.

  1. therocketman2018

    therocketman2018

    Joined:
    Apr 25, 2019
    Posts:
    46
    Camera follows player but slows down in the end when player reaches it's target position faster?
    I made it to dragg a bit, but when player stops, it starts to accelerate brake too slowly.

    Code (CSharp):
    1. targetPos =
    2. new Vector3(followTarget.transform.position.x, transform.position.y, followTarget.transform.position.z);
    3. transform.position = Vector3.Lerp(transform.position, targetPos, (moveSpeed+faster) * Time.deltaTime);
    4.  
    It is because of the TIme.deltaTime which slows it down
     
    Last edited: Aug 1, 2019
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Firstly, give this a read so you understand how Lerp works: https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/

    You're not using it in a way that gives you much control. The first and second parameters are supposed to be, generally speaking, unchanging values, and the third parameter is a number 0-1 which represents the percentage between the two values to return.

    So if you did "Mathf.Lerp(10, 20, 0.5f)" it would output 15, because it is 50% (0.5f) between 10 and 20.

    So what you've got is a first parameter that is constantly changing, and a third parameter that represents a tiny percentage from transform.position to targetPos. So the closer it gets to targetPos, the less and less it moves each frame, because you're using a tiny percentage of an ever-shrinking distance between transform.position and targetPos.

    The parameters you're using perhaps better fit MoveTowards?
    Code (CSharp):
    1. Vector3 step = (moveSpeed+faster) * Time.deltaTime;
    2. transform.position = Vector3.MoveTowards(transform.position, targetPos, step);
     
    therocketman2018 likes this.
  3. therocketman2018

    therocketman2018

    Joined:
    Apr 25, 2019
    Posts:
    46
    Thanks alot mate! I've been making a 2D Pixel Art game for 3 months, and now i'm in the point where everything's pixel perfect instead of character movement, so when i move my character while camera is following, the character sprite is jittering slightly because pixel perfect style can't handle sub-pixel positions, if i have understood correctly. Do you have any ideas on this problem? My sprites and every assets ppu is 64, I'm using pixel perfect camera and the camera solved the issue for the objects that are static, but every moving object "jitters" when the camera is moving.

    I can make the character move pixel perfect, but when i round every floating point number to integer, it will jump 64 pixels on every move, but yes it contains the pixel perfectness. Is it even possible to make good looking pixel perfect movement with 64x64 sprite and PPU 64?

    This problem makes me feel like i should've made my game PPU 1? :O But i've heard it eats lots of resources with big map and many sprites. There must be better way!
     
    Last edited: Aug 2, 2019
  4. therocketman2018

    therocketman2018

    Joined:
    Apr 25, 2019
    Posts:
    46
    So, this is the problem in it's all simplicity:


    There is no stuttering effect if the sprite moves pixel per pixel, but now it moves in sub-pixel areas and i can't get it working to move one pixel at a time

    Code (CSharp):
    1.  
    2. transform.localPosition = new Vector3(agentTransf.localPosition.x , transform.localPosition.y, agentTransf.localPosition.z);
    3.  
    I have a 3D nav mesh under the 2D world, so that's the reason of getting the positions from "agentTransf", but i believe the problem is in this code, because if i change the code like this: (just changing the transform values to an integer.)
    Code (CSharp):
    1. transform.localPosition = new Vector3((int)agentTransf.localPosition.x , transform.localPosition.y, (int)agentTransf.localPosition.z +zOffset);
    Then the player sprite hops/jumps 64 pixels on every move and is pixel perfect with the camera, and there's no jittering at all. BUT the problem is how can i make it jump only 1 pixel on every movement, when the PPU is 64?

    Or i don't really know! I've been stuck on this for a long time.
     
    Last edited: Aug 2, 2019
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You don't need to move by integers to be pixel perfect. Since your pixels-per-unit is 64, that means each pixel is 1/64 units in size, (0.015625). So you can move in those increments.

    So if you want to move 10 pixels to the right, you would move (10 * 0.015625) to the right.

    Also check this out:
    https://blogs.unity3d.com/2019/03/1...up-your-unity-project-for-retro-8-bits-games/

    Unity has a 2D Pixel Perfect package that you can use as of 2018.2 or so.
     
    Last edited: Aug 2, 2019
    therocketman2018 likes this.
  6. therocketman2018

    therocketman2018

    Joined:
    Apr 25, 2019
    Posts:
    46
    Thank you alot!! :) Now the sprites are moving perfeclty without stuttering effect. I corrected the Nav Mesh Agent movement like this:

    Code (CSharp):
    1. double sum = (hit.point.x / 0.015625);
    2. int sum2 = Mathf.RoundToInt((float)sum);
    3. double sum3 = (double)sum2 * 0.015625;
    4.  
    5. double sumy = (hit.point.z / 0.015625);
    6. int sum2y = Mathf.RoundToInt((float)sumy);
    7. double sum3y = (double)sum2y * 0.015625;
    8.  
    9. Vector3 pooint = new Vector3((float)sum3, hit.point.y, (float)sum3y);
    10.  
    11. agentNavMesh.SetDestination(pooint);
     
    LiterallyJeff likes this.