- Home /
Adding A Dampening Effect On Camera Following Player?
Currently, I am running a script attached to my main camera that follows my player as he moves along the x axis, but ignores the y axis by staying static.
How could I go about modifying this to incorporate a dampening effect to create asmoother camera transition (very jerky at the moment).
using UnityEngine;
using System.Collections;
public class CameraMove : MonoBehaviour {
public Transform player;
void Update ()
{
transform.position = new Vector3(player.position.x + 4, 0, -10);
}
}
Thanks for any help.
Comment
Answer by robertbu · Mar 06, 2014 at 04:07 AM
Take a look at Vector3.MoveTowards() or Vector3.Lerp(). Lots of posts on UA on both. An example with your script:
using UnityEngine;
using System.Collections;
public class Bug25a : MonoBehaviour {
public Transform player;
public float speed = 1.5f;
private Vector3 dest;
void Start() {
dest = new Vector3(player.position.x + 4, 0, -10);
}
void Update ()
{
dest = new Vector3(player.position.x + 4, 0, -10);
transform.position = Vector3.Lerp (transform.position, dest, speed * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613135501im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sprite seems to lose frames when moving 0 Answers
How to 2D Camera follow with lag. 1 Answer
Top down movement and rotation 2 Answers