- Home /
Question by
Tageos · Jan 06, 2016 at 05:25 PM ·
c#camera2dcamera follow
Choppy camera follow.
Hello!
I have a camera follow script that worked fine for a while. All of a sudden the camera started to move with a "choppy" movement when following the player. How could i fix this issue?
Camerafollow code:
using UnityEngine;
using System.Collections;
public class SmoothCamera2D : MonoBehaviour {
public float dampingTime;
private Vector3 velocity = Vector3.zero;
public Transform target;
private robotMovement robotScript;
void Update ()
{
if (target && target.position.x < 1.02f && target.position.x > -1.02f && robotScript.batteryOut == false)
{
Vector3 point = camera.WorldToViewportPoint(target.position);
Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.8f, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampingTime);
}
}
}
Comment
Best Answer
Answer by Runalotski · Jan 06, 2016 at 08:16 PM
Try putting and changes to the camera transform in late update so that it changes after the player has moved
LateUpdate()
{
//put camera transform code here
}
works that same as update but waits for all update functions to finish first.
This seem too work, thank you! The camera has stopped moving choppy. If i understand you correctly lateupdate gets called continuously 1 frame after every update function "frame"?
yes it is called once per frame after the all update functions have completed