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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera Smooth - Player moves little bit back when jump

Discussion in '2D' started by IgorUnity3D, Aug 22, 2017.

  1. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Hello,

    I'm facing a problem when I'm using smoothing on my camera.

    When my character jumps, he seems to come back a little before performing the jump animation. I have already checked the animations and they all seem to be right, besides having tried various soft scripts. In addition to trying to change the method of update (Update, FixedUpdate & LateUpdate).

    Everything seems to work that I leave the smoothing value down.

    Code (CSharp):
    1.   void FixedUpdate()
    2.         {
    3.  
    4.             // if the is following option is activated.
    5.             if (isFollowing)
    6.             {
    7.                 var velocityX = velocity.x * Time.deltaTime;
    8.                 var velocityY = velocity.y * Time.deltaTime;
    9.  
    10.                 float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x + XOffset, ref velocityX, smoothTimeX);
    11.                 float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y + YOffset, ref velocityY, smoothTimeY);
    12.                 transform.position = new Vector3(posX, posY, transform.position.z);
    13.  
    14.             }
    15.             // Set the camera bounds.
    16.             if (bounds)
    17.             {
    18.                 transform.position = new Vector3(Mathf.Clamp(transform.position.x, minCamPos.x, maxCamPos.x),
    19.                                                   Mathf.Clamp(transform.position.y, minCamPos.y, maxCamPos.y),
    20.                                                   Mathf.Clamp(transform.position.z, minCamPos.z, maxCamPos.z));
    21.  
    22.             }
    23.  
    24.         }
    Please see the video below:



    What could be happening?

    Any help is welcome!
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Generally it's best to use LateUpdate for camera scripts, so that everything in the scene has a chance to Update before the camera adjusts during LateUpdate, which is still before the frame is rendered.

    I don't think you should be altering the velocity before passing it into SmoothDamp. Just create a class variable to store the velocity, and keep passing it into SmoothDamp, which will allow SmoothDamp to keep track of its current velocity. Since it's a "ref" parameter, the function is responsible for assigning the new velocity each frame to your variable, for use in future frames.
     
  3. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66

    Actually the ideal to be doing the cemera follow something is in the function LateUpdate (including this is mentioned in the documentation). But I believe this is not the problem I am facing here. We declared the speed velocity passed to the SmoothDamp function in a global scope of class but that did not influence at all. I'm getting close to a solution when I've identified that the script that makes my character move can only process 1 "command" at a time, for example: First -> Move, Second -> Jump.

    In my case, it moves the player vertically first and after apply the horizontal speed, the script not process this two operations in the same frame.

    See the image please (with Trail Render component):




    I'll delete the list of commands I have in my FixedUpdate():

    Code (CSharp):
    1. switch (command)
    2.         {
    3.             case EnumPlayerCommands.Move:
    4.                 MoveHorizontal();
    5.                 break;
    6.             case EnumPlayerCommands.Jump:
    7.                 Jump();
    8.                 break;
    9.             case EnumPlayerCommands.Flip:
    10.                 Flip();
    11.                 break;
    12.             default:
    13.                 break;
    14.         }
    Makes sense, right?
     
    Last edited: Aug 22, 2017
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Yes I think that could potentially be the cause of the issue, and it would be advantageous to be able to process multiple actions in a single frame regardless.
     
    IgorUnity3D likes this.
  5. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    The problem was really this! Thank you for your attention!
     
    LiterallyJeff likes this.