1
\$\begingroup\$

When my player moves, moving the camera looks like the camera is teleporting to the player, and then after loading teleporting again. I want to make it move smoothly.

When the camera is following the player position, it is moving smoothly.

Camera following code:

private void Update()
{
    Vector3 position = GetFollowingPoint();
    position = new Vector3(
        Mathf.Clamp(position.x,
                    Bounds.bounds.min.x + Camera.orthographicSize * Screen.width / Screen.height,
                    Bounds.bounds.max.x - Camera.orthographicSize * Screen.width / Screen.height),
        Mathf.Clamp(position.y,
                    Bounds.bounds.min.y + Camera.orthographicSize,
                    Bounds.bounds.max.y - Camera.orthographicSize),
                    transform.position.z);

    transform.position = position;
}

GetFollowingPoint():

return Player.transform.position + (cursor.transform.position - Player.transform.position) / 3;

Player movement: rigidbody.velocity = Vector in FixedUpdate(), Input with Input.GetKey() and Vector += Vector2.up/down/left/right.

Interpolation in Rigidbody set to Interpolate.

\$\endgroup\$
4
  • \$\begingroup\$ How do you move your player? If the problem is only visible when the player moves, then that code could be part of the cause. \$\endgroup\$
    – DMGregory
    Jun 9, 2019 at 13:24
  • \$\begingroup\$ @DMGregory With rigidbody.velocity = Vector in FixedUpdate(), Input with Input.GetKey() and Vector += Vector2.up/down/left/right \$\endgroup\$
    – Arasfon
    Jun 9, 2019 at 15:21
  • \$\begingroup\$ Ahhh, be sure to include that in your question. Have you configured interpolation on your Rigidbody? \$\endgroup\$
    – DMGregory
    Jun 9, 2019 at 15:22
  • \$\begingroup\$ @DMGregory Yes, set to Interpolate \$\endgroup\$
    – Arasfon
    Jun 9, 2019 at 15:23

1 Answer 1

1
\$\begingroup\$

The problem lies with the call of transform.position = position;

When you assign your new position directly to the transform of the object, you are hard-setting the position. You have to do what is called "Interpolation" which calculates an appropriate point between the current position and the destination you want it to approach. This smoothes out the movement per frame to a more reasonable amount.

The call you want to make is Vector3.MoveTowards(_current_position, _new_position, _speed);

You can see an example in the Unity Manual for Vector3.MoveTowards

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .