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.
void FixedUpdate()
{
// if the is following option is activated.
if (isFollowing)
{
var velocityX = velocity.x * Time.deltaTime;
var velocityY = velocity.y * Time.deltaTime;
float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x + XOffset, ref velocityX, smoothTimeX);
float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y + YOffset, ref velocityY, smoothTimeY);
transform.position = new Vector3(posX, posY, transform.position.z);
}
// Set the camera bounds.
if (bounds)
{
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minCamPos.x, maxCamPos.x),
Mathf.Clamp(transform.position.y, minCamPos.y, maxCamPos.y),
Mathf.Clamp(transform.position.z, minCamPos.z, maxCamPos.z));
}
}
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.
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():
switch (command)
{
case EnumPlayerCommands.Move:
MoveHorizontal();
break;
case EnumPlayerCommands.Jump:
Jump();
break;
case EnumPlayerCommands.Flip:
Flip();
break;
default:
break;
}
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.