- Home /
Question by
WilliamRobitaille · Jan 25, 2021 at 05:36 PM ·
cameracamera-movementcamera follow
Make the camera switch offset on x axis with the character direction
I had a code working well for a 2D camera with smoothing factor and now I am trying to switch side on my x offset so the camera is always showing a bit forward depending on which direction my character is looking at. In this code, "offset" is a Vector3, so my z and y offset values work when the character moves right screen, but when I'm trying to create an else if statement of a negative movement (-x) I assume it does work on my x axis to say "- offset", but I would like to keep y and z the same as in the "+ offset" otherwise it throws the camera forward and down...
Maybe I'm simply not doing this properly, perhaps there's a more efficient way to make the camera switch offset on x axis with the character?
Thanks in advance!
void Follow()
{
if (movement > 0f)
{
Vector3 targetPosition = target.position + offset;
Vector3 smoothPosition = Vector3.Lerp(transform.position, targetPosition, smoothFactor * Time.fixedDeltaTime);
transform.position = smoothPosition;
}
else if (movement < 0f)
{
Vector3 targetPosition = target.position - offset;
Vector3 smoothPosition = Vector3.Lerp(transform.position, targetPosition, smoothFactor * Time.fixedDeltaTime);
transform.position = smoothPosition;
}
}
Comment