Smooth 2d Camera

Updated on October 4, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on October 2, 2018

How can I get my camera buttery smooth? The background seems a little choppy like its running at 15 fps 

 

Camera Script

private Transform target;
 private float smoothSpeed = 1000.0f;
 public Vector3 offset;
void Start()
 {
 GameObject go = GameObject.FindGameObjectWithTag("Player");
 target = go.transform;
 }
private void Update()
 {
 Vector3 desiredPosition = target.position + offset;
 Vector3 smoothedPostion = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
 transform.position = smoothedPostion;
 }

Player Movement

private void Update()
 {
 speed = 1.8f * accelerationPower;
 rb.AddRelativeForce(Vector2.up * speed * Time.deltaTime);
 direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up)));
}
  • Liked by
Reply
0 on October 2, 2018

Have you tried lowering your speed?

Lerp works by returning another vector on a line between the beg and end you give it. Where on that line is based on the 3rd value you give it. 0 being the beginning of the line (1st value), 0.5 is the middle of the line, and 1 is the end of the line (2nd value).

Time.deltaTime is the time that has passed between frames. At 60fps dt is about 0.016

0.016 * 1000 = 16

 

I think Lerp is clamped by default, so while you’re not setting it to 16 times past the end position, you’re still just straight setting it to the target position.

  • Liked by
Reply
Cancel
1 on October 4, 2018

Thanks @Mouledoux for reaching out. 

What is the correct smoothSpeed and is there a way to calculate this from the relative force being added? 

If I know the Force and speed forward – I should be able to calculate the perfect smoothSpeed from the Lerp’s beginning and end points, Correct?

Also if you have an iPhone, I’d like to give you the test flight public link to check it out – let me know. Thank you again for the insight

on October 4, 2018

Also, I plan on testing out the 2d Cinemachine camera and see if that helps

Show more replies
  • Liked by
Reply
Cancel
1 on October 4, 2018

Since the 2 position aren’t fixed, you could try just setting it to 1. The distance between the 2 points will continue to get smaller as one moves towards the other. So when they’re farther apart, they will move faster, and gradual get slower as they get closer and closer.

No iPhone, sorry. Android 4eva!

on October 4, 2018

ty

Show more replies
  • Liked by
Reply
Cancel